Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 82 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
MediaManager | |
0.00% |
0 / 82 |
|
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 / 23 |
|
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 oat\taoMediaManager\model\TaoMediaOntology; |
36 | use oat\taoMediaManager\model\transcription\TranscriptionMimeTypesProvider; |
37 | use tao_actions_form_Instance; |
38 | use tao_actions_SaSModule; |
39 | use tao_helpers_form_FormContainer as FormContainer; |
40 | use tao_helpers_Uri; |
41 | use tao_models_classes_FileNotFoundException; |
42 | use tao_models_classes_dataBinding_GenerisFormDataBinder; |
43 | |
44 | class MediaManager extends tao_actions_SaSModule |
45 | { |
46 | /** |
47 | * Show the form to edit an instance, show also a preview of the media |
48 | * |
49 | * @requiresRight id READ |
50 | */ |
51 | public function editInstance() |
52 | { |
53 | $this->defaultData(); |
54 | |
55 | $user = $this->getSession()->getUser(); |
56 | $permissionService = $this->getPermissionService(); |
57 | |
58 | $resource = $this->getCurrentInstance(); |
59 | $editFormContainer = $this->getFormInstance($resource, $user); |
60 | $editForm = $editFormContainer->getForm(); |
61 | |
62 | if ( |
63 | $permissionService->isAllowedToEditResource($resource, $user) |
64 | && $editForm->isSubmited() |
65 | && $editForm->isValid() |
66 | ) { |
67 | $binder = new tao_models_classes_dataBinding_GenerisFormDataBinder($resource); |
68 | $binder->bind($editForm->getValues()); |
69 | |
70 | $this->setData('message', __('Instance saved')); |
71 | $this->setData('reload', true); |
72 | } |
73 | |
74 | $this->setData('isPreviewEnabled', $permissionService->isAllowedToPreview()); |
75 | $this->setData('formTitle', __('Edit Instance')); |
76 | $this->setData('myForm', $editForm->render()); |
77 | |
78 | $uri = $this->getRequestedMediaUri(); |
79 | $url = tao_helpers_Uri::url( |
80 | 'getFile', |
81 | 'MediaManager', |
82 | 'taoMediaManager', |
83 | [ |
84 | 'uri' => $uri, |
85 | ] |
86 | ); |
87 | |
88 | $this->setData('fileurl', $url); |
89 | |
90 | try { |
91 | $fileInfo = (new MediaSource())->getFileInfo($uri); |
92 | $mimeType = $fileInfo['mime']; |
93 | } catch (tao_models_classes_FileNotFoundException $e) { |
94 | $this->setData('error', __('No file found for this media')); |
95 | } |
96 | |
97 | $this->setData('xml', isset($mimeType) ? $this->getClassService()->isXmlAllowedMimeType($mimeType) : null); |
98 | $this->setData('mimeType', $mimeType ?? null); |
99 | $this->setView('form.tpl'); |
100 | } |
101 | |
102 | /** |
103 | * Get the file stream associated to given uri GET parameter |
104 | * |
105 | * @throws \common_exception_Error |
106 | * @throws tao_models_classes_FileNotFoundException |
107 | */ |
108 | public function getFile() |
109 | { |
110 | if (!$this->hasGetParameter('uri')) { |
111 | throw new \common_exception_Error('invalid media identifier'); |
112 | } |
113 | |
114 | $uri = urldecode($this->getGetParameter('uri')); |
115 | |
116 | $mediaSource = new MediaSource([]); |
117 | $fileInfo = $mediaSource->getFileInfo($uri); |
118 | |
119 | $fileManagement = $this->getServiceLocator()->get(FileManagement::SERVICE_ID); |
120 | $stream = $fileManagement->getFileStream($fileInfo['link']); |
121 | |
122 | if ($fileInfo['mime'] === MediaService::SHARED_STIMULUS_MIME_TYPE) { |
123 | $this->response = $this->getPsrResponse()->withBody($stream); |
124 | } elseif ($this->hasGetParameter('xml')) { |
125 | $this->returnJson(htmlentities((string)$stream)); |
126 | } else { |
127 | $this->setContentHeader($fileInfo['mime']); |
128 | if ($this->getServiceLocator()->get(ContentDetector::class)->isGzip($stream)) { |
129 | $this->response = $this->getPsrResponse()->withHeader('Content-Encoding', 'gzip'); |
130 | } |
131 | $this->response = $this->getPsrResponse()->withBody($stream); |
132 | } |
133 | } |
134 | |
135 | /** |
136 | * @inheritDoc |
137 | * |
138 | * @requiresRight id WRITE |
139 | */ |
140 | public function delete() |
141 | { |
142 | return parent::delete(); |
143 | } |
144 | |
145 | /** |
146 | * overwrite the parent moveAllInstances to add the requiresRight only in Items |
147 | * @see tao_actions_TaoModule::moveResource() |
148 | */ |
149 | public function moveResource() |
150 | { |
151 | return parent::moveResource(); |
152 | } |
153 | |
154 | /** |
155 | * @requiresRight id READ |
156 | */ |
157 | public function editClassLabel() |
158 | { |
159 | parent::editClassLabel(); |
160 | } |
161 | |
162 | /** |
163 | * @requiresRight id WRITE |
164 | */ |
165 | public function authoring() |
166 | { |
167 | //This method is required to hide button on FE based on ACL |
168 | } |
169 | |
170 | protected function getClassService() |
171 | { |
172 | return $this->getMediaService(); |
173 | } |
174 | |
175 | private function getRequestedMediaUri(): string |
176 | { |
177 | if ($this->hasRequestParameter('id')) { |
178 | return $this->getRequest()->getParameter('id'); |
179 | } |
180 | |
181 | return $this->getRequest()->getParameter('uri'); |
182 | } |
183 | |
184 | private function getFormInstance( |
185 | core_kernel_classes_Resource $instance, |
186 | User $user |
187 | ): editInstanceForm { |
188 | $permissionService = $this->getPermissionService(); |
189 | $editAllowed = $permissionService->isAllowedToEditResource($instance, $user); |
190 | $canReplaceMedia = $editAllowed && $permissionService->isAllowedToEditMedia(); |
191 | |
192 | return new editInstanceForm( |
193 | $this->getCurrentClass(), |
194 | $instance, |
195 | [ |
196 | FormContainer::CSRF_PROTECTION_OPTION => true, |
197 | FormContainer::IS_DISABLED => !$editAllowed, |
198 | editInstanceForm::IS_REPLACE_ASSET_DISABLED => !$canReplaceMedia, |
199 | FormContainer::ATTRIBUTE_VALIDATORS => [ |
200 | 'data-depends-on-property' => [ |
201 | $this->getDependsOnPropertyValidator(), |
202 | ], |
203 | ], |
204 | tao_actions_form_Instance::RESTRICTED_PROPERTIES => [ |
205 | TaoMediaOntology::PROPERTY_TRANSCRIPTION => [ |
206 | TaoMediaOntology::PROPERTY_MIME_TYPE => |
207 | $this->getPsrContainer()->get(TranscriptionMimeTypesProvider::class)->getAll(), |
208 | ] |
209 | ] |
210 | ] |
211 | ); |
212 | } |
213 | |
214 | private function getMediaService(): MediaService |
215 | { |
216 | return $this->getPsrContainer()->get(MediaService::class); |
217 | } |
218 | |
219 | private function getPermissionService(): MediaPermissionService |
220 | { |
221 | return $this->getPsrContainer()->get(MediaPermissionService::class); |
222 | } |
223 | |
224 | private function getDependsOnPropertyValidator(): ValidatorInterface |
225 | { |
226 | return $this->getPsrContainer()->get(DependsOnPropertyValidator::class); |
227 | } |
228 | } |