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