Completed
Push — master ( 2f0dc6...f53cc0 )
by Jan
05:37
created

AttachmentFileController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 84
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A view() 0 20 3
A download() 0 20 3
A attachmentsTable() 0 13 2
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Controller;
33
34
35
use App\DataTables\AttachmentDataTable;
36
use App\DataTables\PartsDataTable;
37
use App\Entity\Attachments\Attachment;
38
use App\Entity\Attachments\PartAttachment;
39
use App\Services\AttachmentHelper;
40
use Omines\DataTablesBundle\DataTableFactory;
41
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
42
use Symfony\Component\HttpFoundation\BinaryFileResponse;
43
use Symfony\Component\HttpFoundation\Request;
44
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
45
use Symfony\Component\Routing\Annotation\Route;
46
47
class AttachmentFileController extends AbstractController
48
{
49
50
    /**
51
     * Download the selected attachment
52
     *
53
     * @Route("/attachment/{id}/download", name="attachment_download")
54
     * @param Attachment $attachment
55
     * @param AttachmentHelper $helper
56
     * @return BinaryFileResponse
57
     * @throws \Exception
58
     */
59
    public function download(Attachment $attachment, AttachmentHelper $helper)
60
    {
61
        $this->denyAccessUnlessGranted('read', $attachment);
62
63
        if ($attachment->isExternal()) {
64
            throw new \RuntimeException('You can not download external attachments!');
65
        }
66
67
        if (!$helper->isFileExisting($attachment)) {
68
            throw new \RuntimeException('The file associated with the attachment is not existing!');
69
        }
70
71
72
        $file_path = $helper->toAbsoluteFilePath($attachment);
73
        $response = new BinaryFileResponse($file_path);
74
75
        //Set header content disposition, so that the file will be downloaded
76
        $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
77
78
        return $response;
79
    }
80
81
    /**
82
     * View the attachment
83
     *
84
     * @Route("/attachment/{id}/view", name="attachment_view")
85
     * @param Attachment $attachment
86
     * @param AttachmentHelper $helper
87
     * @return BinaryFileResponse
88
     * @throws \Exception
89
     */
90
    public function view(Attachment $attachment, AttachmentHelper $helper)
91
    {
92
        $this->denyAccessUnlessGranted('read', $attachment);
93
94
        if ($attachment->isExternal()) {
95
            throw new \RuntimeException('You can not download external attachments!');
96
        }
97
98
        if (!$helper->isFileExisting($attachment)) {
99
            throw new \RuntimeException('The file associated with the attachment is not existing!');
100
        }
101
102
103
        $file_path = $helper->toAbsoluteFilePath($attachment);
104
        $response = new BinaryFileResponse($file_path);
105
106
        //Set header content disposition, so that the file will be downloaded
107
        $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE);
108
109
        return $response;
110
    }
111
112
    /**
113
     * @Route("/attachment/list", name="attachment_list")
114
     * @param DataTableFactory $dataTable
115
     * @param Request $request
116
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
117
     */
118
    public function attachmentsTable(DataTableFactory $dataTable, Request $request)
119
    {
120
        $this->denyAccessUnlessGranted('read', new PartAttachment());
121
122
        $table = $dataTable->createFromType(AttachmentDataTable::class)
123
            ->handleRequest($request);
124
125
        if ($table->isCallback()) {
126
            return $table->getResponse();
127
        }
128
129
        return $this->render('attachment_list.html.twig', [
130
            'datatable' => $table
131
        ]);
132
    }
133
134
}