Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
11.76% |
10 / 85 |
|
21.43% |
3 / 14 |
CRAP | |
0.00% |
0 / 1 |
UploadService | |
11.76% |
10 / 85 |
|
21.43% |
3 / 14 |
926.29 | |
0.00% |
0 / 1 |
uploadFile | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
30 | |||
fetchUploadedFile | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
6 | |||
getUploadFSid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUploadDir | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSerializer | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
universalizeUpload | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
20 | |||
getUploadedFile | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getUploadedFlyFile | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
getLocalCopy | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
listenUploadEvent | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
listenLocalCopyEvent | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
remove | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
getUserDirectoryHash | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
isUploadedFile | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 |
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) 2016 (original work) Open Assessment Technologies SA |
19 | * |
20 | */ |
21 | |
22 | namespace oat\tao\model\upload; |
23 | |
24 | use common_exception_Error; |
25 | use oat\generis\model\fileReference\UrlFileSerializer; |
26 | use oat\oatbox\event\EventManager; |
27 | use oat\oatbox\filesystem\Directory; |
28 | use oat\oatbox\filesystem\File; |
29 | use oat\oatbox\filesystem\FileSystemService; |
30 | use oat\oatbox\service\ConfigurableService; |
31 | use oat\tao\model\event\FileUploadedEvent; |
32 | use oat\tao\model\event\UploadLocalCopyCreatedEvent; |
33 | use tao_helpers_File; |
34 | use tao_helpers_form_Form; |
35 | |
36 | class UploadService extends ConfigurableService |
37 | { |
38 | public const SERVICE_ID = 'tao/upload'; |
39 | |
40 | public static $tmpFilesystemId = 'sharedTmp'; |
41 | private $uriSerializer; |
42 | |
43 | |
44 | /** |
45 | * @param array $postedFile |
46 | * @param string $folder |
47 | * @return array |
48 | * @throws \InvalidArgumentException |
49 | * @throws \common_Exception |
50 | */ |
51 | public function uploadFile(array $postedFile, $folder) |
52 | { |
53 | $tmp_name = array_key_exists('tmp_name', $postedFile) ? $postedFile['tmp_name'] : null; |
54 | |
55 | if (!$tmp_name) { |
56 | throw new \InvalidArgumentException('Upload filename is missing'); |
57 | } |
58 | $name = array_key_exists('name', $postedFile) ? $postedFile['name'] : uniqid('unknown_', false); |
59 | $extension = pathinfo($name, PATHINFO_EXTENSION); |
60 | |
61 | $targetName = uniqid('tmp', true) . '.' . $extension; |
62 | $targetLocation = tao_helpers_File::concat([$folder, $targetName]); |
63 | |
64 | $file = $this->getUploadDir()->getFile($this->getUserDirectoryHash() . $targetLocation); |
65 | |
66 | $returnValue['uploaded'] = $file->put(fopen($tmp_name, 'rb')); |
67 | $this->getServiceManager()->get(EventManager::CONFIG_ID)->trigger(new FileUploadedEvent($file)); |
68 | tao_helpers_File::remove($tmp_name); |
69 | |
70 | $data['type'] = $file->getMimetype(); |
71 | $data['uploaded_file'] = $file->getBasename(); |
72 | $data['name'] = $name; |
73 | $data['size'] = array_key_exists('size', $postedFile) ? $postedFile['size'] : $file->getSize(); |
74 | $returnValue['name'] = $name; |
75 | $returnValue['uploaded_file'] = $data['uploaded_file']; |
76 | $returnValue['data'] = json_encode($data); |
77 | |
78 | return $returnValue; |
79 | } |
80 | |
81 | /** |
82 | * Helps to get the uploaded file data during upload or during processing of the import task. |
83 | * |
84 | * @param array|tao_helpers_form_Form $form |
85 | * @return File|string |
86 | * @throws common_exception_Error |
87 | */ |
88 | public function fetchUploadedFile($form) |
89 | { |
90 | if (is_array($form) && isset($form['uploaded_file'])) { |
91 | return $this->getUploadDir()->getFile($form['uploaded_file']); |
92 | } |
93 | |
94 | if ($form instanceof tao_helpers_form_Form) { |
95 | $fileInfo = $form->getValue('source'); |
96 | |
97 | /** @var string $file */ |
98 | $file = $form->getValue('importFile') ?: $fileInfo['uploaded_file']; |
99 | |
100 | if ($file) { |
101 | return $file; |
102 | } |
103 | } |
104 | |
105 | throw new common_exception_Error('No source file for import'); |
106 | } |
107 | |
108 | /** |
109 | * Returns the file system identifier. |
110 | * |
111 | * @return string |
112 | */ |
113 | public function getUploadFSid() |
114 | { |
115 | return self::$tmpFilesystemId; |
116 | } |
117 | |
118 | /** |
119 | * Returns the current file system directory instance. |
120 | * |
121 | * @return Directory |
122 | */ |
123 | public function getUploadDir() |
124 | { |
125 | return $this->getServiceLocator()->get(FileSystemService::SERVICE_ID)->getDirectory($this->getUploadFSid()); |
126 | } |
127 | /** |
128 | * |
129 | * @return \oat\generis\model\fileReference\UrlFileSerializer |
130 | */ |
131 | public function getSerializer() |
132 | { |
133 | if (!$this->uriSerializer) { |
134 | $this->uriSerializer = new UrlFileSerializer(); |
135 | $this->uriSerializer->setServiceLocator($this->getServiceLocator()); |
136 | } |
137 | return $this->uriSerializer; |
138 | } |
139 | |
140 | /** |
141 | * Detects |
142 | * @param $file |
143 | * @return File|string |
144 | * @deprecated |
145 | * @throws \common_Exception |
146 | */ |
147 | public function universalizeUpload($file) |
148 | { |
149 | if ((is_string($file) && is_file($file)) || $file instanceof File) { |
150 | return $file; |
151 | } |
152 | |
153 | return $this->getUploadedFlyFile($file); |
154 | } |
155 | |
156 | /** |
157 | * Either create local copy or use original location for tile |
158 | * Returns absolute path to the file, to be compatible with legacy methods |
159 | * @deprecated |
160 | * @param string|File $serial |
161 | * @return string |
162 | * @throws \common_Exception |
163 | */ |
164 | public function getUploadedFile($serial) |
165 | { |
166 | $file = $this->universalizeUpload($serial); |
167 | if ($file instanceof File) { |
168 | $file = $this->getLocalCopy($file); |
169 | } |
170 | return $file; |
171 | } |
172 | |
173 | /** |
174 | * @param string $serial |
175 | * |
176 | * @throws \common_exception_NotAcceptable When the uploaded file url contains a wrong system id. |
177 | * |
178 | * @return File |
179 | */ |
180 | public function getUploadedFlyFile($serial) |
181 | { |
182 | $path = $this->getUserDirectoryHash() . '/'; |
183 | if (filter_var($serial, FILTER_VALIDATE_URL)) { |
184 | // Getting the File instance of the file url. |
185 | $fakeFile = $this->getSerializer()->unserializeFile($serial); |
186 | |
187 | // Filesystem hack check. |
188 | if ($fakeFile->getFileSystemId() !== $this->getUploadFSid()) { |
189 | throw new \common_exception_NotAcceptable( |
190 | 'The uploaded file url contains a wrong filesystem id!' . |
191 | '(Expected: `' . $this->getUploadFSid() . '` Actual: `' . $fakeFile->getFileSystemId() . '`)' |
192 | ); |
193 | } |
194 | |
195 | $path .= $fakeFile->getBasename(); |
196 | } else { |
197 | $path .= $serial; |
198 | } |
199 | |
200 | $realFile = $this->getUploadDir()->getFile($path); |
201 | if ($realFile->exists()) { |
202 | return $realFile; |
203 | } |
204 | |
205 | return null; |
206 | } |
207 | |
208 | /** |
209 | * Deprecated, for compatibility with legacy code |
210 | * @param $file |
211 | * @return string |
212 | * @throws \common_Exception |
213 | */ |
214 | private function getLocalCopy(File $file) |
215 | { |
216 | $tmpName = \tao_helpers_File::concat([\tao_helpers_File::createTempDir(), $file->getBasename()]); |
217 | if (($resource = fopen($tmpName, 'wb')) !== false) { |
218 | stream_copy_to_stream($file->readStream(), $resource); |
219 | fclose($resource); |
220 | $this->getServiceLocator()->get(EventManager::CONFIG_ID)->trigger(new UploadLocalCopyCreatedEvent( |
221 | $file, |
222 | $tmpName |
223 | )); |
224 | return $tmpName; |
225 | } |
226 | throw new \common_Exception('Impossible to make local file copy at ' . $tmpName); |
227 | } |
228 | |
229 | /** |
230 | * @param FileUploadedEvent $event |
231 | */ |
232 | public function listenUploadEvent(FileUploadedEvent $event) |
233 | { |
234 | $storage = new TempFlyStorageAssociation($this->getServiceLocator()); |
235 | $storage->setUpload($event->getFile()); |
236 | } |
237 | |
238 | /** |
239 | * @param UploadLocalCopyCreatedEvent $event |
240 | */ |
241 | public function listenLocalCopyEvent(UploadLocalCopyCreatedEvent $event) |
242 | { |
243 | $storage = new TempFlyStorageAssociation($this->getServiceLocator()); |
244 | $storage->addLocalCopies($event->getFile(), $event->getTmpPath()); |
245 | } |
246 | |
247 | public function remove($file) |
248 | { |
249 | $storage = new TempFlyStorageAssociation($this->getServiceLocator()); |
250 | |
251 | if ($file instanceof File) { |
252 | $storedLocalTmps = $storage->getLocalCopies($file); |
253 | foreach ((array)$storedLocalTmps as $tmp) { |
254 | tao_helpers_File::remove($tmp); |
255 | } |
256 | $storage->removeFiles($file); |
257 | $file->delete(); |
258 | } |
259 | } |
260 | |
261 | /** |
262 | * Returns the username directory hash. |
263 | * |
264 | * @return string |
265 | */ |
266 | public function getUserDirectoryHash() |
267 | { |
268 | return hash( |
269 | 'crc32b', |
270 | \common_session_SessionManager::getSession()->getUser()->getIdentifier() |
271 | ); |
272 | } |
273 | |
274 | /** |
275 | * Is the uploaded filename looks as an uploaded file. |
276 | * |
277 | * @param $filePath |
278 | * |
279 | * @return bool |
280 | */ |
281 | public function isUploadedFile($filePath) |
282 | { |
283 | // If it's a serialized one. |
284 | if (filter_var($filePath, FILTER_VALIDATE_URL)) { |
285 | return true; |
286 | } |
287 | |
288 | // If it's a normal filename. |
289 | $file = $this->getUploadDir()->getFile($this->getUserDirectoryHash() . '/' . $filePath); |
290 | if ($file->exists()) { |
291 | return true; |
292 | } |
293 | |
294 | return false; |
295 | } |
296 | } |