2020-08-09 01:10:25 +09:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 17:26:18 +09:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2020-08-09 01:10:25 +09:00
|
|
|
// {{{ 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/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
2021-12-03 00:12:31 +09:00
|
|
|
namespace Component\Attachment\Controller;
|
2020-08-09 01:10:25 +09:00
|
|
|
|
|
|
|
use App\Core\Controller;
|
2021-04-19 16:28:53 +09:00
|
|
|
use App\Core\DB\DB;
|
2021-04-29 00:01:40 +09:00
|
|
|
use App\Core\Event;
|
2021-04-19 16:28:53 +09:00
|
|
|
use App\Core\GSFile;
|
2021-07-23 04:56:29 +09:00
|
|
|
use function App\Core\I18n\_m;
|
2021-12-27 11:47:04 +09:00
|
|
|
use App\Entity\Note;
|
2021-04-19 16:28:53 +09:00
|
|
|
use App\Util\Common;
|
2021-04-29 00:01:40 +09:00
|
|
|
use App\Util\Exception\ClientException;
|
2021-08-17 01:09:44 +09:00
|
|
|
use App\Util\Exception\NoSuchFileException;
|
2021-04-19 20:20:10 +09:00
|
|
|
use App\Util\Exception\NotFoundException;
|
|
|
|
use App\Util\Exception\ServerException;
|
2021-12-26 18:48:16 +09:00
|
|
|
use Component\Attachment\Entity\AttachmentThumbnail;
|
2022-03-08 02:09:53 +09:00
|
|
|
use Component\Attachment\Entity\AttachmentToNote;
|
2021-04-19 16:28:53 +09:00
|
|
|
use Symfony\Component\HttpFoundation\HeaderUtils;
|
2020-08-09 01:10:25 +09:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-04-19 20:20:10 +09:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2021-07-23 04:56:29 +09:00
|
|
|
use Symfony\Component\Mime\MimeTypes;
|
2020-08-09 01:10:25 +09:00
|
|
|
|
2021-04-18 10:17:57 +09:00
|
|
|
class Attachment extends Controller
|
2020-08-09 01:10:25 +09:00
|
|
|
{
|
2021-08-03 19:13:01 +09:00
|
|
|
/**
|
|
|
|
* Generic function that handles getting a representation for an attachment
|
|
|
|
*/
|
2021-12-27 11:47:04 +09:00
|
|
|
private function attachment(int $attachment_id, Note|int $note, callable $handle)
|
2021-04-18 13:47:16 +09:00
|
|
|
{
|
2021-12-27 11:47:04 +09:00
|
|
|
$attachment = DB::findOneBy('attachment', ['id' => $attachment_id]);
|
|
|
|
$note = \is_int($note) ? Note::getById($note) : $note;
|
|
|
|
|
2022-03-08 02:09:53 +09:00
|
|
|
// Before anything, two very important things!
|
|
|
|
// first: ensure this attachment is associated with this note
|
|
|
|
if (DB::count(AttachmentToNote::class, ['attachment_id' => $attachment->getId(), 'note_id' => $note->getId()]) <= 0) {
|
|
|
|
throw new ClientException(_m('No such attachment.'), 404);
|
|
|
|
}
|
|
|
|
// second: ensure proper scope
|
2021-12-27 11:47:04 +09:00
|
|
|
if (!$note->isVisibleTo(Common::actor())) {
|
|
|
|
throw new ClientException(_m('You don\'t have permissions to view this attachment.'), 401);
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = null;
|
|
|
|
if (Event::handle('AttachmentFileInfo', [$attachment, $note, &$res]) !== Event::stop) {
|
|
|
|
// If no one else claims this attachment, use the default representation
|
|
|
|
try {
|
|
|
|
$res = GSFile::getAttachmentFileInfo($attachment_id);
|
|
|
|
} catch (NoSuchFileException $e) {
|
|
|
|
// Continue below
|
2021-08-03 19:13:01 +09:00
|
|
|
}
|
2021-04-29 00:01:40 +09:00
|
|
|
}
|
2021-08-03 19:13:01 +09:00
|
|
|
|
2021-08-17 01:09:44 +09:00
|
|
|
if (empty($res)) {
|
2021-05-02 08:50:16 +09:00
|
|
|
throw new ClientException(_m('No such attachment'), 404);
|
2021-08-17 01:09:44 +09:00
|
|
|
} else {
|
2021-10-10 17:26:18 +09:00
|
|
|
if (!\array_key_exists('filepath', $res)) {
|
2021-08-17 01:09:44 +09:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
throw new ServerException('This attachment is not stored locally.');
|
2021-08-04 02:53:58 +09:00
|
|
|
// @codeCoverageIgnoreEnd
|
2021-08-17 01:09:44 +09:00
|
|
|
} else {
|
2021-12-27 11:47:04 +09:00
|
|
|
$res['attachment'] = $attachment;
|
|
|
|
$res['note'] = $note;
|
|
|
|
$res['title'] = $attachment->getBestTitle($note);
|
2021-08-17 01:09:44 +09:00
|
|
|
return $handle($res);
|
|
|
|
}
|
2021-04-29 00:01:40 +09:00
|
|
|
}
|
2021-04-18 13:47:16 +09:00
|
|
|
}
|
|
|
|
|
2021-04-29 06:25:35 +09:00
|
|
|
/**
|
|
|
|
* The page where the attachment and it's info is shown
|
|
|
|
*/
|
2021-12-27 11:47:04 +09:00
|
|
|
public function attachmentShowWithNote(Request $request, int $note_id, int $attachment_id)
|
2021-04-29 06:25:35 +09:00
|
|
|
{
|
|
|
|
try {
|
2021-12-27 11:47:04 +09:00
|
|
|
return $this->attachment($attachment_id, $note_id, function ($res) use ($note_id, $attachment_id) {
|
2021-04-29 06:25:35 +09:00
|
|
|
return [
|
2022-02-07 06:37:04 +09:00
|
|
|
'_template' => 'attachment/view.html.twig',
|
2021-12-27 11:47:04 +09:00
|
|
|
'download' => $res['attachment']->getDownloadUrl(note: $note_id),
|
|
|
|
'title' => $res['title'],
|
|
|
|
'attachment' => $res['attachment'],
|
|
|
|
'note' => $res['note'],
|
2021-12-28 13:39:09 +09:00
|
|
|
'right_panel_vars' => ['attachment_id' => $attachment_id, 'note_id' => $note_id],
|
2021-04-29 06:25:35 +09:00
|
|
|
];
|
|
|
|
});
|
|
|
|
} catch (NotFoundException) {
|
2021-09-22 23:08:30 +09:00
|
|
|
throw new ClientException(_m('No such attachment.'), 404);
|
2021-04-29 06:25:35 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the attachment inline
|
|
|
|
*/
|
2021-12-27 11:47:04 +09:00
|
|
|
public function attachmentViewWithNote(Request $request, int $note_id, int $attachment_id)
|
2021-04-18 13:47:16 +09:00
|
|
|
{
|
2021-10-10 17:26:18 +09:00
|
|
|
return $this->attachment(
|
2021-12-27 11:47:04 +09:00
|
|
|
$attachment_id,
|
|
|
|
$note_id,
|
2021-10-10 17:26:18 +09:00
|
|
|
fn (array $res) => GSFile::sendFile(
|
|
|
|
$res['filepath'],
|
|
|
|
$res['mimetype'],
|
2021-12-27 11:47:04 +09:00
|
|
|
GSFile::ensureFilenameWithProperExtension($res['title'], $res['mimetype']) ?? $res['filename'],
|
2021-10-10 17:26:18 +09:00
|
|
|
HeaderUtils::DISPOSITION_INLINE,
|
|
|
|
),
|
2021-08-17 01:09:44 +09:00
|
|
|
);
|
2021-04-18 13:47:16 +09:00
|
|
|
}
|
|
|
|
|
2021-12-27 11:47:04 +09:00
|
|
|
public function attachmentDownloadWithNote(Request $request, int $note_id, int $attachment_id)
|
2020-09-05 11:28:50 +09:00
|
|
|
{
|
2021-10-10 17:26:18 +09:00
|
|
|
return $this->attachment(
|
2021-12-27 11:47:04 +09:00
|
|
|
$attachment_id,
|
|
|
|
$note_id,
|
2021-10-10 17:26:18 +09:00
|
|
|
fn (array $res) => GSFile::sendFile(
|
|
|
|
$res['filepath'],
|
|
|
|
$res['mimetype'],
|
2021-12-27 11:47:04 +09:00
|
|
|
GSFile::ensureFilenameWithProperExtension($res['title'], $res['mimetype']) ?? $res['filename'],
|
2021-10-10 17:26:18 +09:00
|
|
|
HeaderUtils::DISPOSITION_ATTACHMENT,
|
|
|
|
),
|
2021-08-17 01:09:44 +09:00
|
|
|
);
|
2021-04-18 13:47:16 +09:00
|
|
|
}
|
|
|
|
|
2021-04-19 20:20:10 +09:00
|
|
|
/**
|
|
|
|
* Controller to produce a thumbnail for a given attachment id
|
|
|
|
*
|
2021-12-27 11:47:04 +09:00
|
|
|
* @param int $attachment_id Attachment ID
|
2021-04-19 20:20:10 +09:00
|
|
|
*
|
2021-10-10 17:26:18 +09:00
|
|
|
* @throws \App\Util\Exception\DuplicateFoundException
|
2021-05-02 08:50:16 +09:00
|
|
|
* @throws ClientException
|
2021-04-19 20:20:10 +09:00
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws ServerException
|
|
|
|
*/
|
2021-12-27 11:47:04 +09:00
|
|
|
public function attachmentThumbnailWithNote(Request $request, int $note_id, int $attachment_id, string $size = 'small'): Response
|
2021-04-18 13:47:16 +09:00
|
|
|
{
|
2021-12-27 11:47:04 +09:00
|
|
|
$attachment = DB::findOneBy('attachment', ['id' => $attachment_id]);
|
2022-03-08 02:09:53 +09:00
|
|
|
$note = Note::getById($note_id);
|
|
|
|
|
|
|
|
// Before anything, two very important things!
|
|
|
|
// first: ensure this attachment is associated with this note
|
|
|
|
if (DB::count(AttachmentToNote::class, ['attachment_id' => $attachment->getId(), 'note_id' => $note->getId()]) <= 0) {
|
|
|
|
throw new ClientException(_m('No such attachment.'), 404);
|
|
|
|
}
|
|
|
|
// second: ensure proper scope
|
|
|
|
if (!$note->isVisibleTo(Common::actor())) {
|
|
|
|
throw new ClientException(_m('You don\'t have permissions to view this attachment.'), 401);
|
|
|
|
}
|
2021-07-23 04:56:29 +09:00
|
|
|
|
2021-09-22 23:08:30 +09:00
|
|
|
$crop = Common::config('thumbnail', 'smart_crop');
|
2021-04-19 16:28:53 +09:00
|
|
|
|
2021-09-22 23:08:30 +09:00
|
|
|
$thumbnail = AttachmentThumbnail::getOrCreate(attachment: $attachment, size: $size, crop: $crop);
|
2021-10-10 17:26:18 +09:00
|
|
|
if (\is_null($thumbnail)) {
|
2021-09-22 23:08:30 +09:00
|
|
|
throw new ClientException(_m('Can not generate thumbnail for attachment with id={id}', ['id' => $attachment->getId()]));
|
2021-04-29 06:53:02 +09:00
|
|
|
}
|
2021-04-19 16:28:53 +09:00
|
|
|
|
|
|
|
$filename = $thumbnail->getFilename();
|
|
|
|
$path = $thumbnail->getPath();
|
2021-08-15 00:47:45 +09:00
|
|
|
$mimetype = $thumbnail->getMimetype();
|
2021-04-19 16:28:53 +09:00
|
|
|
|
2021-07-23 04:56:29 +09:00
|
|
|
return GSFile::sendFile(filepath: $path, mimetype: $mimetype, output_filename: $filename . '.' . MimeTypes::getDefault()->getExtensions($mimetype)[0], disposition: HeaderUtils::DISPOSITION_INLINE);
|
2020-09-05 11:28:50 +09:00
|
|
|
}
|
2020-08-09 01:10:25 +09:00
|
|
|
}
|