forked from GNUsocial/gnu-social
[Embed] Fix plugin. Only attempt to show an image, if we have one
This commit is contained in:
parent
2adb3c3521
commit
30107de079
@ -51,6 +51,7 @@ use App\Entity\AttachmentThumbnail;
|
|||||||
use App\Util\Common;
|
use App\Util\Common;
|
||||||
use App\Util\Exception\DuplicateFoundException;
|
use App\Util\Exception\DuplicateFoundException;
|
||||||
use App\Util\Exception\NotFoundException;
|
use App\Util\Exception\NotFoundException;
|
||||||
|
use App\Util\Formatting;
|
||||||
use App\Util\TemporaryFile;
|
use App\Util\TemporaryFile;
|
||||||
use Embed\Embed as LibEmbed;
|
use Embed\Embed as LibEmbed;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
@ -152,28 +153,37 @@ class Embed extends Plugin
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Replace enclosure representation of an attachment with the data from embed
|
* Replace enclosure representation of an attachment with the data from embed
|
||||||
*
|
|
||||||
* @param mixed $enclosure
|
|
||||||
*/
|
*/
|
||||||
public function onFileEnclosureMetadata(Attachment $attachment, &$enclosure)
|
public function onAttachmentFileInfo(int $attachment_id, ?array &$enclosure)
|
||||||
{
|
{
|
||||||
// Never treat generic HTML links as an enclosure type!
|
|
||||||
// But if we have embed info, we'll consider it golden.
|
|
||||||
try {
|
try {
|
||||||
$embed = DB::findOneBy('attachment_embed', ['attachment_id' => $attachment->getId()]);
|
$embed = DB::findOneBy('attachment_embed', ['attachment_id' => $attachment_id]);
|
||||||
} catch (NotFoundException) {
|
} catch (NotFoundException) {
|
||||||
return Event::next;
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (['mimetype', 'url', 'title', 'modified', 'width', 'height'] as $key) {
|
// We know about this attachment, so we 'own' it, but know
|
||||||
if (isset($embed->{$key}) && !empty($embed->{$key})) {
|
// that it doesn't have an image
|
||||||
$enclosure->{$key} = $embed->{$key};
|
if (!$embed->isImage()) {
|
||||||
}
|
$enclosure = null;
|
||||||
|
return Event::stop;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
$enclosure = [
|
||||||
|
'filepath' => $embed->getFilepath(),
|
||||||
|
'mimetype' => $embed->getMimetype(),
|
||||||
|
'title' => $embed->getTitle(),
|
||||||
|
'width' => $embed->getWidth(),
|
||||||
|
'height' => $embed->getHeight(),
|
||||||
|
'url' => $embed->getUrl(),
|
||||||
|
];
|
||||||
|
|
||||||
|
return Event::stop;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Placeholder */
|
/**
|
||||||
|
* Show this attachment enhanced with the corresponing Embed data, if available
|
||||||
|
*/
|
||||||
public function onShowAttachment(Attachment $attachment, array &$res)
|
public function onShowAttachment(Attachment $attachment, array &$res)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@ -186,18 +196,23 @@ class Embed extends Plugin
|
|||||||
return Event::next;
|
return Event::next;
|
||||||
}
|
}
|
||||||
if (is_null($embed) && empty($embed->getAuthorName()) && empty($embed->getProvider())) {
|
if (is_null($embed) && empty($embed->getAuthorName()) && empty($embed->getProvider())) {
|
||||||
|
Log::debug('Embed doesn\'t have a representation for the attachment #' . $attachment->getId());
|
||||||
return Event::next;
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
||||||
$thumbnail = AttachmentThumbnail::getOrCreate(attachment: $attachment, width: $width, height: $height, crop: $smart_crop);
|
$width = Common::config('thumbnail', 'width');
|
||||||
$attributes = $thumbnail->getHTMLAttributes(['class' => 'u-photo embed']);
|
$height = Common::config('thumbnail', 'height');
|
||||||
|
$smart_crop = Common::config('thumbnail', 'smart_crop');
|
||||||
|
$attributes = $embed->getImageHTMLAttributes(['class' => 'u-photo embed']);
|
||||||
|
|
||||||
$res[] = Formatting::twigRender(<<<END
|
$res[] = Formatting::twigRender(<<<END
|
||||||
<article class="h-entry embed">
|
<article class="h-entry embed">
|
||||||
<header>
|
<header>
|
||||||
<img class="u-photo embed" width="{{attributes['width']}}" height="{{attributes['height']}}" src="{{attributes['src']}}" />
|
{% if attributes != false %}
|
||||||
|
<img class="u-photo embed" width="{{attributes['width']}}" height="{{attributes['height']}}" src="{{attributes['src']}}" />
|
||||||
|
{% endif %}
|
||||||
<h5 class="p-name embed">
|
<h5 class="p-name embed">
|
||||||
<a class="u-url" href="{{attachment.getUrl()}}">{{embed.getTitle() | escape}}</a>
|
<a class="u-url" href="{{embed.getUrl()}}">{{embed.getTitle() | escape}}</a>
|
||||||
</h5>
|
</h5>
|
||||||
<div class="p-author embed">
|
<div class="p-author embed">
|
||||||
{% if embed.getAuthorName() is not null %}
|
{% if embed.getAuthorName() is not null %}
|
||||||
@ -224,7 +239,7 @@ class Embed extends Plugin
|
|||||||
{{ embed.getHtml() | escape }}
|
{{ embed.getHtml() | escape }}
|
||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
END, ['embed' => $embed, 'thumbnail' => $thumbnail, 'attributes' => $attributes]);
|
END, ['embed' => $embed, 'attributes' => $attributes]);
|
||||||
|
|
||||||
return Event::stop;
|
return Event::stop;
|
||||||
}
|
}
|
||||||
@ -319,23 +334,20 @@ END, ['embed' => $embed, 'thumbnail' => $thumbnail, 'attributes' => $attributes]
|
|||||||
$file = new TemporaryFile();
|
$file = new TemporaryFile();
|
||||||
$file->write($imgData);
|
$file->write($imgData);
|
||||||
|
|
||||||
$mimetype = $headers['content-type'][0];
|
|
||||||
Event::handle('AttachmentValidation', [&$file, &$mimetype]);
|
|
||||||
|
|
||||||
Event::handle('HashFile', [$file->getPathname(), &$hash]);
|
Event::handle('HashFile', [$file->getPathname(), &$hash]);
|
||||||
$filename = Common::config('attachments', 'dir') . "embed/{$hash}";
|
$filepath = Common::config('storage', 'dir') . "embed/{$hash}" . Common::config('thumbnail', 'extension');
|
||||||
$file->commit($filename);
|
$width = Common::config('thumbnail', 'width');
|
||||||
|
$height = Common::config('thumbnail', 'height');
|
||||||
|
$smart_crop = Common::config('thumbnail', 'smart_crop');
|
||||||
|
Event::handle('ResizeImagePath', [$file->getPathname(), $filepath, $width, $height, $smart_crop, &$mimetype]);
|
||||||
|
|
||||||
unset($file);
|
unset($file);
|
||||||
|
|
||||||
if (array_key_exists('content-disposition', $headers) && preg_match('/^.+; filename="(.+?)"$/', $headers['content-disposition'][0], $matches) === 1) {
|
if (array_key_exists('content-disposition', $headers) && preg_match('/^.+; filename="(.+?)"$/', $headers['content-disposition'][0], $matches) === 1) {
|
||||||
$original_name = $matches[1];
|
$original_name = $matches[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
$info = getimagesize($filename);
|
return [$filepath, $width, $height, $original_name ?? null, $mimetype];
|
||||||
$width = $info[0];
|
|
||||||
$height = $info[1];
|
|
||||||
|
|
||||||
return [$filename, $width, $height, $original_name ?? null, $mimetype];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -382,7 +394,7 @@ END, ['embed' => $embed, 'thumbnail' => $thumbnail, 'attributes' => $attributes]
|
|||||||
try {
|
try {
|
||||||
$imgData = HTTPClient::get($url)->getContent();
|
$imgData = HTTPClient::get($url)->getContent();
|
||||||
if (isset($imgData)) {
|
if (isset($imgData)) {
|
||||||
[$filename, $width, $height, $original_name, $mimetype] = $this->validateAndWriteImage($imgData, $url, $headers);
|
[$filepath, $width, $height, $original_name, $mimetype] = $this->validateAndWriteImage($imgData, $url, $headers);
|
||||||
} else {
|
} else {
|
||||||
throw new UnsupportedMediaException(_m('HTTPClient returned an empty result'));
|
throw new UnsupportedMediaException(_m('HTTPClient returned an empty result'));
|
||||||
}
|
}
|
||||||
@ -394,10 +406,9 @@ END, ['embed' => $embed, 'thumbnail' => $thumbnail, 'attributes' => $attributes]
|
|||||||
}
|
}
|
||||||
|
|
||||||
DB::persist(AttachmentThumbnail::create(['attachment_id' => $attachment->getId(), 'width' => $width, 'height' => $height]));
|
DB::persist(AttachmentThumbnail::create(['attachment_id' => $attachment->getId(), 'width' => $width, 'height' => $height]));
|
||||||
$attachment->setFilename($filename);
|
|
||||||
DB::flush();
|
DB::flush();
|
||||||
|
|
||||||
return [$filename, $width, $height, $original_name, $mimetype];
|
return [$filepath, $width, $height, $original_name, $mimetype];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -435,14 +446,15 @@ END, ['embed' => $embed, 'thumbnail' => $thumbnail, 'attributes' => $attributes]
|
|||||||
if (substr($info->image, 0, 4) === 'data') {
|
if (substr($info->image, 0, 4) === 'data') {
|
||||||
// Inline image
|
// Inline image
|
||||||
$imgData = base64_decode(substr($info->image, stripos($info->image, 'base64,') + 7));
|
$imgData = base64_decode(substr($info->image, stripos($info->image, 'base64,') + 7));
|
||||||
[$filename, $width, $height, $original_name, $mimetype] = $this->validateAndWriteImage($imgData);
|
[$filepath, $width, $height, $original_name, $mimetype] = $this->validateAndWriteImage($imgData);
|
||||||
} else {
|
} else {
|
||||||
$attachment->setRemoteUrl((string) $info->image);
|
$attachment->setRemoteUrl((string) $info->image);
|
||||||
[$filename, $width, $height, $original_name, $mimetype] = $this->storeRemoteThumbnail($attachment);
|
[$filepath, $width, $height, $original_name, $mimetype] = $this->storeRemoteThumbnail($attachment);
|
||||||
}
|
}
|
||||||
$metadata['width'] = $height;
|
$metadata['width'] = $width;
|
||||||
$metadata['height'] = $width;
|
$metadata['height'] = $height;
|
||||||
$metadata['mimetype'] = $mimetype;
|
$metadata['mimetype'] = $mimetype;
|
||||||
|
$metadata['filename'] = Formatting::removePrefix($filepath, Common::config('storage', 'dir'));
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::info("Failed to find Embed data for {$url} with 'oscarotero/Embed', got exception: " . get_class($e));
|
Log::info("Failed to find Embed data for {$url} with 'oscarotero/Embed', got exception: " . get_class($e));
|
||||||
|
@ -33,6 +33,9 @@
|
|||||||
namespace Plugin\Embed\Entity;
|
namespace Plugin\Embed\Entity;
|
||||||
|
|
||||||
use App\Core\Entity;
|
use App\Core\Entity;
|
||||||
|
use App\Core\GSFile;
|
||||||
|
use App\Core\Router\Router;
|
||||||
|
use App\Util\Common;
|
||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,6 +50,7 @@ class AttachmentEmbed extends Entity
|
|||||||
// {{{ Autocode
|
// {{{ Autocode
|
||||||
private int $attachment_id;
|
private int $attachment_id;
|
||||||
private ?string $mimetype;
|
private ?string $mimetype;
|
||||||
|
private ?string $filename;
|
||||||
private ?string $provider;
|
private ?string $provider;
|
||||||
private ?string $provider_url;
|
private ?string $provider_url;
|
||||||
private ?int $width;
|
private ?int $width;
|
||||||
@ -80,6 +84,17 @@ class AttachmentEmbed extends Entity
|
|||||||
return $this->mimetype;
|
return $this->mimetype;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setFilename(?string $filename): self
|
||||||
|
{
|
||||||
|
$this->filename = $filename;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFilename(): ?string
|
||||||
|
{
|
||||||
|
return $this->filename;
|
||||||
|
}
|
||||||
|
|
||||||
public function setProvider(?string $provider): self
|
public function setProvider(?string $provider): self
|
||||||
{
|
{
|
||||||
$this->provider = $provider;
|
$this->provider = $provider;
|
||||||
@ -192,6 +207,38 @@ class AttachmentEmbed extends Entity
|
|||||||
|
|
||||||
// }}} Autocode
|
// }}} Autocode
|
||||||
|
|
||||||
|
public function getAttachmentUrl()
|
||||||
|
{
|
||||||
|
return Router::url('attachment_show', ['id' => $this->getAttachmentId()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isImage()
|
||||||
|
{
|
||||||
|
return isset($this->mimetype) && GSFile::mimetypeMajor($this->mimetype) == 'image';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the HTML attributes for this attachment
|
||||||
|
*/
|
||||||
|
public function getImageHTMLAttributes(array $orig = [], bool $overwrite = true)
|
||||||
|
{
|
||||||
|
if ($this->isImage()) {
|
||||||
|
$attrs = [
|
||||||
|
'height' => $this->getHeight(),
|
||||||
|
'width' => $this->getWidth(),
|
||||||
|
'src' => $this->getAttachmentUrl(),
|
||||||
|
];
|
||||||
|
return $overwrite ? array_merge($orig, $attrs) : array_merge($attrs, $orig);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFilepath()
|
||||||
|
{
|
||||||
|
return Common::config('storage', 'dir') . $this->filename;
|
||||||
|
}
|
||||||
|
|
||||||
public static function schemaDef()
|
public static function schemaDef()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
@ -199,6 +246,7 @@ class AttachmentEmbed extends Entity
|
|||||||
'fields' => [
|
'fields' => [
|
||||||
'attachment_id' => ['type' => 'int', 'not null' => true, 'description' => 'oEmbed for that URL/file'],
|
'attachment_id' => ['type' => 'int', 'not null' => true, 'description' => 'oEmbed for that URL/file'],
|
||||||
'mimetype' => ['type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'],
|
'mimetype' => ['type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'],
|
||||||
|
'filename' => ['type' => 'varchar', 'length' => 191, 'description' => 'file name of resource when available'],
|
||||||
'provider' => ['type' => 'text', 'description' => 'name of this oEmbed provider'],
|
'provider' => ['type' => 'text', 'description' => 'name of this oEmbed provider'],
|
||||||
'provider_url' => ['type' => 'text', 'description' => 'URL of this oEmbed provider'],
|
'provider_url' => ['type' => 'text', 'description' => 'URL of this oEmbed provider'],
|
||||||
'width' => ['type' => 'int', 'description' => 'width of oEmbed resource when available'],
|
'width' => ['type' => 'int', 'description' => 'width of oEmbed resource when available'],
|
||||||
|
@ -55,6 +55,11 @@ class ImageEncoder extends Plugin
|
|||||||
return Event::stop;
|
return Event::stop;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function onResizeImage(Attachment $attachment, AttachmentThumbnail $thumbnail, int $width, int $height, bool $smart_crop): bool
|
||||||
|
{
|
||||||
|
return $this->onResizeImagePath($attachment->getPath(), $thumbnail->getPath(), $width, $height, $smart_crop, $__mimetype);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resizes an image. It will encode the image in the
|
* Resizes an image. It will encode the image in the
|
||||||
* `self::preferredType()` format. This only applies henceforward,
|
* `self::preferredType()` format. This only applies henceforward,
|
||||||
@ -76,33 +81,35 @@ class ImageEncoder extends Plugin
|
|||||||
* @return bool
|
* @return bool
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function onResizeImage(Attachment $attachment, AttachmentThumbnail $thumbnail, int $width, int $height, bool $crop): bool
|
public function onResizeImagePath(string $source, string $destination, int $width, int $height, bool $smart_crop, ?string &$mimetype)
|
||||||
{
|
{
|
||||||
$old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
|
$old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
|
||||||
try {
|
try {
|
||||||
try {
|
try {
|
||||||
// -1 means load all pages, 'sequential' access means decode pixels on demand
|
// -1 means load all pages, 'sequential' access means decode pixels on demand
|
||||||
// $image = Vips\Image::newFromFile(self::getPath($attachment), ['n' => -1, 'access' => 'sequential']);
|
// $image = Vips\Image::newFromFile(self::getPath($attachment), ['n' => -1, 'access' => 'sequential']);
|
||||||
$image = Vips\Image::thumbnail($attachment->getPath(), $width, ['height' => $height]);
|
$image = Vips\Image::thumbnail($source, $width, ['height' => $height]);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::error(__METHOD__ . ' encountered exception: ' . print_r($e, true));
|
Log::error(__METHOD__ . ' encountered exception: ' . print_r($e, true));
|
||||||
// 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(_m('Unknown file type'));
|
throw new Exception(_m('Unknown file type'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($attachment->getPath() === $thumbnail->getPath()) {
|
if ($source === $destination) {
|
||||||
@unlink($thumbnail->getPath());
|
@unlink($destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($crop) {
|
$type = self::preferredType();
|
||||||
|
$mimetype = image_type_to_mime_type($type);
|
||||||
|
|
||||||
|
if ($smart_crop) {
|
||||||
$image = $image->smartcrop($width, $height);
|
$image = $image->smartcrop($width, $height);
|
||||||
}
|
}
|
||||||
$image->writeToFile($thumbnail->getPath());
|
$image->writeToFile($destination);
|
||||||
unset($image);
|
unset($image);
|
||||||
} finally {
|
} finally {
|
||||||
ini_set('memory_limit', $old_limit); // Restore the old memory limit
|
ini_set('memory_limit', $old_limit); // Restore the old memory limit
|
||||||
}
|
}
|
||||||
|
|
||||||
return Event::next;
|
return Event::next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -250,7 +250,7 @@ class Attachment extends Entity
|
|||||||
'gsactor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'GSActor.id', 'multiplicity' => 'one to one', 'description' => 'If set, used so each actor can have a version of this file (for avatars, for instance)'],
|
'gsactor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'GSActor.id', 'multiplicity' => 'one to one', 'description' => 'If set, used so each actor can have a version of this file (for avatars, for instance)'],
|
||||||
'mimetype' => ['type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'],
|
'mimetype' => ['type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'],
|
||||||
'title' => ['type' => 'text', 'description' => 'title of resource when available'],
|
'title' => ['type' => 'text', 'description' => 'title of resource when available'],
|
||||||
'filename' => ['type' => 'varchar', 'length' => 191, 'description' => 'title of resource when available'],
|
'filename' => ['type' => 'varchar', 'length' => 191, 'description' => 'file name of resource when available'],
|
||||||
'is_local' => ['type' => 'bool', 'description' => 'whether the file is stored locally'],
|
'is_local' => ['type' => 'bool', 'description' => 'whether the file is stored locally'],
|
||||||
'source' => ['type' => 'int', 'default' => null, 'description' => 'Source of the Attachment (upload, TFN, embed)'],
|
'source' => ['type' => 'int', 'default' => null, 'description' => 'Source of the Attachment (upload, TFN, embed)'],
|
||||||
'scope' => ['type' => 'int', 'default' => null, 'description' => 'visibility scope for this attachment'],
|
'scope' => ['type' => 'int', 'default' => null, 'description' => 'visibility scope for this attachment'],
|
||||||
|
@ -142,7 +142,9 @@ class AttachmentThumbnail extends Entity
|
|||||||
|
|
||||||
public function getFilename()
|
public function getFilename()
|
||||||
{
|
{
|
||||||
return $this->getAttachment()->getFileHash() . "-{$this->width}x{$this->height}.webp";
|
// TODO only for images
|
||||||
|
$ext = image_type_to_extension(IMAGETYPE_WEBP, include_dot: true);
|
||||||
|
return $this->getAttachment()->getFileHash() . "-{$this->width}x{$this->height}{$ext}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPath()
|
public function getPath()
|
||||||
|
Loading…
Reference in New Issue
Block a user