diff --git a/plugins/ImageEncoder/ImageEncoder.php b/plugins/ImageEncoder/ImageEncoder.php index 505f3df604..1eb3a6bbda 100644 --- a/plugins/ImageEncoder/ImageEncoder.php +++ b/plugins/ImageEncoder/ImageEncoder.php @@ -80,26 +80,30 @@ class ImageEncoder extends Plugin public function onResizeImage(Attachment $attachment, AttachmentThumbnail $thumbnail, int $width, int $height, bool $crop): bool { $old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit')); - try { - // -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::thumbnail($attachment->getPath(), $width, ['height' => $height]); - } catch (Exception $e) { - Log::error(__METHOD__ . ' encountered exception: ' . print_r($e, true)); - // TRANS: Exception thrown when trying to resize an unknown file type. - throw new Exception(_m('Unknown file type')); + try { + // -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::thumbnail($attachment->getPath(), $width, ['height' => $height]); + } catch (Exception $e) { + Log::error(__METHOD__ . ' encountered exception: ' . print_r($e, true)); + // TRANS: Exception thrown when trying to resize an unknown file type. + throw new Exception(_m('Unknown file type')); + } + + if ($attachment->getPath() === $thumbnail->getPath()) { + @unlink($thumbnail->getPath()); + } + + if ($crop) { + $image = $image->smartcrop($width, $height); + } + $image->writeToFile($thumbnail->getPath()); + unset($image); + } finally { + ini_set('memory_limit', $old_limit); // Restore the old memory limit } - if ($attachment->getPath() === $thumbnail->getPath()) { - @unlink($thumbnail->getPath()); - } - - $image->writeToFile($thumbnail->getPath()); - unset($image); - - ini_set('memory_limit', $old_limit); // Restore the old memory limit - return Event::next; } } diff --git a/src/Controller/Attachment.php b/src/Controller/Attachment.php index 3980e3c8c2..f8a838f293 100644 --- a/src/Controller/Attachment.php +++ b/src/Controller/Attachment.php @@ -22,28 +22,55 @@ namespace App\Controller; use App\Core\Controller; -use App\Core\GSFile as M; +use App\Core\DB\DB; +use App\Core\GSFile; +use App\Entity\AttachmentThumbnail; +use App\Util\Common; +use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Component\HttpFoundation\Request; class Attachment extends Controller { public function attachment_show(Request $request, int $id) { + $res = GSFile::getAttachmentFileInfo($id); + return GSFile::sendFile($res['file_path'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_INLINE); } public function attachment_view(Request $request, int $id) { - $res = M::getAttachmentFileInfo($id); - return M::sendFile($res['file_path'], $res['mimetype'], $res['title'], 'inline'); + $res = GSFile::getAttachmentFileInfo($id); + return GSFile::sendFile($res['file_path'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_INLINE); } public function attachment_download(Request $request, int $id) { - $res = M::getAttachmentFileInfo($id); - return M::sendFile($res['file_path'], $res['mimetype'], $res['title'], 'attachment'); + $res = GSFile::getAttachmentFileInfo($id); + return GSFile::sendFile($res['file_path'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_ATTACHMENT); } public function attachment_thumbnail(Request $request, int $id) { + $attachment = DB::findOneBy('attachment', ['id' => $id]); + if (!is_null($attachment->getScope())) { + // && ($attachment->scope | VisibilityScope::PUBLIC) != 0 + // $user = Common::ensureLoggedIn(); + assert(false, 'Attachment scope not implemented'); + } + + // TODO rate limit, limit to known sizes + + $max_width = Common::config('thumbnail', 'width'); + $max_height = Common::config('thumbnail', 'height'); + $width = Common::clamp($this->int('w') ?: $max_width, min: 0, max: $max_width); + $height = Common::clamp($this->int('h') ?: $max_height, min: 0, max: $max_height); + $crop = $this->bool('c') ?: false; + + $thumbnail = AttachmentThumbnail::getOrCreate(attachment: $attachment, width: $width, height: $height, crop: $crop); + + $filename = $thumbnail->getFilename(); + $path = $thumbnail->getPath(); + + return GSFile::sendFile(filepath: $path, mimetype: $attachment->getMimetype(), output_filename: $filename, disposition: 'inline'); } } diff --git a/src/Core/GSFile.php b/src/Core/GSFile.php index 601bd764dd..5e8b9c3f87 100644 --- a/src/Core/GSFile.php +++ b/src/Core/GSFile.php @@ -86,7 +86,7 @@ class GSFile [ 'Content-Description' => 'File Transfer', 'Content-Type' => $mimetype, - 'Content-Disposition' => HeaderUtils::makeDisposition($disposition, $output_filename ?: _m('Untitled attachment')), + 'Content-Disposition' => HeaderUtils::makeDisposition($disposition, $output_filename ?: _m('Untitled attachment'), _m('Untitled attachment')), 'Cache-Control' => 'public', ], $public = true, diff --git a/src/Routes/Attachments.php b/src/Routes/Attachments.php new file mode 100644 index 0000000000..83b9ab6311 --- /dev/null +++ b/src/Routes/Attachments.php @@ -0,0 +1,49 @@ +. + +// }}} + +/** + * Define social's attachment routes + * + * @package GNUsocial + * @category Router + * + * @author Diogo Cordeiro + * @author Hugo Sales + * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ + +namespace App\Routes; + +use App\Controller as C; +use App\Core\Router\RouteLoader; + +abstract class Attachments +{ + public static function load(RouteLoader $r): void + { + $r->connect('attachment_show', '/attachment/{id<\d+>}', [C\Attachment::class, 'attachment_show']); + $r->connect('attachment_view', '/attachment/{id<\d+>}/view', [C\Attachment::class, 'attachment_view']); + $r->connect('attachment_download', '/attachment/{id<\d+>}/download', [C\Attachment::class, 'attachment_download']); + $r->connect('attachment_thumbnail', '/attachment/{id<\d+>}/thumbnail', [C\Attachment::class, 'attachment_thumbnail']); + $r->connect('thumbnail', '/thumbnail/{id<\d+>}', [C\Attachment::class, 'attachment_thumbnail']); + } +} diff --git a/src/Routes/Main.php b/src/Routes/Main.php index 7a68cc3bbc..04c370502a 100644 --- a/src/Routes/Main.php +++ b/src/Routes/Main.php @@ -69,11 +69,5 @@ abstract class Main foreach (['personal_info', 'avatar', 'notifications', 'account'] as $s) { $r->connect('settings_' . $s, '/settings/' . $s, [C\UserPanel::class, $s]); } - - // Attachments - $r->connect('attachment_show', '/attachment/{id<\d+>}', [C\Attachment::class, 'attachment_show']); - $r->connect('attachment_view', '/attachment/{id<\d+>}/view', [C\Attachment::class, 'attachment_view']); - $r->connect('attachment_download', '/attachment/{id<\d+>}/download', [C\Attachment::class, 'attachment_download']); - $r->connect('attachment_thumbnail', '/attachment/{id<\d+>}/thumbnail', [C\Attachment::class, 'attachment_thumbnail']); } }