Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 93 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
ZipImporter | |
0.00% |
0 / 93 |
|
0.00% |
0 / 3 |
552 | |
0.00% |
0 / 1 |
import | |
0.00% |
0 / 50 |
|
0.00% |
0 / 1 |
90 | |||
createClass | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
extractArchive | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
156 |
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-2018 (original work) Open Assessment Technologies SA; |
19 | * |
20 | */ |
21 | |
22 | namespace oat\taoMediaManager\model; |
23 | |
24 | use common_report_Report as Report; |
25 | use helpers_TimeOutHelper; |
26 | use oat\oatbox\filesystem\File; |
27 | use oat\oatbox\log\LoggerAwareTrait; |
28 | use oat\oatbox\log\TaoLoggerAwareInterface; |
29 | use tao_helpers_form_Form as Form; |
30 | use core_kernel_classes_Class; |
31 | use oat\tao\model\import\ImportHandlerHelperTrait; |
32 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
33 | |
34 | /** |
35 | * Service methods to manage the Media |
36 | * |
37 | * @access public |
38 | * @author Antoine Robin, <antoine.robin@vesperiagroup.com> |
39 | * @package taoMediaManager |
40 | */ |
41 | class ZipImporter implements ServiceLocatorAwareInterface, TaoLoggerAwareInterface |
42 | { |
43 | use ImportHandlerHelperTrait; |
44 | use LoggerAwareTrait; |
45 | |
46 | protected $directoryMap = []; |
47 | |
48 | /** |
49 | * Starts the import based on the form values |
50 | * |
51 | * @param \core_kernel_classes_Class $class |
52 | * @param \tao_helpers_form_Form|array $form |
53 | * @param string|null $userId owner of the resource |
54 | * @return \common_report_Report |
55 | */ |
56 | public function import($class, $form, $userId = null) |
57 | { |
58 | try { |
59 | $uploadedFile = $this->fetchUploadedFile($form); |
60 | $resource = new core_kernel_classes_Class( |
61 | $form instanceof Form |
62 | ? $form->getValue('classUri') |
63 | : $form['classUri'] |
64 | ); |
65 | |
66 | // unzip the file |
67 | try { |
68 | $directory = $this->extractArchive($uploadedFile); |
69 | } catch (\Exception $e) { |
70 | $report = Report::createFailure(__('Unable to extract the archive')); |
71 | $report->setData(['uriResource' => '']); |
72 | |
73 | return $report; |
74 | } |
75 | |
76 | // get list of directory in order to create classes |
77 | $iterator = new \RecursiveIteratorIterator( |
78 | new \RecursiveDirectoryIterator( |
79 | $directory, |
80 | \RecursiveDirectoryIterator::CURRENT_AS_FILEINFO | \RecursiveDirectoryIterator::SKIP_DOTS |
81 | ), |
82 | \RecursiveIteratorIterator::LEAVES_ONLY |
83 | ); |
84 | |
85 | $service = MediaService::singleton(); |
86 | $language = $form instanceof Form ? $form->getValue('lang') : $form['lang']; |
87 | |
88 | $this->directoryMap = [ |
89 | rtrim($directory, DIRECTORY_SEPARATOR) => $resource->getUri() |
90 | ]; |
91 | $report = Report::createSuccess(__('Media imported successfully')); |
92 | |
93 | /** @var $file \SplFileInfo */ |
94 | foreach ($iterator as $file) { |
95 | if ($file->isFile()) { |
96 | $this->logInfo('File ' . $file->getPathname()); |
97 | if (isset($this->directoryMap[$file->getPath()])) { |
98 | $classUri = $this->directoryMap[$file->getPath()]; |
99 | } else { |
100 | $classUri = $this->createClass($file->getPath()); |
101 | } |
102 | |
103 | $mediaResourceUri = $service->createMediaInstance( |
104 | $file->getRealPath(), |
105 | $classUri, |
106 | $language, |
107 | $file->getFilename(), |
108 | null, |
109 | $userId |
110 | ); |
111 | $report->add(Report::createSuccess( |
112 | __('Imported %s', substr($file->getRealPath(), strlen($directory))), |
113 | // 'uriResource' key is needed by javascript in tao/views/templates/form/import.tpl |
114 | ['uriResource' => $mediaResourceUri] |
115 | )); |
116 | } |
117 | } |
118 | } catch (\Exception $e) { |
119 | $message = $e instanceof \common_exception_UserReadableException |
120 | ? $e->getUserMessage() |
121 | : __('An error has occurred. Please contact your administrator.'); |
122 | $report = Report::createFailure($message); |
123 | $this->logError($e->getMessage()); |
124 | $report->setData(['uriResource' => '']); |
125 | } |
126 | |
127 | return $report; |
128 | } |
129 | |
130 | /** |
131 | * @param $relPath |
132 | * @return string |
133 | * @throws \common_exception_Error |
134 | */ |
135 | protected function createClass($relPath) |
136 | { |
137 | $parentPath = dirname($relPath); |
138 | if (isset($this->directoryMap[$parentPath])) { |
139 | $parentUri = $this->directoryMap[$parentPath]; |
140 | } else { |
141 | $parentUri = $this->createClass($parentPath); |
142 | } |
143 | $parentClass = new \core_kernel_classes_Class($parentUri); |
144 | $childClazz = MediaService::singleton()->createSubClass($parentClass, basename($relPath)); |
145 | $this->directoryMap[$relPath] = $childClazz->getUri(); |
146 | |
147 | return $childClazz->getUri(); |
148 | } |
149 | |
150 | /** |
151 | * Unzip archive from the upload form |
152 | * |
153 | * @param string|File $archiveFile |
154 | * @return string temporary directory zipfile was extracted to |
155 | * |
156 | * @throws \common_Exception |
157 | */ |
158 | protected function extractArchive($archiveFile) |
159 | { |
160 | helpers_TimeOutHelper::setTimeOutLimit(helpers_TimeOutHelper::LONG); |
161 | |
162 | if ($archiveFile instanceof File) { |
163 | if (!$archiveFile->exists()) { |
164 | throw new \common_Exception('Unable to open archive ' . '/' . $archiveFile->getPrefix()); |
165 | } |
166 | $tmpDir = \tao_helpers_File::createTempDir(); |
167 | $tmpFilePath = $tmpDir . uniqid('sharedStimulus-import') . '.zip'; |
168 | $tmpFile = fopen($tmpFilePath, 'w'); |
169 | $originalPackage = $archiveFile->readStream(); |
170 | stream_copy_to_stream($originalPackage, $tmpFile); |
171 | fclose($originalPackage); |
172 | fclose($tmpFile); |
173 | |
174 | $archiveFile = $tmpFilePath; |
175 | } |
176 | |
177 | $archiveDir = \tao_helpers_File::createTempDir(); |
178 | $archiveObj = new \ZipArchive(); |
179 | |
180 | if ($archiveFile instanceof File) { |
181 | // get a local copy of zip |
182 | $tmpName = \tao_helpers_File::concat([\tao_helpers_File::createTempDir(), $archiveFile->getBasename()]); |
183 | if (($resource = fopen($tmpName, 'wb')) !== false) { |
184 | stream_copy_to_stream($archiveFile->readStream(), $resource); |
185 | fclose($resource); |
186 | } |
187 | |
188 | $archiveFile = $tmpName; |
189 | } |
190 | |
191 | $archiveHandle = $archiveObj->open($archiveFile); |
192 | if (true !== $archiveHandle) { |
193 | throw new \common_Exception('Unable to open archive ' . $archiveFile); |
194 | } |
195 | if (\tao_helpers_File::checkWhetherArchiveIsBomb($archiveObj)) { |
196 | throw new \common_Exception(sprintf('Source "%s" seems to be a ZIP bomb', $archiveFile)); |
197 | } |
198 | if (!$archiveObj->extractTo($archiveDir)) { |
199 | $archiveObj->close(); |
200 | throw new \common_Exception('Unable to extract to ' . $archiveDir); |
201 | } |
202 | $archiveObj->close(); |
203 | |
204 | if (isset($tmpFilePath) && file_exists($tmpFilePath)) { |
205 | unlink($tmpFilePath); |
206 | } |
207 | if (isset($tmpDir) && file_exists($tmpDir)) { |
208 | rmdir($tmpDir); |
209 | } |
210 | |
211 | helpers_TimeOutHelper::reset(); |
212 | |
213 | return $archiveDir; |
214 | } |
215 | } |