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.
This commit is contained in:
parent
00aa3fa734
commit
06d4cecf7f
|
@ -1440,3 +1440,8 @@ OtherAccountProfiles: Hook to add account profiles to a user account profile blo
|
||||||
href: link to the profile
|
href: link to the profile
|
||||||
text: text for the profile
|
text: text for the profile
|
||||||
image: mini image 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.)
|
||||||
|
|
|
@ -433,6 +433,11 @@ class File extends Managed_DataObject
|
||||||
return File_thumbnail::getKV('file_id', $this->id);
|
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
|
* Blow the cache of notices that link to this URL
|
||||||
*
|
*
|
||||||
|
|
|
@ -84,6 +84,8 @@ class File_thumbnail extends Managed_DataObject
|
||||||
/**
|
/**
|
||||||
* Save a thumbnail record for the referenced file record.
|
* Save a thumbnail record for the referenced file record.
|
||||||
*
|
*
|
||||||
|
* FIXME: Add error handling
|
||||||
|
*
|
||||||
* @param int $file_id
|
* @param int $file_id
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param int $width
|
* @param int $width
|
||||||
|
|
|
@ -70,8 +70,7 @@ class ImageFile
|
||||||
($info[2] == IMAGETYPE_PNG && function_exists('imagecreatefrompng')))) {
|
($info[2] == IMAGETYPE_PNG && function_exists('imagecreatefrompng')))) {
|
||||||
|
|
||||||
// TRANS: Exception thrown when trying to upload an unsupported image file format.
|
// TRANS: Exception thrown when trying to upload an unsupported image file format.
|
||||||
throw new Exception(_('Unsupported image file format.'));
|
throw new UnsupportedMediaException(_('Unsupported image format.'), $this->filepath);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->type = ($info) ? $info[2]:$type;
|
$this->type = ($info) ? $info[2]:$type;
|
||||||
|
@ -79,32 +78,38 @@ class ImageFile
|
||||||
$this->height = ($info) ? $info[1]:$height;
|
$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')
|
static function fromUpload($param='upload')
|
||||||
{
|
{
|
||||||
switch ($_FILES[$param]['error']) {
|
switch ($_FILES[$param]['error']) {
|
||||||
case UPLOAD_ERR_OK: // success, jump out
|
case UPLOAD_ERR_OK: // success, jump out
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case UPLOAD_ERR_INI_SIZE:
|
case UPLOAD_ERR_INI_SIZE:
|
||||||
case UPLOAD_ERR_FORM_SIZE:
|
case UPLOAD_ERR_FORM_SIZE:
|
||||||
// TRANS: Exception thrown when too large a file is uploaded.
|
// TRANS: Exception thrown when too large a file is uploaded.
|
||||||
// TRANS: %s is the maximum file size, for example "500b", "10kB" or "2MB".
|
// 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.'),
|
throw new Exception(sprintf(_('That file is too big. The maximum file size is %s.'), ImageFile::maxFileSize()));
|
||||||
ImageFile::maxFileSize()));
|
|
||||||
return;
|
|
||||||
case UPLOAD_ERR_PARTIAL:
|
case UPLOAD_ERR_PARTIAL:
|
||||||
@unlink($_FILES[$param]['tmp_name']);
|
@unlink($_FILES[$param]['tmp_name']);
|
||||||
// TRANS: Exception thrown when uploading an image and that action could not be completed.
|
// TRANS: Exception thrown when uploading an image and that action could not be completed.
|
||||||
throw new Exception(_('Partial upload.'));
|
throw new Exception(_('Partial upload.'));
|
||||||
return;
|
|
||||||
case UPLOAD_ERR_NO_FILE:
|
case UPLOAD_ERR_NO_FILE:
|
||||||
// No file; probably just a non-AJAX submission.
|
// No file; probably just a non-AJAX submission.
|
||||||
return;
|
|
||||||
default:
|
default:
|
||||||
common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
|
common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " . $_FILES[$param]['error']);
|
||||||
$_FILES[$param]['error']);
|
|
||||||
// TRANS: Exception thrown when uploading an image fails for an unknown reason.
|
// TRANS: Exception thrown when uploading an image fails for an unknown reason.
|
||||||
throw new Exception(_('System error uploading file.'));
|
throw new Exception(_('System error uploading file.'));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$info = @getimagesize($_FILES[$param]['tmp_name']);
|
$info = @getimagesize($_FILES[$param]['tmp_name']);
|
||||||
|
@ -112,8 +117,7 @@ class ImageFile
|
||||||
if (!$info) {
|
if (!$info) {
|
||||||
@unlink($_FILES[$param]['tmp_name']);
|
@unlink($_FILES[$param]['tmp_name']);
|
||||||
// TRANS: Exception thrown when uploading a file as image that is not an image or is a corrupt file.
|
// 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.'));
|
throw new UnsupportedMediaException(_('Not an image or corrupt file.'), '[deleted]');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ImageFile(null, $_FILES[$param]['tmp_name']);
|
return new ImageFile(null, $_FILES[$param]['tmp_name']);
|
||||||
|
@ -176,7 +180,6 @@ class ImageFile
|
||||||
if (!file_exists($this->filepath)) {
|
if (!file_exists($this->filepath)) {
|
||||||
// TRANS: Exception thrown during resize when image has been registered as present, but is no longer there.
|
// TRANS: Exception thrown during resize when image has been registered as present, but is no longer there.
|
||||||
throw new Exception(_('Lost our file.'));
|
throw new Exception(_('Lost our file.'));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't crop/scale if it isn't necessary
|
// Don't crop/scale if it isn't necessary
|
||||||
|
@ -214,7 +217,6 @@ class ImageFile
|
||||||
default:
|
default:
|
||||||
// TRANS: Exception thrown when trying to resize an unknown file type.
|
// TRANS: Exception thrown when trying to resize an unknown file type.
|
||||||
throw new Exception(_('Unknown file type'));
|
throw new Exception(_('Unknown file type'));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$image_dest = imagecreatetruecolor($width, $height);
|
$image_dest = imagecreatetruecolor($width, $height);
|
||||||
|
@ -255,7 +257,6 @@ class ImageFile
|
||||||
default:
|
default:
|
||||||
// TRANS: Exception thrown when trying resize an unknown file type.
|
// TRANS: Exception thrown when trying resize an unknown file type.
|
||||||
throw new Exception(_('Unknown file type'));
|
throw new Exception(_('Unknown file type'));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
imagedestroy($image_src);
|
imagedestroy($image_src);
|
||||||
|
|
|
@ -43,6 +43,7 @@ class MediaFile
|
||||||
var $fileurl = null;
|
var $fileurl = null;
|
||||||
var $short_fileurl = null;
|
var $short_fileurl = null;
|
||||||
var $mimetype = null;
|
var $mimetype = null;
|
||||||
|
var $thumbnailRecord = null;
|
||||||
|
|
||||||
function __construct(Profile $scoped, $filename = null, $mimetype = null)
|
function __construct(Profile $scoped, $filename = null, $mimetype = null)
|
||||||
{
|
{
|
||||||
|
@ -51,7 +52,12 @@ class MediaFile
|
||||||
$this->filename = $filename;
|
$this->filename = $filename;
|
||||||
$this->mimetype = $mimetype;
|
$this->mimetype = $mimetype;
|
||||||
$this->fileRecord = $this->storeFile();
|
$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',
|
$this->fileurl = common_local_url('attachment',
|
||||||
array('attachment' => $this->fileRecord->id));
|
array('attachment' => $this->fileRecord->id));
|
||||||
|
@ -68,6 +74,11 @@ class MediaFile
|
||||||
common_local_url('file', array('notice' => $notice->id)));
|
common_local_url('file', array('notice' => $notice->id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getPath()
|
||||||
|
{
|
||||||
|
return File::path($this->filename);
|
||||||
|
}
|
||||||
|
|
||||||
function shortUrl()
|
function shortUrl()
|
||||||
{
|
{
|
||||||
return $this->short_fileurl;
|
return $this->short_fileurl;
|
||||||
|
@ -108,16 +119,30 @@ class MediaFile
|
||||||
*/
|
*/
|
||||||
function storeThumbnail()
|
function storeThumbnail()
|
||||||
{
|
{
|
||||||
if (substr($this->mimetype, 0, strlen('image/')) != 'image/') {
|
$imgPath = null;
|
||||||
// @fixme video thumbs would be nice!
|
$media = common_get_mime_media($this->mimetype);
|
||||||
return null;
|
|
||||||
|
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 {
|
try {
|
||||||
$image = new ImageFile($this->fileRecord->id,
|
$image = new ImageFile($this->fileRecord->id, $imgPath);
|
||||||
File::path($this->filename));
|
} catch (UnsupportedMediaException $e) {
|
||||||
} catch (Exception $e) {
|
// Avoid deleting the original
|
||||||
// Unsupported image type.
|
if ($image->getPath() != $this->getPath()) {
|
||||||
return null;
|
$image->unlink();
|
||||||
|
}
|
||||||
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
$outname = File::filename($this->scoped, 'thumb-' . $this->filename, $this->mimetype);
|
$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);
|
list($width, $height) = $this->scaleToFit($image->width, $image->height, $maxWidth, $maxHeight);
|
||||||
|
|
||||||
$image->resizeTo($outpath, $width, $height);
|
$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),
|
File::url($outname),
|
||||||
$width,
|
$width,
|
||||||
$height);
|
$height);
|
||||||
|
@ -189,40 +219,33 @@ class MediaFile
|
||||||
// TRANS: Client exception thrown when an uploaded file is larger than set in php.ini.
|
// TRANS: Client exception thrown when an uploaded file is larger than set in php.ini.
|
||||||
throw new ClientException(_('The uploaded file exceeds the ' .
|
throw new ClientException(_('The uploaded file exceeds the ' .
|
||||||
'upload_max_filesize directive in php.ini.'));
|
'upload_max_filesize directive in php.ini.'));
|
||||||
return;
|
|
||||||
case UPLOAD_ERR_FORM_SIZE:
|
case UPLOAD_ERR_FORM_SIZE:
|
||||||
throw new ClientException(
|
throw new ClientException(
|
||||||
// TRANS: Client exception.
|
// TRANS: Client exception.
|
||||||
_('The uploaded file exceeds the MAX_FILE_SIZE directive' .
|
_('The uploaded file exceeds the MAX_FILE_SIZE directive' .
|
||||||
' that was specified in the HTML form.'));
|
' that was specified in the HTML form.'));
|
||||||
return;
|
|
||||||
case UPLOAD_ERR_PARTIAL:
|
case UPLOAD_ERR_PARTIAL:
|
||||||
@unlink($_FILES[$param]['tmp_name']);
|
@unlink($_FILES[$param]['tmp_name']);
|
||||||
// TRANS: Client exception.
|
// TRANS: Client exception.
|
||||||
throw new ClientException(_('The uploaded file was only' .
|
throw new ClientException(_('The uploaded file was only' .
|
||||||
' partially uploaded.'));
|
' partially uploaded.'));
|
||||||
return;
|
|
||||||
case UPLOAD_ERR_NO_FILE:
|
case UPLOAD_ERR_NO_FILE:
|
||||||
// No file; probably just a non-AJAX submission.
|
// No file; probably just a non-AJAX submission.
|
||||||
return;
|
return;
|
||||||
case UPLOAD_ERR_NO_TMP_DIR:
|
case UPLOAD_ERR_NO_TMP_DIR:
|
||||||
// TRANS: Client exception thrown when a temporary folder is not present to store a file upload.
|
// TRANS: Client exception thrown when a temporary folder is not present to store a file upload.
|
||||||
throw new ClientException(_('Missing a temporary folder.'));
|
throw new ClientException(_('Missing a temporary folder.'));
|
||||||
return;
|
|
||||||
case UPLOAD_ERR_CANT_WRITE:
|
case UPLOAD_ERR_CANT_WRITE:
|
||||||
// TRANS: Client exception thrown when writing to disk is not possible during a file upload operation.
|
// 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.'));
|
throw new ClientException(_('Failed to write file to disk.'));
|
||||||
return;
|
|
||||||
case UPLOAD_ERR_EXTENSION:
|
case UPLOAD_ERR_EXTENSION:
|
||||||
// TRANS: Client exception thrown when a file upload operation has been stopped by an extension.
|
// TRANS: Client exception thrown when a file upload operation has been stopped by an extension.
|
||||||
throw new ClientException(_('File upload stopped by extension.'));
|
throw new ClientException(_('File upload stopped by extension.'));
|
||||||
return;
|
|
||||||
default:
|
default:
|
||||||
common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
|
common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
|
||||||
$_FILES[$param]['error']);
|
$_FILES[$param]['error']);
|
||||||
// TRANS: Client exception thrown when a file upload operation has failed with an unknown reason.
|
// TRANS: Client exception thrown when a file upload operation has failed with an unknown reason.
|
||||||
throw new ClientException(_('System error uploading file.'));
|
throw new ClientException(_('System error uploading file.'));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Throws exception if additional size does not respect quota
|
// Throws exception if additional size does not respect quota
|
||||||
|
|
39
lib/unsupportedmediaexception.php
Normal file
39
lib/unsupportedmediaexception.php
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* GNU social - a federating social network
|
||||||
|
*
|
||||||
|
* class for an exception when a File is of unsupported media type
|
||||||
|
*
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* LICENCE: This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* @category Exception
|
||||||
|
* @package GNUsocial
|
||||||
|
* @author Mikael Nordfeldth <mmn@hethane.se>
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
}
|
90
plugins/VideoThumbnails/VideoThumbnailsPlugin.php
Normal file
90
plugins/VideoThumbnails/VideoThumbnailsPlugin.php
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* GNU social - a federating social network
|
||||||
|
*
|
||||||
|
* Plugin to make thumbnails of video files with ffmpeg
|
||||||
|
*
|
||||||
|
* PHP version 5
|
||||||
|
*
|
||||||
|
* LICENCE: This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* @category Plugin
|
||||||
|
* @package GNUsocial
|
||||||
|
* @author Mikael Nordfeldth <mmn@hethane.se>
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user