Merge remote-tracking branch 'upstream/nightly' into nightly

This commit is contained in:
www-data 2016-07-27 12:38:19 +02:00
commit c946ddc275
6 changed files with 68 additions and 23 deletions

View File

@ -98,6 +98,7 @@ class File_thumbnail extends Managed_DataObject
if ($notNullUrl) { if ($notNullUrl) {
$thumb->whereAdd('url IS NOT NULL'); $thumb->whereAdd('url IS NOT NULL');
} }
$thumb->orderBy('modified ASC'); // the first created, a somewhat ugly hack
$thumb->limit(1); $thumb->limit(1);
if (!$thumb->find(true)) { if (!$thumb->find(true)) {
throw new NoResultException($thumb); throw new NoResultException($thumb);
@ -272,6 +273,10 @@ class File_thumbnail extends Managed_DataObject
return File::getByID($this->file_id); return File::getByID($this->file_id);
} }
public function getFileId()
{
return $this->file_id;
}
static public function hashurl($url) static public function hashurl($url)
{ {

View File

@ -174,7 +174,7 @@ class AttachmentListItem extends Widget
default: default:
unset($thumb); // there's no need carrying this along unset($thumb); // there's no need carrying this along
switch ($this->attachment->mimetype) { switch (common_bare_mime($this->attachment->mimetype)) {
case 'text/plain': case 'text/plain':
$this->element('div', ['class'=>'e-content plaintext'], file_get_contents($this->attachment->getPath())); $this->element('div', ['class'=>'e-content plaintext'], file_get_contents($this->attachment->getPath()));
break; break;

View File

@ -146,15 +146,16 @@ class ImageFile
} }
if (!file_exists($imgPath)) { if (!file_exists($imgPath)) {
throw new ServerException(sprintf('Image not available locally: %s', $imgPath)); throw new FileNotFoundException($imgPath);
} }
try { try {
$image = new ImageFile($file->getID(), $imgPath); $image = new ImageFile($file->getID(), $imgPath);
} catch (UnsupportedMediaException $e) { } catch (Exception $e) {
// Avoid deleting the original // Avoid deleting the original
try { try {
if ($imgPath !== $file->getPath()) { if (strlen($imgPath) > 0 && $imgPath !== $file->getPath()) {
common_debug(__METHOD__.': Deleting temporary file that was created as image file thumbnail source: '._ve($imgPath));
@unlink($imgPath); @unlink($imgPath);
} }
} catch (FileNotFoundException $e) { } catch (FileNotFoundException $e) {
@ -162,6 +163,7 @@ class ImageFile
// doesn't exist anyway, so it's safe to delete $imgPath // doesn't exist anyway, so it's safe to delete $imgPath
@unlink($imgPath); @unlink($imgPath);
} }
common_debug(sprintf('Exception caught when creating ImageFile for File id==%s and imgPath==', _ve($file->id), _ve($imgPath)));
throw $e; throw $e;
} }
return $image; return $image;
@ -584,7 +586,7 @@ class ImageFile
list($width, $height, $x, $y, $w, $h) = $this->scaleToFit($width, $height, $crop); list($width, $height, $x, $y, $w, $h) = $this->scaleToFit($width, $height, $crop);
$thumb = File_thumbnail::pkeyGet(array( $thumb = File_thumbnail::pkeyGet(array(
'file_id'=> $this->fileRecord->id, 'file_id'=> $this->fileRecord->getID(),
'width' => $width, 'width' => $width,
'height' => $height, 'height' => $height,
)); ));
@ -594,7 +596,7 @@ class ImageFile
$filename = $this->fileRecord->filehash ?: $this->filename; // Remote files don't have $this->filehash $filename = $this->fileRecord->filehash ?: $this->filename; // Remote files don't have $this->filehash
$extension = File::guessMimeExtension($this->mimetype); $extension = File::guessMimeExtension($this->mimetype);
$outname = "thumb-{$this->fileRecord->id}-{$width}x{$height}-{$filename}." . $extension; $outname = "thumb-{$this->fileRecord->getID()}-{$width}x{$height}-{$filename}." . $extension;
$outpath = File_thumbnail::path($outname); $outpath = File_thumbnail::path($outname);
// The boundary box for our resizing // The boundary box for our resizing
@ -612,7 +614,7 @@ class ImageFile
throw new ServerException('Bad thumbnail size parameters.'); throw new ServerException('Bad thumbnail size parameters.');
} }
common_debug(sprintf('Generating a thumbnail of File id==%u of size %ux%u', $this->fileRecord->id, $width, $height)); common_debug(sprintf('Generating a thumbnail of File id==%u of size %ux%u', $this->fileRecord->getID(), $width, $height));
// Perform resize and store into file // Perform resize and store into file
$this->resizeTo($outpath, $box); $this->resizeTo($outpath, $box);

View File

@ -102,7 +102,7 @@ class OembedPlugin extends Plugin
array(), array(),
array('format'=>'json', 'url'=> array('format'=>'json', 'url'=>
common_local_url('attachment', common_local_url('attachment',
array('attachment' => $action->attachment->id)))), array('attachment' => $action->attachment->getID())))),
'title'=>'oEmbed'),null); 'title'=>'oEmbed'),null);
$action->element('link',array('rel'=>'alternate', $action->element('link',array('rel'=>'alternate',
'type'=>'text/xml+oembed', 'type'=>'text/xml+oembed',
@ -111,7 +111,7 @@ class OembedPlugin extends Plugin
array(), array(),
array('format'=>'xml','url'=> array('format'=>'xml','url'=>
common_local_url('attachment', common_local_url('attachment',
array('attachment' => $action->attachment->id)))), array('attachment' => $action->attachment->getID())))),
'title'=>'oEmbed'),null); 'title'=>'oEmbed'),null);
break; break;
case 'shownotice': case 'shownotice':
@ -159,9 +159,9 @@ class OembedPlugin extends Plugin
*/ */
public function onEndFileSaveNew(File $file) public function onEndFileSaveNew(File $file)
{ {
$fo = File_oembed::getKV('file_id', $file->id); $fo = File_oembed::getKV('file_id', $file->getID());
if ($fo instanceof File_oembed) { if ($fo instanceof File_oembed) {
common_log(LOG_WARNING, "Strangely, a File_oembed object exists for new file {$file->id}", __FILE__); common_log(LOG_WARNING, "Strangely, a File_oembed object exists for new file {$file->getID()}", __FILE__);
return true; return true;
} }
@ -178,14 +178,14 @@ class OembedPlugin extends Plugin
return true; return true;
} }
File_oembed::saveNew($oembed_data, $file->id); File_oembed::saveNew($oembed_data, $file->getID());
} }
return true; return true;
} }
public function onEndShowAttachmentLink(HTMLOutputter $out, File $file) public function onEndShowAttachmentLink(HTMLOutputter $out, File $file)
{ {
$oembed = File_oembed::getKV('file_id', $file->id); $oembed = File_oembed::getKV('file_id', $file->getID());
if (empty($oembed->author_name) && empty($oembed->provider)) { if (empty($oembed->author_name) && empty($oembed->provider)) {
return true; return true;
} }
@ -217,7 +217,7 @@ class OembedPlugin extends Plugin
{ {
// Never treat generic HTML links as an enclosure type! // Never treat generic HTML links as an enclosure type!
// But if we have oEmbed info, we'll consider it golden. // But if we have oEmbed info, we'll consider it golden.
$oembed = File_oembed::getKV('file_id', $file->id); $oembed = File_oembed::getKV('file_id', $file->getID());
if (!$oembed instanceof File_oembed || !in_array($oembed->type, array('photo', 'video'))) { if (!$oembed instanceof File_oembed || !in_array($oembed->type, array('photo', 'video'))) {
return true; return true;
} }
@ -377,16 +377,36 @@ class OembedPlugin extends Plugin
protected function storeRemoteFileThumbnail(File_thumbnail $thumbnail) protected function storeRemoteFileThumbnail(File_thumbnail $thumbnail)
{ {
if (!empty($thumbnail->filename) && file_exists($thumbnail->getPath())) { if (!empty($thumbnail->filename) && file_exists($thumbnail->getPath())) {
throw new AlreadyFulfilledException(sprintf('A thumbnail seems to already exist for remote file with id==%u', $thumbnail->file_id)); throw new AlreadyFulfilledException(sprintf('A thumbnail seems to already exist for remote file with id==%u', $thumbnail->getFileId()));
} }
$url = $thumbnail->getUrl(); $remoteUrl = $thumbnail->getUrl();
$this->checkWhitelist($url); $this->checkWhitelist($remoteUrl);
// First we download the file to memory and test whether it's actually an image file $http = new HTTPClient();
// First see if it's too large for us
common_debug(__METHOD__ . ': '.sprintf('Performing HEAD request for remote file id==%u to avoid unnecessarily downloading too large files. URL: %s', $thumbnail->getFileId(), $remoteUrl));
$head = $http->head($remoteUrl);
$remoteUrl = $head->getEffectiveUrl(); // to avoid going through redirects again
$headers = $head->getHeader();
$filesize = isset($headers['content-length']) ? $headers['content-length'] : null;
// FIXME: I just copied some checks from StoreRemoteMedia, maybe we should have other checks for thumbnails? Or at least embed into some class somewhere.
if (empty($filesize)) {
// file size not specified on remote server
common_debug(sprintf('%s: Ignoring remote thumbnail because we did not get a content length for thumbnail for file id==%u', __CLASS__, $thumbnail->getFileId()));
return true;
} elseif ($filesize > common_config('attachments', 'file_quota')) {
// file too big according to site configuration
common_debug(sprintf('%s: Skip downloading remote thumbnail because content length (%u) is larger than file_quota (%u) for file id==%u', __CLASS__, intval($filesize), common_config('attachments', 'file_quota'), $thumbnail->getFileId()));
return true;
}
// Then we download the file to memory and test whether it's actually an image file
// FIXME: To support remote video/whatever files, this needs reworking. // FIXME: To support remote video/whatever files, this needs reworking.
common_debug(sprintf('Downloading remote thumbnail for file id==%u with thumbnail URL: %s', $thumbnail->file_id, $url)); common_debug(sprintf('Downloading remote thumbnail for file id==%u (should be size %u) with effective URL: %s', $thumbnail->getFileId(), $filesize, _ve($remoteUrl)));
$imgData = HTTPClient::quickGet($url); $imgData = HTTPClient::quickGet($remoteUrl);
$info = @getimagesizefromstring($imgData); $info = @getimagesizefromstring($imgData);
if ($info === false) { if ($info === false) {
throw new UnsupportedMediaException(_('Remote file format was not identified as an image.'), $url); throw new UnsupportedMediaException(_('Remote file format was not identified as an image.'), $url);

View File

@ -91,7 +91,7 @@ class StoreRemoteMediaPlugin extends Plugin
$http = new HTTPClient(); $http = new HTTPClient();
common_debug(sprintf('Performing HEAD request for remote file id==%u to avoid unnecessarily downloading too large files. URL: %s', $file->getID(), $remoteUrl)); common_debug(sprintf('Performing HEAD request for remote file id==%u to avoid unnecessarily downloading too large files. URL: %s', $file->getID(), $remoteUrl));
$head = $http->head($remoteUrl); $head = $http->head($remoteUrl);
$remoteUrl = $head->effectiveUrl; // to avoid going through redirects again $remoteUrl = $head->getEffectiveUrl(); // to avoid going through redirects again
if (!$this->checkBlackList($remoteUrl)) { if (!$this->checkBlackList($remoteUrl)) {
common_log(LOG_WARN, sprintf('%s: Non-blacklisted URL %s redirected to blacklisted URL %s', __CLASS__, $file->getUrl(), $remoteUrl)); common_log(LOG_WARN, sprintf('%s: Non-blacklisted URL %s redirected to blacklisted URL %s', __CLASS__, $file->getUrl(), $remoteUrl));
return true; return true;
@ -120,7 +120,7 @@ class StoreRemoteMediaPlugin extends Plugin
common_debug(sprintf('Downloading remote file id==%u (should be size %u) with effective URL: %s', $file->getID(), $filesize, _ve($remoteUrl))); common_debug(sprintf('Downloading remote file id==%u (should be size %u) with effective URL: %s', $file->getID(), $filesize, _ve($remoteUrl)));
$imgData = HTTPClient::quickGet($remoteUrl); $imgData = HTTPClient::quickGet($remoteUrl);
} catch (HTTP_Request2_ConnectionException $e) { } catch (HTTP_Request2_ConnectionException $e) {
common_log(LOG_ERR, __CLASS__.': quickGet on URL: '._ve($file->getUrl()).' threw exception: '.$e->getMessage()); common_log(LOG_ERR, __CLASS__.': '._ve(get_class($e)).' on URL: '._ve($file->getUrl()).' threw exception: '.$e->getMessage());
return true; return true;
} }
$info = @getimagesizefromstring($imgData); $info = @getimagesizefromstring($imgData);

View File

@ -55,6 +55,22 @@ class VideoThumbnailsPlugin extends Plugin
return true; return true;
} }
try {
// Exception thrown if no thumbnail found
$thumb = File_thumbnail::byFile($file, false);
$imgPath = $thumb->getPath();
// If getPath didn't throw an exception, we have a working locally stored thumbnail
return false;
} catch (NoResultException $e) {
// Alright, no thumbnail found, so let's create one.
} catch (InvalidFilenameException $e) {
// I guess this means $thumb->filename is null? Shouldn't happen because $file->filename is not null, so delete it
$thumb->delete();
} catch (FileNotFoundException $e) {
// Thumb file was not found, let's delete it.
$thumb->delete();
}
// Let's save our frame to a temporary file. If we fail, remove it. // Let's save our frame to a temporary file. If we fail, remove it.
$tmp_imgPath = tempnam(sys_get_temp_dir(), 'socialthumb-'); $tmp_imgPath = tempnam(sys_get_temp_dir(), 'socialthumb-');
@ -67,7 +83,9 @@ class VideoThumbnailsPlugin extends Plugin
common_log(LOG_ERR, 'Neither ffmpeg nor avconv was found in your PATH. Cannot create video thumbnail.'); common_log(LOG_ERR, 'Neither ffmpeg nor avconv was found in your PATH. Cannot create video thumbnail.');
return true; return true;
} }
$result = exec($cmd.' -y -i '.escapeshellarg($file->getPath()).' -vcodec mjpeg -vframes 1 -f image2 -an '.escapeshellarg($tmp_imgPath)); $fullcmd = $cmd.' -y -i '.escapeshellarg($file->getPath()).' -vcodec mjpeg -vframes 1 -f image2 -an '.escapeshellarg($tmp_imgPath);
common_debug(__METHOD__ . ' executing: '._ve($fullcmd));
$result = exec($fullcmd);
if (!getimagesize($tmp_imgPath)) { if (!getimagesize($tmp_imgPath)) {
common_debug('exec of "avconv" produced a bad/nonexisting image it seems'); common_debug('exec of "avconv" produced a bad/nonexisting image it seems');