From 06d4cecf7f03adae35a50375cc4e91c9eda1fc9e Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Wed, 16 Apr 2014 19:14:26 +0200 Subject: [PATCH] MediaFile thumbnail event hooks + VideoThumbnails plugin The exception thrown from MediaFile will be caught and simply result in no thumbnail at all right now. In the future we might use a catch-all and have a "cannot generate preview"-icon or something. VideoThumbnails requires php5-ffmpeg and php5-gd. --- EVENTS.txt | 5 ++ classes/File.php | 5 ++ classes/File_thumbnail.php | 2 + lib/imagefile.php | 31 +++---- lib/mediafile.php | 57 ++++++++---- lib/unsupportedmediaexception.php | 39 ++++++++ .../VideoThumbnails/VideoThumbnailsPlugin.php | 90 +++++++++++++++++++ 7 files changed, 197 insertions(+), 32 deletions(-) create mode 100644 lib/unsupportedmediaexception.php create mode 100644 plugins/VideoThumbnails/VideoThumbnailsPlugin.php diff --git a/EVENTS.txt b/EVENTS.txt index 0a1951d640..705a761358 100644 --- a/EVENTS.txt +++ b/EVENTS.txt @@ -1440,3 +1440,8 @@ OtherAccountProfiles: Hook to add account profiles to a user account profile blo href: link to the profile text: text for the profile image: mini image for the profile + +CreateFileImageThumbnail: Hook to create image thumbnail source from a File +- $file: MediaFile object with related metadata +- &$imgPath: Path to image file which can be used as source for our thumbnail algorithm. +- $media: MIME media type ('image', 'video', 'audio' etc.) diff --git a/classes/File.php b/classes/File.php index 5f03e8bbd3..8d0623bf48 100644 --- a/classes/File.php +++ b/classes/File.php @@ -433,6 +433,11 @@ class File extends Managed_DataObject return File_thumbnail::getKV('file_id', $this->id); } + public function getPath() + { + return self::path($this->filename); + } + /** * Blow the cache of notices that link to this URL * diff --git a/classes/File_thumbnail.php b/classes/File_thumbnail.php index 7d62c405d2..04c9774bf5 100644 --- a/classes/File_thumbnail.php +++ b/classes/File_thumbnail.php @@ -84,6 +84,8 @@ class File_thumbnail extends Managed_DataObject /** * Save a thumbnail record for the referenced file record. * + * FIXME: Add error handling + * * @param int $file_id * @param string $url * @param int $width diff --git a/lib/imagefile.php b/lib/imagefile.php index 50be74b347..1648980c18 100644 --- a/lib/imagefile.php +++ b/lib/imagefile.php @@ -70,8 +70,7 @@ class ImageFile ($info[2] == IMAGETYPE_PNG && function_exists('imagecreatefrompng')))) { // TRANS: Exception thrown when trying to upload an unsupported image file format. - throw new Exception(_('Unsupported image file format.')); - return; + throw new UnsupportedMediaException(_('Unsupported image format.'), $this->filepath); } $this->type = ($info) ? $info[2]:$type; @@ -79,32 +78,38 @@ class ImageFile $this->height = ($info) ? $info[1]:$height; } + public function getPath() + { + if (!file_exists($this->filepath)) { + throw new ServerException('No file in ImageFile filepath'); + } + + return $this->filepath; + } + static function fromUpload($param='upload') { switch ($_FILES[$param]['error']) { case UPLOAD_ERR_OK: // success, jump out break; + case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: // TRANS: Exception thrown when too large a file is uploaded. // TRANS: %s is the maximum file size, for example "500b", "10kB" or "2MB". - throw new Exception(sprintf(_('That file is too big. The maximum file size is %s.'), - ImageFile::maxFileSize())); - return; + throw new Exception(sprintf(_('That file is too big. The maximum file size is %s.'), ImageFile::maxFileSize())); + case UPLOAD_ERR_PARTIAL: @unlink($_FILES[$param]['tmp_name']); // TRANS: Exception thrown when uploading an image and that action could not be completed. throw new Exception(_('Partial upload.')); - return; + case UPLOAD_ERR_NO_FILE: // No file; probably just a non-AJAX submission. - return; default: - common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " . - $_FILES[$param]['error']); + common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " . $_FILES[$param]['error']); // TRANS: Exception thrown when uploading an image fails for an unknown reason. throw new Exception(_('System error uploading file.')); - return; } $info = @getimagesize($_FILES[$param]['tmp_name']); @@ -112,8 +117,7 @@ class ImageFile if (!$info) { @unlink($_FILES[$param]['tmp_name']); // TRANS: Exception thrown when uploading a file as image that is not an image or is a corrupt file. - throw new Exception(_('Not an image or corrupt file.')); - return; + throw new UnsupportedMediaException(_('Not an image or corrupt file.'), '[deleted]'); } return new ImageFile(null, $_FILES[$param]['tmp_name']); @@ -176,7 +180,6 @@ class ImageFile if (!file_exists($this->filepath)) { // TRANS: Exception thrown during resize when image has been registered as present, but is no longer there. throw new Exception(_('Lost our file.')); - return; } // Don't crop/scale if it isn't necessary @@ -214,7 +217,6 @@ class ImageFile default: // TRANS: Exception thrown when trying to resize an unknown file type. throw new Exception(_('Unknown file type')); - return; } $image_dest = imagecreatetruecolor($width, $height); @@ -255,7 +257,6 @@ class ImageFile default: // TRANS: Exception thrown when trying resize an unknown file type. throw new Exception(_('Unknown file type')); - return; } imagedestroy($image_src); diff --git a/lib/mediafile.php b/lib/mediafile.php index 07d5c2dd26..4656122536 100644 --- a/lib/mediafile.php +++ b/lib/mediafile.php @@ -43,6 +43,7 @@ class MediaFile var $fileurl = null; var $short_fileurl = null; var $mimetype = null; + var $thumbnailRecord = null; function __construct(Profile $scoped, $filename = null, $mimetype = null) { @@ -51,7 +52,12 @@ class MediaFile $this->filename = $filename; $this->mimetype = $mimetype; $this->fileRecord = $this->storeFile(); - $this->thumbnailRecord = $this->storeThumbnail(); + try { + $this->thumbnailRecord = $this->storeThumbnail(); + } catch (UnsupportedMediaException $e) { + // FIXME: Add "unknown media" icon or something + $this->thumbnailRecord = null; + } $this->fileurl = common_local_url('attachment', array('attachment' => $this->fileRecord->id)); @@ -68,6 +74,11 @@ class MediaFile common_local_url('file', array('notice' => $notice->id))); } + public function getPath() + { + return File::path($this->filename); + } + function shortUrl() { return $this->short_fileurl; @@ -108,16 +119,30 @@ class MediaFile */ function storeThumbnail() { - if (substr($this->mimetype, 0, strlen('image/')) != 'image/') { - // @fixme video thumbs would be nice! - return null; + $imgPath = null; + $media = common_get_mime_media($this->mimetype); + + if (Event::handle('CreateFileImageThumbnail', array($this, &$imgPath, $media))) { + switch ($media) { + case 'image': + $imgPath = $this->getPath(); + break; + default: + throw new UnsupportedMediaException(_('Unsupported media format.'), $this->getPath()); + } + } + if (!file_exists($imgPath)) { + throw new ServerException(sprintf('Thumbnail source is not stored locally: %s', $imgPath)); } + try { - $image = new ImageFile($this->fileRecord->id, - File::path($this->filename)); - } catch (Exception $e) { - // Unsupported image type. - return null; + $image = new ImageFile($this->fileRecord->id, $imgPath); + } catch (UnsupportedMediaException $e) { + // Avoid deleting the original + if ($image->getPath() != $this->getPath()) { + $image->unlink(); + } + throw $e; } $outname = File::filename($this->scoped, 'thumb-' . $this->filename, $this->mimetype); @@ -128,7 +153,12 @@ class MediaFile list($width, $height) = $this->scaleToFit($image->width, $image->height, $maxWidth, $maxHeight); $image->resizeTo($outpath, $width, $height); - File_thumbnail::saveThumbnail($this->fileRecord->id, + + // Avoid deleting the original + if ($image->getPath() != $this->getPath()) { + $image->unlink(); + } + return File_thumbnail::saveThumbnail($this->fileRecord->id, File::url($outname), $width, $height); @@ -189,40 +219,33 @@ class MediaFile // TRANS: Client exception thrown when an uploaded file is larger than set in php.ini. throw new ClientException(_('The uploaded file exceeds the ' . 'upload_max_filesize directive in php.ini.')); - return; case UPLOAD_ERR_FORM_SIZE: throw new ClientException( // TRANS: Client exception. _('The uploaded file exceeds the MAX_FILE_SIZE directive' . ' that was specified in the HTML form.')); - return; case UPLOAD_ERR_PARTIAL: @unlink($_FILES[$param]['tmp_name']); // TRANS: Client exception. throw new ClientException(_('The uploaded file was only' . ' partially uploaded.')); - return; case UPLOAD_ERR_NO_FILE: // No file; probably just a non-AJAX submission. return; case UPLOAD_ERR_NO_TMP_DIR: // TRANS: Client exception thrown when a temporary folder is not present to store a file upload. throw new ClientException(_('Missing a temporary folder.')); - return; case UPLOAD_ERR_CANT_WRITE: // TRANS: Client exception thrown when writing to disk is not possible during a file upload operation. throw new ClientException(_('Failed to write file to disk.')); - return; case UPLOAD_ERR_EXTENSION: // TRANS: Client exception thrown when a file upload operation has been stopped by an extension. throw new ClientException(_('File upload stopped by extension.')); - return; default: common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " . $_FILES[$param]['error']); // TRANS: Client exception thrown when a file upload operation has failed with an unknown reason. throw new ClientException(_('System error uploading file.')); - return; } // Throws exception if additional size does not respect quota diff --git a/lib/unsupportedmediaexception.php b/lib/unsupportedmediaexception.php new file mode 100644 index 0000000000..df6916a76c --- /dev/null +++ b/lib/unsupportedmediaexception.php @@ -0,0 +1,39 @@ +. + * + * @category Exception + * @package GNUsocial + * @author Mikael Nordfeldth + * @copyright 2014 Free Software Foundation, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link https://www.gnu.org/software/social/ + */ + +if (!defined('GNUSOCIAL')) { exit(1); } + +class UnsupportedMediaException extends ServerException +{ + public function __construct($msg, $path) + { + common_debug(sprintf('UnsupportedMediaException "%1$s" for file "%2$s"', $msg, $path)); + parent::__construct($msg); + } +} diff --git a/plugins/VideoThumbnails/VideoThumbnailsPlugin.php b/plugins/VideoThumbnails/VideoThumbnailsPlugin.php new file mode 100644 index 0000000000..7ffc94c305 --- /dev/null +++ b/plugins/VideoThumbnails/VideoThumbnailsPlugin.php @@ -0,0 +1,90 @@ +. + * + * @category Plugin + * @package GNUsocial + * @author Mikael Nordfeldth + * @copyright 2014 Free Software Foundation http://fsf.org + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link https://www.gnu.org/software/social/ + */ + +if (!defined('GNUSOCIAL')) { exit(1); } + +/* + * Dependencies: + * php5-ffmpeg + * php5-gd + * + * Video support will depend on your ffmpeg. + */ + +class VideoThumbnailsPlugin extends Plugin +{ + /* + * This function should only extract an image from the video stream + * and disregard any scaling aspects in the resulting file, since + * this will be handled in the core thumbnail algorithm. + */ + public function onCreateFileImageThumbnail(MediaFile $file, &$imgPath, $media=null) + { + // This might accidentally pass application/ogg videos. + // If that's a problem, let's fix it in the calling function. + if ($media !== 'video') { + return true; + } + + $movie = new ffmpeg_movie($file->getPath(), false); + + $frames = $movie->getFrameCount(); + if ($frames > 0) { + $frame = $movie->getFrame(floor($frames/2)); + } else { + $frame = $movie->getNextKeyFrame(); + } + + // We failed to get a frame. + if ($frame === null) { + return true; + } + + // Let's save our frame to a temporary file. If we fail, remove it. + $imgPath = tempnam(sys_get_temp_dir(), 'socialthumb'); + if (!imagejpeg($frame->toGDImage(), $imgPath)) { + @unlink($imgPath); + return true; + } + return false; + } + + public function onPluginVersion(&$versions) + { + $versions[] = array('name' => 'Video Thumbnails', + 'version' => GNUSOCIAL_VERSION, + 'author' => 'Mikael Nordfeldth', + 'homepage' => 'https://www.gnu.org/software/social/', + 'rawdescription' => + // TRANS: Plugin description. + _m('HTML5 media attachment support')); + return true; + } +} + -- 2.39.5