Passed
Push — master ( 595fcf...04cbf0 )
by Julito
13:01
created

ResourceController::uploadFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
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\Filesystem;
7
use League\Flysystem\Adapter\Local;
8
use Symfony\Component\HttpFoundation\BinaryFileResponse;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Routing\Annotation\Route;
11
12
/**
13
 * Class ResourceController
14
 * @author Julio Montoya <[email protected]>.
15
 *
16
 * @Route("/resource")
17
 *
18
 * @package Chamilo\CoreBundle\Controller
19
 */
20
class ResourceController extends BaseController
21
{
22
    /**
23
     * @Route("/upload", name="resource_upload", methods={"GET", "POST"}, options={"expose"=true})
24
     *
25
     * @return Response
26
     */
27
    public function uploadFile(): Response
28
    {
29
        $helper = $this->container->get('oneup_uploader.templating.uploader_helper');
30
        $endpoint = $helper->endpoint('courses');
31
32
        return $this->render(
33
            '@ChamiloCore/Resource/upload.html.twig',
34
            [
35
            ]
36
        );
37
    }
38
39
    /**
40
     * Gets a document from the courses/MATHS/document/file.jpg to the user.
41
     *
42
     * @todo check permissions
43
     *
44
     * @param string $course
45
     * @param string $file
46
     *
47
     * @return \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
48
     */
49
    public function getDocumentAction($course, $file)
50
    {
51
        try {
52
            /** @var Filesystem $fs */
53
            $fs = $this->container->get('oneup_flysystem.courses_filesystem');
54
55
56
            $path = $course.'/document/'.$file;
57
58
            // Has folder
59
            if (!$fs->has($course)) {
60
                return $this->abort();
61
            }
62
63
            /** @var Local $adapter */
64
            $adapter = $fs->getAdapter();
65
            $filePath = $adapter->getPathPrefix().$path;
66
67
            return new BinaryFileResponse($filePath);
68
        } catch (\InvalidArgumentException $e) {
69
            return $this->abort();
70
        }
71
    }
72
73
    /**
74
     * Gets a document from the data/courses/MATHS/scorm/file.jpg to the user.
75
     *
76
     * @todo check permissions
77
     *
78
     * @param Application $app
79
     * @param string      $courseCode
80
     * @param string      $file
81
     *
82
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|void
83
     */
84
    public function getScormDocumentAction($app, $courseCode, $file)
85
    {
86
        try {
87
            $file = $app['chamilo.filesystem']->getCourseScormDocument(
88
                $courseCode,
89
                $file
90
            );
91
92
            return $app->sendFile($file->getPathname());
93
        } catch (\InvalidArgumentException $e) {
94
            return $app->abort(404, 'File not found');
95
        }
96
    }
97
98
    /**
99
     * Gets a document from the data/default_platform_document/* folder.
100
     *
101
     * @param Application $app
102
     * @param string      $file
103
     *
104
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|void
105
     */
106
    public function getDefaultCourseDocumentAction($app, $file)
107
    {
108
        try {
109
            $file = $app['chamilo.filesystem']->get(
110
                'default_course_document/'.$file
111
            );
112
113
            return $app->sendFile($file->getPathname());
114
        } catch (\InvalidArgumentException $e) {
115
            return $app->abort(404, 'File not found');
116
        }
117
    }
118
119
    /**
120
     * @param Application $app
121
     * @param $groupId
122
     * @param $file
123
     *
124
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|void
125
     */
126
    public function getGroupFile($app, $groupId, $file)
127
    {
128
        try {
129
            $file = $app['chamilo.filesystem']->get(
130
                'upload/groups/'.$groupId.'/'.$file
131
            );
132
133
            return $app->sendFile($file->getPathname());
134
        } catch (\InvalidArgumentException $e) {
135
            return $app->abort(404, 'File not found');
136
        }
137
    }
138
139
    /**
140
     * @param Application $app
141
     * @param $file
142
     *
143
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|void
144
     */
145
    public function getUserFile($app, $file)
146
    {
147
        try {
148
            $file = $app['chamilo.filesystem']->get('upload/users/'.$file);
149
150
            return $app->sendFile($file->getPathname());
151
        } catch (\InvalidArgumentException $e) {
152
            return $app->abort(404, 'File not found');
153
        }
154
    }
155
}
156