[ATTACHMENTS] Move thumbnail controller to core and cleanup

This commit is contained in:
Hugo Sales 2021-04-19 07:28:53 +00:00
parent 3a7e92ed01
commit e1995f44ce
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
5 changed files with 103 additions and 29 deletions

View File

@ -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;
}
}

View File

@ -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');
}
}

View File

@ -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,

View File

@ -0,0 +1,49 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social 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.
//
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
/**
* Define social's attachment routes
*
* @package GNUsocial
* @category Router
*
* @author Diogo Cordeiro <mail@diogo.site>
* @author Hugo Sales <hugo@hsal.es>
* @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']);
}
}

View File

@ -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']);
}
}