Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
2.88% |
3 / 104 |
|
13.33% |
2 / 15 |
CRAP | |
0.00% |
0 / 1 |
| LocalItemSource | |
2.88% |
3 / 104 |
|
13.33% |
2 / 15 |
1290.91 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| getItem | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDirectories | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getDirectory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFileInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getInfoFromSource | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| getInfoFromFile | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| download | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| getBaseName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| add | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
| delete | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFileStream | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFile | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getItemDirectory | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| searchDirectories | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
132 | |||
| 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 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoItems\model\media; |
| 24 | |
| 25 | use common_exception_Error; |
| 26 | use oat\oatbox\filesystem\Directory; |
| 27 | use oat\oatbox\filesystem\File; |
| 28 | use oat\tao\model\media\MediaManagement; |
| 29 | use oat\tao\model\media\mediaSource\DirectorySearchQuery; |
| 30 | use Psr\Http\Message\StreamInterface; |
| 31 | use tao_helpers_File; |
| 32 | use taoItems_models_classes_ItemsService; |
| 33 | |
| 34 | /** |
| 35 | * This media source gives access to files that are part of the item |
| 36 | * and are addressed in a relative way |
| 37 | */ |
| 38 | class LocalItemSource implements MediaManagement |
| 39 | { |
| 40 | private $item; |
| 41 | |
| 42 | private $lang; |
| 43 | |
| 44 | protected $itemService; |
| 45 | |
| 46 | public function __construct($data) |
| 47 | { |
| 48 | $this->item = (isset($data['item'])) ? $data['item'] : null; |
| 49 | $this->lang = (isset($data['lang'])) ? $data['lang'] : ''; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @return \core_kernel_classes_Resource |
| 54 | */ |
| 55 | public function getItem() |
| 56 | { |
| 57 | return $this->item; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @throws \common_Exception |
| 62 | * @throws \tao_models_classes_FileNotFoundException |
| 63 | * @throws common_exception_Error |
| 64 | */ |
| 65 | public function getDirectories(DirectorySearchQuery $params): array |
| 66 | { |
| 67 | return $this->searchDirectories( |
| 68 | $params->getParentLink(), |
| 69 | $params->getFilter(), |
| 70 | $params->getDepth(), |
| 71 | $params->getChildrenLimit() |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get a array representing the tree of directory |
| 77 | * @deprecated in favor of MediaBrowser::getDirectories |
| 78 | * @throws \common_Exception |
| 79 | * @throws \tao_models_classes_FileNotFoundException |
| 80 | * @throws common_exception_Error |
| 81 | */ |
| 82 | public function getDirectory($parentLink = '', $acceptableMime = [], $depth = 1) |
| 83 | { |
| 84 | return $this->searchDirectories($parentLink, $acceptableMime, $depth, 0); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Return file info regarding a file |
| 89 | * |
| 90 | * @see \oat\tao\model\media\MediaBrowser::getFileInfo |
| 91 | * @param string $link |
| 92 | * @return array |
| 93 | * @throws \tao_models_classes_FileNotFoundException |
| 94 | * @throws common_exception_Error |
| 95 | */ |
| 96 | public function getFileInfo($link) |
| 97 | { |
| 98 | return $this->getInfoFromFile($this->getFile($link)); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Method should be used only after uploading local file, when metadata like file size and mime type |
| 103 | * of remote uploaded file are not critical. To get more precise info about remote file use getInfoFromFile(). |
| 104 | * |
| 105 | * In case of using S3 Flysystem adapter there might be latency and file cannot be accessed right after upload. |
| 106 | * |
| 107 | * @param File $file |
| 108 | * @param string $sourceFile |
| 109 | * @return array |
| 110 | * @throws \common_Exception |
| 111 | * @throws \tao_models_classes_FileNotFoundException |
| 112 | */ |
| 113 | protected function getInfoFromSource(File $file, string $sourceFile): array |
| 114 | { |
| 115 | if (file_exists($sourceFile)) { |
| 116 | $link = $this->getItemDirectory()->getRelPath($file); |
| 117 | return [ |
| 118 | 'name' => $file->getBasename(), |
| 119 | 'uri' => $link, |
| 120 | 'mime' => tao_helpers_File::getMimeType($sourceFile), |
| 121 | 'filePath' => $link, |
| 122 | 'size' => filesize($sourceFile), |
| 123 | ]; |
| 124 | } else { |
| 125 | return $this->getInfoFromFile($file); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | protected function getInfoFromFile(File $file) |
| 130 | { |
| 131 | $link = $this->getItemDirectory()->getRelPath($file); |
| 132 | return [ |
| 133 | 'name' => $file->getBasename(), |
| 134 | 'uri' => $link, |
| 135 | 'mime' => $file->getMimeType(), |
| 136 | 'filePath' => $link, |
| 137 | 'size' => $file->getSize(), |
| 138 | ]; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Copy file content to temp file. Path is return to be downloaded |
| 143 | * |
| 144 | * @see \oat\tao\model\media\MediaBrowser::download |
| 145 | * @param string $link |
| 146 | * @return string |
| 147 | * @throws \common_Exception |
| 148 | * @throws \tao_models_classes_FileNotFoundException |
| 149 | * @throws common_exception_Error |
| 150 | */ |
| 151 | public function download($link) |
| 152 | { |
| 153 | $file = $this->getFile($link); |
| 154 | |
| 155 | $relPath = $this->getItemDirectory()->getRelPath($file); |
| 156 | |
| 157 | $tmpDir = rtrim(\tao_helpers_File::createTempDir(), '/'); |
| 158 | $tmpFile = $tmpDir . $relPath; |
| 159 | if (! is_dir(dirname($tmpFile))) { |
| 160 | mkdir(dirname($tmpFile), 0755, true); |
| 161 | } |
| 162 | |
| 163 | if (($resource = fopen($tmpFile, 'w')) !== false) { |
| 164 | stream_copy_to_stream($file->readStream(), $resource); |
| 165 | fclose($resource); |
| 166 | return $tmpFile; |
| 167 | } |
| 168 | |
| 169 | throw new \common_Exception('Unable to write "' . $link . '" into tmp folder("' . $tmpFile . '").'); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Return filename of given path |
| 174 | * |
| 175 | * @see \oat\tao\model\media\MediaBrowser::getBaseName |
| 176 | * @param string $link |
| 177 | * @return string |
| 178 | */ |
| 179 | public function getBaseName($link) |
| 180 | { |
| 181 | return basename($link); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Add content to file |
| 186 | * |
| 187 | * @see \oat\tao\model\media\MediaManagement::add |
| 188 | * @param string $source |
| 189 | * @param string $fileName |
| 190 | * @param string $parent |
| 191 | * @return array |
| 192 | * @throws \common_Exception |
| 193 | * @throws \tao_models_classes_FileNotFoundException |
| 194 | */ |
| 195 | public function add($source, $fileName, $parent) |
| 196 | { |
| 197 | if (! \tao_helpers_File::securityCheck($fileName, true)) { |
| 198 | throw new \common_Exception('Unsecured filename "' . $fileName . '"'); |
| 199 | } |
| 200 | |
| 201 | if (($resource = fopen($source, 'r')) === false) { |
| 202 | throw new \common_Exception('Unable to read content of file ("' . $source . '")'); |
| 203 | } |
| 204 | |
| 205 | $file = $this->getItemDirectory()->getDirectory($parent)->getFile($fileName); |
| 206 | $writeSuccess = $file->put($resource); |
| 207 | if (is_resource($resource)) { |
| 208 | fclose($resource); |
| 209 | } |
| 210 | if (! $writeSuccess) { |
| 211 | throw new \common_Exception('Unable to write file ("' . $fileName . '")'); |
| 212 | } |
| 213 | |
| 214 | return $this->getInfoFromSource($file, $source); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Delete the file located at $link |
| 219 | * |
| 220 | * @see \oat\tao\model\media\MediaManagement::delete |
| 221 | * @param $link |
| 222 | * @return bool |
| 223 | * @throws \tao_models_classes_FileNotFoundException |
| 224 | * @throws common_exception_Error |
| 225 | */ |
| 226 | public function delete($link) |
| 227 | { |
| 228 | return $this->getFile($link)->delete(); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Return file stream of file located at $link |
| 233 | * |
| 234 | * @see tao/models/classes/media/MediaBrowser.php:getFileStream |
| 235 | * @param string $link |
| 236 | * @return StreamInterface |
| 237 | * @throws \tao_models_classes_FileNotFoundException |
| 238 | * @throws common_exception_Error |
| 239 | */ |
| 240 | public function getFileStream($link) |
| 241 | { |
| 242 | return $this->getFile($link)->readPsrStream(); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Get file object associated to $link, search in main item directory or specific item directory |
| 247 | * |
| 248 | * @param $link |
| 249 | * @return File |
| 250 | * @throws \common_Exception |
| 251 | * @throws \tao_models_classes_FileNotFoundException |
| 252 | * @throws common_exception_Error |
| 253 | */ |
| 254 | private function getFile($link) |
| 255 | { |
| 256 | if (!tao_helpers_File::securityCheck($link)) { |
| 257 | throw new common_exception_Error(__('Your path contains error')); |
| 258 | } |
| 259 | |
| 260 | $file = $this->getItemDirectory()->getFile($link); |
| 261 | if ($file->exists()) { |
| 262 | return $file; |
| 263 | } |
| 264 | throw new \tao_models_classes_FileNotFoundException($link); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Get item directory based on $this->item & $this->lang |
| 269 | * |
| 270 | * @return Directory |
| 271 | * @throws \common_Exception |
| 272 | */ |
| 273 | protected function getItemDirectory() |
| 274 | { |
| 275 | if (! $this->itemService) { |
| 276 | $this->itemService = taoItems_models_classes_ItemsService::singleton(); |
| 277 | } |
| 278 | return $this->itemService->getItemDirectory($this->item, $this->lang); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * @throws \common_Exception |
| 283 | * @throws \tao_models_classes_FileNotFoundException |
| 284 | * @throws common_exception_Error |
| 285 | */ |
| 286 | private function searchDirectories( |
| 287 | string $parentLink, |
| 288 | array $acceptableMime, |
| 289 | int $depth, |
| 290 | int $childrenLimit |
| 291 | ): array { |
| 292 | if (!tao_helpers_File::securityCheck($parentLink)) { |
| 293 | throw new common_exception_Error(__('Your path contains error')); |
| 294 | } |
| 295 | |
| 296 | $label = rtrim($parentLink, '/'); |
| 297 | if (strrpos($parentLink, '/') !== false && substr($parentLink, -1) !== '/') { |
| 298 | $label = substr($parentLink, strrpos($parentLink, '/') + 1); |
| 299 | $parentLink = $parentLink . '/'; |
| 300 | } |
| 301 | |
| 302 | if (in_array($parentLink, ['', '/'])) { |
| 303 | $label = $this->getItem()->getLabel(); |
| 304 | $parentLink = '/'; |
| 305 | } |
| 306 | |
| 307 | $data = [ |
| 308 | 'path' => $parentLink, |
| 309 | 'label' => $label, |
| 310 | 'childrenLimit' => $childrenLimit, |
| 311 | 'total' => 0, |
| 312 | ]; |
| 313 | |
| 314 | if ($depth <= 0) { |
| 315 | $data['parent'] = $parentLink; |
| 316 | return $data; |
| 317 | } |
| 318 | |
| 319 | $children = []; |
| 320 | |
| 321 | /** @var Directory $directory */ |
| 322 | $itemDirectory = $this->getItemDirectory(); |
| 323 | if ($parentLink != '/') { |
| 324 | $directory = $itemDirectory->getDirectory($parentLink); |
| 325 | } else { |
| 326 | $directory = $itemDirectory; |
| 327 | } |
| 328 | |
| 329 | $iterator = $directory->getFlyIterator(); |
| 330 | $total = 0; |
| 331 | foreach ($iterator as $content) { |
| 332 | if ($content instanceof Directory) { |
| 333 | $children[] = $this->searchDirectories( |
| 334 | $itemDirectory->getRelPath($content), |
| 335 | $acceptableMime, |
| 336 | $depth - 1, |
| 337 | $childrenLimit |
| 338 | ); |
| 339 | continue; |
| 340 | } |
| 341 | |
| 342 | $fileInfo = $this->getInfoFromFile($content); |
| 343 | if (empty($acceptableMime) || in_array($fileInfo['mime'], $acceptableMime)) { |
| 344 | $children[] = $fileInfo; |
| 345 | $total++; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | $data['children'] = $children; |
| 350 | $data['total'] = $total; |
| 351 | |
| 352 | return $data; |
| 353 | } |
| 354 | } |