Part-DB.Part-DB-server/src/Controller/AttachmentFileController.php

126 lines
4.5 KiB
PHP
Raw Normal View History

<?php
2020-02-22 18:14:36 +01:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
2022-11-29 22:28:53 +01:00
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
2020-02-22 18:14:36 +01:00
*
* This program 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-01-05 15:46:58 +01:00
declare(strict_types=1);
namespace App\Controller;
use App\DataTables\AttachmentDataTable;
use App\DataTables\Filters\AttachmentFilter;
use App\Entity\Attachments\Attachment;
use App\Form\Filters\AttachmentFilterType;
use App\Services\Attachments\AttachmentManager;
use App\Services\Trees\NodesListBuilder;
use Omines\DataTablesBundle\DataTableFactory;
2020-01-05 22:49:00 +01:00
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
2020-01-05 22:49:00 +01:00
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
class AttachmentFileController extends AbstractController
{
/**
* Download the selected attachment.
*/
2023-05-28 01:21:05 +02:00
#[Route(path: '/attachment/{id}/download', name: 'attachment_download')]
2020-02-02 14:05:36 +01:00
public function download(Attachment $attachment, AttachmentManager $helper): BinaryFileResponse
{
2019-08-20 18:39:57 +02:00
$this->denyAccessUnlessGranted('read', $attachment);
if ($attachment->isSecure()) {
$this->denyAccessUnlessGranted('show_private', $attachment);
}
if ($attachment->isExternal()) {
2020-01-05 22:49:00 +01:00
throw new RuntimeException('You can not download external attachments!');
}
2020-08-21 21:36:22 +02:00
if (!$helper->isFileExisting($attachment)) {
2020-01-05 22:49:00 +01:00
throw new RuntimeException('The file associated with the attachment is not existing!');
}
$file_path = $helper->toAbsoluteFilePath($attachment);
$response = new BinaryFileResponse($file_path);
//Set header content disposition, so that the file will be downloaded
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
return $response;
}
/**
* View the attachment.
*/
2023-05-28 01:21:05 +02:00
#[Route(path: '/attachment/{id}/view', name: 'attachment_view')]
2020-02-02 14:05:36 +01:00
public function view(Attachment $attachment, AttachmentManager $helper): BinaryFileResponse
{
2019-08-20 18:39:57 +02:00
$this->denyAccessUnlessGranted('read', $attachment);
if ($attachment->isSecure()) {
$this->denyAccessUnlessGranted('show_private', $attachment);
}
if ($attachment->isExternal()) {
2020-01-05 22:49:00 +01:00
throw new RuntimeException('You can not download external attachments!');
}
2020-08-21 21:36:22 +02:00
if (!$helper->isFileExisting($attachment)) {
2020-01-05 22:49:00 +01:00
throw new RuntimeException('The file associated with the attachment is not existing!');
}
$file_path = $helper->toAbsoluteFilePath($attachment);
$response = new BinaryFileResponse($file_path);
//Set header content disposition, so that the file will be downloaded
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE);
return $response;
}
2023-05-28 01:21:05 +02:00
#[Route(path: '/attachment/list', name: 'attachment_list')]
2023-04-15 21:49:19 +02:00
public function attachmentsTable(Request $request, DataTableFactory $dataTableFactory, NodesListBuilder $nodesListBuilder): Response
{
$this->denyAccessUnlessGranted('@attachments.list_attachments');
$formRequest = clone $request;
$formRequest->setMethod('GET');
$filter = new AttachmentFilter($nodesListBuilder);
$filterForm = $this->createForm(AttachmentFilterType::class, $filter, ['method' => 'GET']);
$filterForm->handleRequest($formRequest);
$table = $dataTableFactory->createFromType(AttachmentDataTable::class, ['filter' => $filter])
->handleRequest($request);
if ($table->isCallback()) {
return $table->getResponse();
}
return $this->render('attachment_list.html.twig', [
'datatable' => $table,
2023-05-28 01:21:05 +02:00
'filterForm' => $filterForm,
]);
}
}