[Embed] Apply encoding and increased type strictness patches from StoreRemoteMedia

This commit is contained in:
Diogo Peralta Cordeiro 2021-04-12 03:30:35 +01:00
parent 5b23781e68
commit f088a3d54f

View File

@ -124,10 +124,10 @@ class EmbedPlugin extends Plugin
* @param $metadata stdClass class representing the metadata * @param $metadata stdClass class representing the metadata
* @return bool true if successful, the exception object if it isn't. * @return bool true if successful, the exception object if it isn't.
*/ */
public function onGetRemoteUrlMetadataFromDom($url, DOMDocument $dom, stdClass &$metadata) public function onGetRemoteUrlMetadataFromDom(string $url, DOMDocument $dom, stdClass &$metadata)
{ {
try { try {
common_log(LOG_INFO, "Trying to find Embed data for {$url} with 'oscarotero/Embed'"); common_log(LOG_INFO, "Trying to find Embed data for $url with 'oscarotero/Embed'");
$info = Embed::create($url); $info = Embed::create($url);
$metadata->version = '1.0'; // Yes. $metadata->version = '1.0'; // Yes.
@ -149,7 +149,7 @@ class EmbedPlugin extends Plugin
$metadata->thumbnail_url = $info->image; $metadata->thumbnail_url = $info->image;
} }
} catch (Exception $e) { } catch (Exception $e) {
common_log(LOG_INFO, "Failed to find Embed data for {$url} with 'oscarotero/Embed'" . common_log(LOG_INFO, "Failed to find Embed data for $url with 'oscarotero/Embed'" .
", got exception: " . get_class($e)); ", got exception: " . get_class($e));
} }
@ -160,7 +160,7 @@ class EmbedPlugin extends Plugin
if ($metadata->thumbnail_url[0] == '/') { if ($metadata->thumbnail_url[0] == '/') {
$thumbnail_url_parsed = parse_url($metadata->url); $thumbnail_url_parsed = parse_url($metadata->url);
$metadata->thumbnail_url = "{$thumbnail_url_parsed['scheme']}://". $metadata->thumbnail_url = "{$thumbnail_url_parsed['scheme']}://".
"{$thumbnail_url_parsed['host']}{$metadata->thumbnail_url}"; "{$thumbnail_url_parsed['host']}$metadata->thumbnail_url";
} }
// some wordpress opengraph implementations sometimes return a white blank image // some wordpress opengraph implementations sometimes return a white blank image
@ -200,7 +200,7 @@ class EmbedPlugin extends Plugin
'link', 'link',
[ [
'rel' =>'alternate', 'rel' =>'alternate',
'type' => "application/{$format}+oembed", 'type' => "application/$format+oembed",
'href' => common_local_url('oembed', [], ['format' => $format, 'url' => $url]), 'href' => common_local_url('oembed', [], ['format' => $format, 'url' => $url]),
'title' => 'oEmbed' 'title' => 'oEmbed'
] ]
@ -239,7 +239,7 @@ class EmbedPlugin extends Plugin
try { try {
$embed_data = File_embed::getEmbed($file->url); $embed_data = File_embed::getEmbed($file->url);
if ($embed_data === false) { if ($embed_data === false) {
throw new Exception("Did not get Embed data from URL {$file->url}"); throw new Exception("Did not get Embed data from URL $file->url");
} }
$file->setTitle($embed_data->title); $file->setTitle($embed_data->title);
} catch (Exception $e) { } catch (Exception $e) {
@ -281,6 +281,7 @@ class EmbedPlugin extends Plugin
} }
} }
$out->elementEnd('div'); $out->elementEnd('div');
return false;
} }
public function onFileEnclosureMetadata(File $file, &$enclosure) public function onFileEnclosureMetadata(File $file, &$enclosure)
@ -397,7 +398,7 @@ class EmbedPlugin extends Plugin
* object thumbnails. * object thumbnails.
* *
* @param $file File the file of the created thumbnail * @param $file File the file of the created thumbnail
* @param &$imgPath string = the path to the created thumbnail * @param &$imgPath null|string = the path to the created thumbnail (output)
* @param $media string = media type * @param $media string = media type
* @return bool true if it succeeds (including non-action * @return bool true if it succeeds (including non-action
* states where it isn't oEmbed data, so it doesn't mess up the event handle * states where it isn't oEmbed data, so it doesn't mess up the event handle
@ -406,7 +407,7 @@ class EmbedPlugin extends Plugin
* @throws NoResultException * @throws NoResultException
* @throws ServerException * @throws ServerException
*/ */
public function onCreateFileImageThumbnailSource(File $file, &$imgPath, string $media): bool public function onCreateFileImageThumbnailSource(File $file, ?string &$imgPath, string $media): bool
{ {
// If we are on a private node, we won't do any remote calls (just as a precaution until // If we are on a private node, we won't do any remote calls (just as a precaution until
// we can configure this from config.php for the private nodes) // we can configure this from config.php for the private nodes)
@ -496,7 +497,7 @@ class EmbedPlugin extends Plugin
$headers = $head->getHeader(); $headers = $head->getHeader();
$headers = array_change_key_case($headers, CASE_LOWER); $headers = array_change_key_case($headers, CASE_LOWER);
} }
return isset($headers['content-length']) ? $headers['content-length'] : false; return $headers['content-length'] ?? false;
} catch (Exception $err) { } catch (Exception $err) {
common_log(LOG_ERR, __CLASS__.': getRemoteFileSize on URL : '._ve($url). common_log(LOG_ERR, __CLASS__.': getRemoteFileSize on URL : '._ve($url).
' threw exception: '.$err->getMessage()); ' threw exception: '.$err->getMessage());
@ -554,6 +555,13 @@ class EmbedPlugin extends Plugin
$original_name = HTTPClient::get_filename($url, $headers); $original_name = HTTPClient::get_filename($url, $headers);
} }
$filename = MediaFile::encodeFilename($original_name ?? _m('Untitled attachment'), $filehash); $filename = MediaFile::encodeFilename($original_name ?? _m('Untitled attachment'), $filehash);
} catch (Exception $err) {
common_log(LOG_ERR, "Went to write a thumbnail to disk in StoreRemoteMediaPlugin::storeRemoteThumbnail " .
"but encountered error: $err");
throw $err;
}
try {
$fullpath = File_thumbnail::path($filename); $fullpath = File_thumbnail::path($filename);
// Write the file to disk. Throw Exception on failure // Write the file to disk. Throw Exception on failure
if (!file_exists($fullpath)) { if (!file_exists($fullpath)) {
@ -571,6 +579,7 @@ class EmbedPlugin extends Plugin
// If the image is not of the desired size, resize it // If the image is not of the desired size, resize it
if ($this->crop && ($info[0] > $this->thumbnail_width || $info[1] > $this->thumbnail_height)) { if ($this->crop && ($info[0] > $this->thumbnail_width || $info[1] > $this->thumbnail_height)) {
try {
// Temporary object, not stored in DB // Temporary object, not stored in DB
$img = new ImageFile(-1, $fullpath); $img = new ImageFile(-1, $fullpath);
list($width, $height, $x, $y, $w, $h) = $img->scaleToFit($this->thumbnail_width, $this->thumbnail_height, $this->crop); list($width, $height, $x, $y, $w, $h) = $img->scaleToFit($this->thumbnail_width, $this->thumbnail_height, $this->crop);
@ -585,6 +594,10 @@ class EmbedPlugin extends Plugin
$width = $box['width']; $width = $box['width'];
$height = $box['height']; $height = $box['height'];
$img->resizeTo($fullpath, $box); $img->resizeTo($fullpath, $box);
} catch (\Intervention\Image\Exception\NotReadableException $e) {
common_log(LOG_ERR, "StoreRemoteMediaPlugin::storeRemoteThumbnail was unable to decode image with Intervention: $e");
// No need to interrupt processing
}
} }
} else { } else {
throw new AlreadyFulfilledException('A thumbnail seems to already exist for remote file' . throw new AlreadyFulfilledException('A thumbnail seems to already exist for remote file' .
@ -594,7 +607,7 @@ class EmbedPlugin extends Plugin
// Carry on // Carry on
} catch (Exception $err) { } catch (Exception $err) {
common_log(LOG_ERR, "Went to write a thumbnail to disk in EmbedPlugin::storeRemoteThumbnail " . common_log(LOG_ERR, "Went to write a thumbnail to disk in EmbedPlugin::storeRemoteThumbnail " .
"but encountered error: {$err}"); "but encountered error: $err");
throw $err; throw $err;
} finally { } finally {
unset($imgData); unset($imgData);
@ -670,7 +683,7 @@ class EmbedPlugin extends Plugin
} }
} catch (UnsupportedMediaException $e) { } catch (UnsupportedMediaException $e) {
// Couldn't find anything that looks like an image, nothing to do // Couldn't find anything that looks like an image, nothing to do
common_debug("Embed was not able to find an image for URL `{$url}`: " . $e->getMessage()); common_debug("Embed was not able to find an image for URL `$url`: " . $e->getMessage());
return false; return false;
} }
} }