Passed
Push — master ( 447932...1a32cb )
by Julito
13:13
created

ResourceController::getFileAction()   A

Complexity

Conditions 4
Paths 12

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 12
nop 2
dl 0
loc 27
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Controller;
5
6
use League\Flysystem\Adapter\Local;
7
use League\Flysystem\Filesystem;
8
use Symfony\Component\HttpFoundation\BinaryFileResponse;
9
use Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
/**
16
 * Class ResourceController.
17
 *
18
 * @author Julio Montoya <[email protected]>.
19
 *
20
 * @Route("/resource")
21
 *
22
 * @package Chamilo\CoreBundle\Controller
23
 */
24
class ResourceController extends BaseController
25
{
26
    /**
27
     * Upload form
28
     * @Route("/upload/{type}/{id}", name="resource_upload", methods={"GET", "POST"}, options={"expose"=true})
29
     *
30
     * @return Response
31
     */
32
    public function uploadFile($type, $id): Response
33
    {
34
        //$helper = $this->container->get('oneup_uploader.templating.uploader_helper');
35
        //$endpoint = $helper->endpoint('courses');
36
        return $this->render(
37
            '@ChamiloCore/Resource/upload.html.twig',
38
            [
39
                'identifier' => $id,
40
                'type' => $type,
41
            ]
42
        );
43
    }
44
45
    /**
46
     * Downloads the file courses/MATHS/document/file.jpg to the user.
47
     * @Route("/download/{course}/", name="resource_download", methods={"GET"}, options={"expose"=true})
48
     * @todo check permissions
49
     *
50
     * @param string $course
51
     *
52
     * @return \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
53
     */
54
    public function downloadFileAction(Request $request, $course)
55
    {
56
        try {
57
            /** @var Filesystem $fs */
58
            $fs = $this->container->get('oneup_flysystem.courses_filesystem');
59
            $file = $request->get('file');
60
61
            $path = $course.'/document/'.$file;
62
63
            // Has folder
64
            if (!$fs->has($course)) {
65
                return $this->abort();
66
            }
67
68
            // Has file
69
            if (!$fs->has($path)) {
70
                return $this->abort();
71
            }
72
73
            /** @var Local $adapter */
74
            $adapter = $fs->getAdapter();
75
            $filePath = $adapter->getPathPrefix().$path;
76
77
            $response = new BinaryFileResponse($filePath);
78
79
            // To generate a file download, you need the mimetype of the file
80
            $mimeTypeGuesser = new FileinfoMimeTypeGuesser();
81
82
            // Set the mimetype with the guesser or manually
83
            if ($mimeTypeGuesser->isSupported()) {
84
                // Guess the mimetype of the file according to the extension of the file
85
                $response->headers->set('Content-Type', $mimeTypeGuesser->guess($filePath));
86
            } else {
87
                // Set the mimetype of the file manually, in this case for a text file is text/plain
88
                $response->headers->set('Content-Type', 'text/plain');
89
            }
90
91
            $response->setContentDisposition(
92
                ResponseHeaderBag::DISPOSITION_ATTACHMENT,
93
                basename($filePath)
94
            );
95
96
            return $response;
97
98
        } catch (\InvalidArgumentException $e) {
99
            return $this->abort();
100
        }
101
    }
102
103
    /**
104
     * Gets a document in browser courses/MATHS/document/file.jpg to the user.
105
     * @Route("/get/{course}/", name="resource_get", methods={"GET"}, options={"expose"=true})
106
     * @todo check permissions
107
     *
108
     * @param string $course
109
     *
110
     * @return \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
111
     */
112
    public function getFileAction(Request $request, $course)
113
    {
114
        try {
115
            /** @var Filesystem $fs */
116
            $fs = $this->container->get('oneup_flysystem.courses_filesystem');
117
            $file = $request->get('file');
118
119
            $path = $course.'/document/'.$file;
120
121
            // Has folder
122
            if (!$fs->has($course)) {
123
                return $this->abort();
124
            }
125
126
            // Has file
127
            if (!$fs->has($path)) {
128
                return $this->abort();
129
            }
130
131
            /** @var Local $adapter */
132
            $adapter = $fs->getAdapter();
133
            $filePath = $adapter->getPathPrefix().$path;
134
135
            return $this->file($filePath, null, ResponseHeaderBag::DISPOSITION_INLINE);
136
137
        } catch (\InvalidArgumentException $e) {
138
            return $this->abort();
139
        }
140
    }
141
}
142