2019-08-10 18:06:28 +02:00
|
|
|
<?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);
|
|
|
|
|
2019-08-10 18:06:28 +02:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2019-10-04 18:06:37 +02:00
|
|
|
use App\DataTables\AttachmentDataTable;
|
2022-09-11 02:00:22 +02:00
|
|
|
use App\DataTables\Filters\AttachmentFilter;
|
|
|
|
use App\DataTables\Filters\PartFilter;
|
|
|
|
use App\DataTables\PartsDataTable;
|
2019-08-12 15:47:57 +02:00
|
|
|
use App\Entity\Attachments\Attachment;
|
2019-10-04 18:06:37 +02:00
|
|
|
use App\Entity\Attachments\PartAttachment;
|
2022-09-11 02:00:22 +02:00
|
|
|
use App\Form\Filters\AttachmentFilterType;
|
|
|
|
use App\Form\Filters\PartFilterType;
|
2019-10-19 23:29:51 +02:00
|
|
|
use App\Services\Attachments\AttachmentManager;
|
2022-09-11 02:00:22 +02:00
|
|
|
use App\Services\Trees\NodesListBuilder;
|
2019-10-04 18:06:37 +02:00
|
|
|
use Omines\DataTablesBundle\DataTableFactory;
|
2020-01-05 22:49:00 +01:00
|
|
|
use RuntimeException;
|
2019-08-10 18:06:28 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
2020-01-05 22:49:00 +01:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2019-10-04 18:06:37 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2020-01-05 22:49:00 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2019-08-10 18:06:28 +02:00
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
|
|
|
|
class AttachmentFileController extends AbstractController
|
|
|
|
{
|
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* Download the selected attachment.
|
2019-08-10 18:06:28 +02:00
|
|
|
*
|
|
|
|
* @Route("/attachment/{id}/download", name="attachment_download")
|
|
|
|
*/
|
2020-02-02 14:05:36 +01:00
|
|
|
public function download(Attachment $attachment, AttachmentManager $helper): BinaryFileResponse
|
2019-08-10 18:06:28 +02:00
|
|
|
{
|
2019-08-20 18:39:57 +02:00
|
|
|
$this->denyAccessUnlessGranted('read', $attachment);
|
2019-08-10 18:06:28 +02:00
|
|
|
|
2020-03-30 16:56:58 +02:00
|
|
|
if ($attachment->isSecure()) {
|
|
|
|
$this->denyAccessUnlessGranted('show_private', $attachment);
|
|
|
|
}
|
|
|
|
|
2019-08-10 18:06:28 +02:00
|
|
|
if ($attachment->isExternal()) {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new RuntimeException('You can not download external attachments!');
|
2019-08-10 18:06:28 +02:00
|
|
|
}
|
|
|
|
|
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!');
|
2019-08-10 18:06:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* View the attachment.
|
2019-08-10 18:06:28 +02:00
|
|
|
*
|
|
|
|
* @Route("/attachment/{id}/view", name="attachment_view")
|
|
|
|
*/
|
2020-02-02 14:05:36 +01:00
|
|
|
public function view(Attachment $attachment, AttachmentManager $helper): BinaryFileResponse
|
2019-08-10 18:06:28 +02:00
|
|
|
{
|
2019-08-20 18:39:57 +02:00
|
|
|
$this->denyAccessUnlessGranted('read', $attachment);
|
2019-08-10 18:06:28 +02:00
|
|
|
|
2020-03-30 16:56:58 +02:00
|
|
|
if ($attachment->isSecure()) {
|
|
|
|
$this->denyAccessUnlessGranted('show_private', $attachment);
|
|
|
|
}
|
|
|
|
|
2019-08-10 18:06:28 +02:00
|
|
|
if ($attachment->isExternal()) {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new RuntimeException('You can not download external attachments!');
|
2019-08-10 18:06:28 +02:00
|
|
|
}
|
|
|
|
|
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!');
|
2019-08-10 18:06:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2019-10-04 18:06:37 +02:00
|
|
|
/**
|
|
|
|
* @Route("/attachment/list", name="attachment_list")
|
|
|
|
*/
|
2023-04-15 21:49:19 +02:00
|
|
|
public function attachmentsTable(Request $request, DataTableFactory $dataTableFactory, NodesListBuilder $nodesListBuilder): Response
|
2019-10-04 18:06:37 +02:00
|
|
|
{
|
2022-11-05 23:49:53 +01:00
|
|
|
$this->denyAccessUnlessGranted('@attachments.list_attachments');
|
2019-10-04 18:06:37 +02:00
|
|
|
|
2022-09-11 02:00:22 +02:00
|
|
|
$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])
|
2019-10-04 18:06:37 +02:00
|
|
|
->handleRequest($request);
|
|
|
|
|
|
|
|
if ($table->isCallback()) {
|
|
|
|
return $table->getResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render('attachment_list.html.twig', [
|
2019-11-09 00:47:20 +01:00
|
|
|
'datatable' => $table,
|
2022-09-11 02:00:22 +02:00
|
|
|
'filterForm' => $filterForm->createView(),
|
2019-10-04 18:06:37 +02:00
|
|
|
]);
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|