Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 76 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
MediaManager | |
0.00% |
0 / 76 |
|
0.00% |
0 / 12 |
552 | |
0.00% |
0 / 1 |
editInstance | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
42 | |||
getFile | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
30 | |||
delete | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
moveResource | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
editClassLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
authoring | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getClassService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getRequestedMediaUri | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getFormInstance | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
getMediaService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPermissionService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDependsOnPropertyValidator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2014-2021 (original work) Open Assessment Technologies SA; |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoMediaManager\controller; |
24 | |
25 | use oat\tao\model\http\ContentDetector; |
26 | use oat\oatbox\user\User; |
27 | use oat\oatbox\validator\ValidatorInterface; |
28 | use oat\taoMediaManager\model\editInstanceForm; |
29 | use oat\taoMediaManager\model\MediaService; |
30 | use oat\taoMediaManager\model\MediaSource; |
31 | use oat\taoMediaManager\model\accessControl\MediaPermissionService; |
32 | use oat\taoMediaManager\model\fileManagement\FileManagement; |
33 | use oat\tao\model\Lists\Business\Validation\DependsOnPropertyValidator; |
34 | use core_kernel_classes_Resource; |
35 | use tao_actions_SaSModule; |
36 | use tao_helpers_form_FormContainer as FormContainer; |
37 | use tao_helpers_Uri; |
38 | use tao_models_classes_FileNotFoundException; |
39 | use tao_models_classes_dataBinding_GenerisFormDataBinder; |
40 | |
41 | class MediaManager extends tao_actions_SaSModule |
42 | { |
43 | /** |
44 | * Show the form to edit an instance, show also a preview of the media |
45 | * |
46 | * @requiresRight id READ |
47 | */ |
48 | public function editInstance() |
49 | { |
50 | $this->defaultData(); |
51 | |
52 | $user = $this->getSession()->getUser(); |
53 | $permissionService = $this->getPermissionService(); |
54 | |
55 | $resource = $this->getCurrentInstance(); |
56 | $editFormContainer = $this->getFormInstance($resource, $user); |
57 | $editForm = $editFormContainer->getForm(); |
58 | |
59 | if ( |
60 | $permissionService->isAllowedToEditResource($resource, $user) |
61 | && $editForm->isSubmited() |
62 | && $editForm->isValid() |
63 | ) { |
64 | $binder = new tao_models_classes_dataBinding_GenerisFormDataBinder($resource); |
65 | $binder->bind($editForm->getValues()); |
66 | |
67 | $this->setData('message', __('Instance saved')); |
68 | $this->setData('reload', true); |
69 | } |
70 | |
71 | $this->setData('isPreviewEnabled', $permissionService->isAllowedToPreview()); |
72 | $this->setData('formTitle', __('Edit Instance')); |
73 | $this->setData('myForm', $editForm->render()); |
74 | |
75 | $uri = $this->getRequestedMediaUri(); |
76 | $url = tao_helpers_Uri::url( |
77 | 'getFile', |
78 | 'MediaManager', |
79 | 'taoMediaManager', |
80 | [ |
81 | 'uri' => $uri, |
82 | ] |
83 | ); |
84 | |
85 | $this->setData('fileurl', $url); |
86 | |
87 | try { |
88 | $fileInfo = (new MediaSource())->getFileInfo($uri); |
89 | $mimeType = $fileInfo['mime']; |
90 | } catch (tao_models_classes_FileNotFoundException $e) { |
91 | $this->setData('error', __('No file found for this media')); |
92 | } |
93 | |
94 | $this->setData('xml', isset($mimeType) ? $this->getClassService()->isXmlAllowedMimeType($mimeType) : null); |
95 | $this->setData('mimeType', $mimeType ?? null); |
96 | $this->setView('form.tpl'); |
97 | } |
98 | |
99 | /** |
100 | * Get the file stream associated to given uri GET parameter |
101 | * |
102 | * @throws \common_exception_Error |
103 | * @throws tao_models_classes_FileNotFoundException |
104 | */ |
105 | public function getFile() |
106 | { |
107 | if (!$this->hasGetParameter('uri')) { |
108 | throw new \common_exception_Error('invalid media identifier'); |
109 | } |
110 | |
111 | $uri = urldecode($this->getGetParameter('uri')); |
112 | |
113 | $mediaSource = new MediaSource([]); |
114 | $fileInfo = $mediaSource->getFileInfo($uri); |
115 | |
116 | $fileManagement = $this->getServiceLocator()->get(FileManagement::SERVICE_ID); |
117 | $stream = $fileManagement->getFileStream($fileInfo['link']); |
118 | |
119 | if ($fileInfo['mime'] === MediaService::SHARED_STIMULUS_MIME_TYPE) { |
120 | $this->response = $this->getPsrResponse()->withBody($stream); |
121 | } elseif ($this->hasGetParameter('xml')) { |
122 | $this->returnJson(htmlentities((string)$stream)); |
123 | } else { |
124 | $this->setContentHeader($fileInfo['mime']); |
125 | if ($this->getServiceLocator()->get(ContentDetector::class)->isGzip($stream)) { |
126 | $this->response = $this->getPsrResponse()->withHeader('Content-Encoding', 'gzip'); |
127 | } |
128 | $this->response = $this->getPsrResponse()->withBody($stream); |
129 | } |
130 | } |
131 | |
132 | /** |
133 | * @inheritDoc |
134 | * |
135 | * @requiresRight id WRITE |
136 | */ |
137 | public function delete() |
138 | { |
139 | return parent::delete(); |
140 | } |
141 | |
142 | /** |
143 | * overwrite the parent moveAllInstances to add the requiresRight only in Items |
144 | * @see tao_actions_TaoModule::moveResource() |
145 | */ |
146 | public function moveResource() |
147 | { |
148 | return parent::moveResource(); |
149 | } |
150 | |
151 | /** |
152 | * @requiresRight id READ |
153 | */ |
154 | public function editClassLabel() |
155 | { |
156 | parent::editClassLabel(); |
157 | } |
158 | |
159 | /** |
160 | * @requiresRight id WRITE |
161 | */ |
162 | public function authoring() |
163 | { |
164 | //This method is required to hide button on FE based on ACL |
165 | } |
166 | |
167 | protected function getClassService() |
168 | { |
169 | return $this->getMediaService(); |
170 | } |
171 | |
172 | private function getRequestedMediaUri(): string |
173 | { |
174 | if ($this->hasRequestParameter('id')) { |
175 | return $this->getRequest()->getParameter('id'); |
176 | } |
177 | |
178 | return $this->getRequest()->getParameter('uri'); |
179 | } |
180 | |
181 | private function getFormInstance( |
182 | core_kernel_classes_Resource $instance, |
183 | User $user |
184 | ): editInstanceForm { |
185 | $permissionService = $this->getPermissionService(); |
186 | $editAllowed = $permissionService->isAllowedToEditResource($instance, $user); |
187 | $canReplaceMedia = $editAllowed && $permissionService->isAllowedToEditMedia(); |
188 | |
189 | return new editInstanceForm( |
190 | $this->getCurrentClass(), |
191 | $instance, |
192 | [ |
193 | FormContainer::CSRF_PROTECTION_OPTION => true, |
194 | FormContainer::IS_DISABLED => !$editAllowed, |
195 | editInstanceForm::IS_REPLACE_ASSET_DISABLED => !$canReplaceMedia, |
196 | FormContainer::ATTRIBUTE_VALIDATORS => [ |
197 | 'data-depends-on-property' => [ |
198 | $this->getDependsOnPropertyValidator(), |
199 | ], |
200 | ], |
201 | ] |
202 | ); |
203 | } |
204 | |
205 | private function getMediaService(): MediaService |
206 | { |
207 | return $this->getPsrContainer()->get(MediaService::class); |
208 | } |
209 | |
210 | private function getPermissionService(): MediaPermissionService |
211 | { |
212 | return $this->getPsrContainer()->get(MediaPermissionService::class); |
213 | } |
214 | |
215 | private function getDependsOnPropertyValidator(): ValidatorInterface |
216 | { |
217 | return $this->getPsrContainer()->get(DependsOnPropertyValidator::class); |
218 | } |
219 | } |