/** * Handle the request * * @return void */ protected function handle() { parent::handle(); $profile = $this->user->getProfile(); $base64img = $this->img; if (stristr($base64img, 'image/jpeg')) { $base64img_mime = 'image/jpeg'; } elseif (stristr($base64img, 'image/png')) { // should convert to jpg here!! $base64img_mime = 'image/png'; } $base64img = str_replace('data:image/jpeg;base64,', '', $base64img); $base64img = str_replace('data:image/png;base64,', '', $base64img); $base64img = str_replace(' ', '+', $base64img); $base64img_hash = md5($base64img); $base64img = base64_decode($base64img); $base64img_basename = basename('cover'); $base64img_filename = File::filename($profile, $base64img_basename, $base64img_mime); $base64img_path = File::path($base64img_filename); $base64img_success = file_put_contents($base64img_path, $base64img); $base64img_mimetype = MediaFile::getUploadedMimeType($base64img_path, $base64img_filename); $mediafile = new MediaFile($profile, $base64img_filename, $base64img_mimetype); $imagefile = new ImageFile($mediafile->fileRecord->id, File::path($mediafile->filename)); $imagefile->resizeTo(File::path($mediafile->filename), array('width' => $this->cropW, 'height' => $this->cropH, 'x' => $this->cropX, 'y' => $this->cropY, 'w' => $this->cropW, 'h' => $this->cropH)); $result['url'] = File::url($mediafile->filename); Profile_prefs::setData($profile, 'qvitter', 'cover_photo', $result['url']); $this->initDocument('json'); $this->showJsonObjects($result); $this->endDocument('json'); }
public function __construct($page) { $this->kirby = $page->kirby; $this->site = $page->site; $this->page = $page; $inventory = $page->inventory(); foreach ($inventory['files'] as $filename) { $file = new File($this, $filename); $this->data[strtolower($file->filename())] = $file; } }
private function getPageTypes() { $listitems = array(); // Add the Blank Page type $listitems[] = 'Blank Page'; // Add all available templates to the list foreach (scandir(Constants::$ROOT_DIR . 'templates/sandbox') as $file) { if (File::fileExtension($file) == "html") { // Add the filename to the list $listitems[] = File::filename($file); } } return $listitems; }
function makeBackupFile($user) { // XXX: this is pretty lose-y; try another way $tmpdir = sys_get_temp_dir() . '/offline-backup/' . $user->nickname . '/' . common_date_iso8601(common_sql_now()); common_log(LOG_INFO, 'Writing backup data to ' . $tmpdir . ' for ' . $user->nickname); mkdir($tmpdir, 0700, true); $this->dumpNotices($user, $tmpdir); $this->dumpFaves($user, $tmpdir); $this->dumpSubscriptions($user, $tmpdir); $this->dumpSubscribers($user, $tmpdir); $this->dumpGroups($user, $tmpdir); $fileName = File::filename($user->getProfile(), "backup", "application/atom+xml"); $fullPath = File::path($fileName); $this->makeActivityFeed($user, $tmpdir, $fullPath); $this->delTree($tmpdir); return $fileName; }
/** * Handle the request * * @return void */ protected function handle() { parent::handle(); $profile = $this->user->getProfile(); $base64img = $this->img; if (stristr($base64img, 'image/jpeg')) { $base64img_mime = 'image/jpeg'; } elseif (stristr($base64img, 'image/png')) { // should convert to jpg here!! $base64img_mime = 'image/png'; } $base64img = str_replace('data:image/jpeg;base64,', '', $base64img); $base64img = str_replace('data:image/png;base64,', '', $base64img); $base64img = str_replace(' ', '+', $base64img); $base64img_hash = md5($base64img); $base64img = base64_decode($base64img); $base64img_basename = basename('avatar'); $base64img_filename = File::filename($profile, $base64img_basename, $base64img_mime); $base64img_path = File::path($base64img_filename); $base64img_success = file_put_contents($base64img_path, $base64img); $base64img_mimetype = MediaFile::getUploadedMimeType($base64img_path, $base64img_filename); $mediafile = new MediaFile($profile, $base64img_filename, $base64img_mimetype); $imagefile = new ImageFile($mediafile->fileRecord->id, File::path($mediafile->filename)); $imagefile->resizeTo(File::path($mediafile->filename), array('width' => $this->cropW, 'height' => $this->cropH, 'x' => $this->cropX, 'y' => $this->cropY, 'w' => $this->cropW, 'h' => $this->cropH)); $type = $imagefile->preferredType(); $filename = Avatar::filename($profile->id, image_type_to_extension($type), null, common_timestamp()); $filepath = Avatar::path($filename); $imagefile->copyTo($filepath); $profile = $this->user->getProfile(); $profile->setOriginal($filename); $mediafile->delete(); $twitter_user = $this->twitterUserArray($profile, true); $this->initDocument('json'); $this->showJsonObjects($twitter_user); $this->endDocument('json'); }
/** * Store the full-length scrubbed HTML of a remote notice to an attachment * file on our server. We'll link to this at the end of the cropped version. * * @param string $title plaintext for HTML page's title * @param string $rendered HTML fragment for HTML page's body * @return File */ function saveHTMLFile($title, $rendered) { $final = sprintf("<!DOCTYPE html>\n" . '<html><head>' . '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' . '<title>%s</title>' . '</head>' . '<body>%s</body></html>', htmlspecialchars($title), $rendered); $filename = File::filename($this->localProfile(), 'ostatus', 'text/html'); $filepath = File::path($filename); $fileurl = File::url($filename); file_put_contents($filepath, $final); $file = new File(); $file->filename = $filename; $file->urlhash = File::hashurl($fileurl); $file->url = $fileurl; $file->size = filesize($filepath); $file->date = time(); $file->mimetype = 'text/html'; $file_id = $file->insert(); if ($file_id === false) { common_log_db_error($file, "INSERT", __FILE__); // TRANS: Server exception. throw new ServerException(_m('Could not store HTML content of long post as file.')); } return $file; }
/** * Check if a file is present in the current value * * @since 1.0.0 * * @param \File $file * @return bool */ public function isInValue($file) { return in_array($file->filename(), $this->value()); }
static function fromFilehandle($fh, $user) { $stream = stream_get_meta_data($fh); if (!MediaFile::respectsQuota($user, filesize($stream['uri']))) { // Should never actually get here // TRANS: Client exception thrown when a file upload operation would cause a user to exceed a set quota. throw new ClientException(_('File exceeds user\'s quota.')); return; } $mimetype = MediaFile::getUploadedFileType($fh); $filename = null; if (isset($mimetype)) { $filename = File::filename($user->getProfile(), "email", $mimetype); $filepath = File::path($filename); $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664); if (!$result) { // TRANS: Client exception thrown when a file upload operation fails because the file could // TRANS: not be moved from the temporary folder to the permanent file location. throw new ClientException(_('File could not be moved to destination directory.' . $stream['uri'] . ' ' . $filepath)); } } else { // TRANS: Client exception thrown when a file upload operation has been stopped because the MIME // TRANS: type of the uploaded file could not be determined. throw new ClientException(_('Could not determine file\'s MIME type.')); return; } return new MediaFile($user, $filename, $mimetype); }
public function add($filename) { $file = new File($this, $filename); $this->data[$file->filename()] = $file; return $file; }
static function fromFilehandle($fh, $user) { $stream = stream_get_meta_data($fh); if (!MediaFile::respectsQuota($user, filesize($stream['uri']))) { // Should never actually get here throw new ClientException(_('File exceeds user\'s quota.')); return; } $mimetype = MediaFile::getUploadedFileType($fh); $filename = null; if (isset($mimetype)) { $filename = File::filename($user->getProfile(), "email", $mimetype); $filepath = File::path($filename); $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664); if (!$result) { throw new ClientException(_('File could not be moved to destination directory.' . $stream['uri'] . ' ' . $filepath)); } } else { throw new ClientException(_('Could not determine file\'s MIME type.')); return; } return new MediaFile($user, $filename, $mimetype); }
static function fromFilehandle($fh, Profile $scoped) { $stream = stream_get_meta_data($fh); // So far we're only handling filehandles originating from tmpfile(), // so we can always do hash_file on $stream['uri'] as far as I can tell! $filehash = hash_file(File::FILEHASH_ALG, $stream['uri']); try { $file = File::getByHash($filehash); // Already have it, so let's reuse the locally stored File $filename = $file->filename; $mimetype = $file->mimetype; } catch (NoResultException $e) { File::respectsQuota($scoped, filesize($stream['uri'])); $mimetype = self::getUploadedMimeType($stream['uri']); switch (common_config('attachments', 'filename_base')) { case 'upload': $filename = File::filename($scoped, "email", $mimetype); break; case 'hash': default: $filename = strtolower($filehash) . '.' . File::guessMimeExtension($mimetype); } $filepath = File::path($filename); $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664); if (!$result) { // TRANS: Client exception thrown when a file upload operation fails because the file could // TRANS: not be moved from the temporary folder to the permanent file location. throw new ClientException(_('File could not be moved to destination directory.' . $stream['uri'] . ' ' . $filepath)); } } return new MediaFile($scoped, $filename, $mimetype, $filehash); }
/** * Handle the request * * @return void */ protected function handle() { parent::handle(); $profile = $this->user->getProfile(); // see if we have regular uploaded image data try { $mediafile = MediaFile::fromUpload('banner', $profile); } catch (NoUploadedMediaException $e) { // if not we may have base64 data $img = $this->img; if (stristr($img, 'image/jpeg')) { $img_mime = 'image/jpeg'; } elseif (stristr($img, 'image/png')) { // should convert to jpg here!! $img_mime = 'image/png'; } // i don't remember why we had to do this $img = str_replace('data:image/jpeg;base64,', '', $img); $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $img = base64_decode($img, true); try { $img_filename = File::filename($profile, 'cover', $img_mime); $img_path = File::path($img_filename); $img_success = file_put_contents($img_path, $img); $img_mimetype = MediaFile::getUploadedMimeType($img_path, $img_filename); $mediafile = new MediaFile($profile, $img_filename, $img_mimetype); } catch (Exception $e) { $this->clientError($e, 400); } } if (!$mediafile instanceof MediaFile) { $this->clientError(_('Could not process image data.'), 400); } // maybe resize $width = $this->cropW; $height = $this->cropH; $scale = 1; if ($width > 1200) { $scale = 1200 / $width; } elseif ($height > 600) { $scale = 600 / $height; } $width = round($width * $scale); $height = round($height * $scale); // crop try { $imagefile = new ImageFile($mediafile->fileRecord->id, File::path($mediafile->filename)); $imagefile->resizeTo(File::path($mediafile->filename), array('width' => $width, 'height' => $height, 'x' => $this->cropX, 'y' => $this->cropY, 'w' => $this->cropW, 'h' => $this->cropH)); $result['url'] = File::url($mediafile->filename); } catch (Exception $e) { $this->clientError(_('The image could not be resized and cropped. ' . $e), 422); } // save in profile_prefs try { Profile_prefs::setData($profile, 'qvitter', 'cover_photo', $result['url']); } catch (ServerException $e) { $this->clientError(_('The image could not be resized and cropped. ' . $e), 422); } // return json $this->initDocument('json'); $this->showJsonObjects($result); $this->endDocument('json'); }
/** * Generate file slug. * * @since 1.0.0 * * @param \File $file * @return string */ public function itemId($file) { return $this->name() . '-' . str::slug($file->filename()); }