Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| FlySystemManagement | |
0.00% |
0 / 31 |
|
0.00% |
0 / 8 |
210 | |
0.00% |
0 / 1 |
| storeFile | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| deleteDirectory | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getFileSize | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getFileStream | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| retrieveFile | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| deleteFile | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getFilesystem | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getUniqueFilename | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 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 | */ |
| 22 | |
| 23 | namespace oat\taoMediaManager\model\fileManagement; |
| 24 | |
| 25 | use oat\oatbox\filesystem\File; |
| 26 | use oat\oatbox\filesystem\FilesystemException; |
| 27 | use oat\oatbox\filesystem\FilesystemInterface; |
| 28 | use oat\oatbox\service\ConfigurableService; |
| 29 | use Slim\Http\Stream; |
| 30 | use Psr\Http\Message\StreamInterface; |
| 31 | use oat\oatbox\filesystem\FileSystemService; |
| 32 | |
| 33 | class FlySystemManagement extends ConfigurableService implements FileManagement |
| 34 | { |
| 35 | public const OPTION_FS = 'fs'; |
| 36 | |
| 37 | /** |
| 38 | * @param string|File $fileSource |
| 39 | * @param string $label |
| 40 | * @return string |
| 41 | * @throws FilesystemException |
| 42 | */ |
| 43 | public function storeFile($fileSource, $label) |
| 44 | { |
| 45 | $filename = $this->getUniqueFilename($label); |
| 46 | $stream = $fileSource instanceof File ? $fileSource->readStream() : fopen($fileSource, 'r'); |
| 47 | $this->getFileSystem()->writeStream($filename, $stream); |
| 48 | |
| 49 | if (is_resource($stream)) { |
| 50 | fclose($stream); |
| 51 | } |
| 52 | |
| 53 | return $filename; |
| 54 | } |
| 55 | |
| 56 | public function deleteDirectory(string $directoryPath): bool |
| 57 | { |
| 58 | try { |
| 59 | $this->getFilesystem()->deleteDirectory($directoryPath); |
| 60 | return true; |
| 61 | } catch (FilesystemException $e) { |
| 62 | $this->logWarning($e->getMessage()); |
| 63 | return false; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public function getFileSize($link) |
| 68 | { |
| 69 | try { |
| 70 | return $this->getFilesystem()->fileSize($link); |
| 71 | } catch (FilesystemException $e) { |
| 72 | $this->logWarning($e->getMessage()); |
| 73 | return null; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * |
| 79 | * @param string $link |
| 80 | * @return StreamInterface |
| 81 | */ |
| 82 | public function getFileStream($link) |
| 83 | { |
| 84 | $resource = $this->getFilesystem()->readStream($link); |
| 85 | return new Stream($resource); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * (non-PHPdoc) |
| 90 | * @see \oat\taoMediaManager\model\fileManagement\FileManagement::retrieveFile() |
| 91 | */ |
| 92 | public function retrieveFile($link) |
| 93 | { |
| 94 | $this->logWarning('Deprecated'); |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * (non-PHPdoc) |
| 100 | * @see \oat\taoMediaManager\model\fileManagement\FileManagement::deleteFile() |
| 101 | */ |
| 102 | public function deleteFile($link) |
| 103 | { |
| 104 | try { |
| 105 | $this->getFilesystem()->delete($link); |
| 106 | return true; |
| 107 | } catch (FilesystemException $e) { |
| 108 | $this->logWarning($e->getMessage()); |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | protected function getFilesystem(): FilesystemInterface |
| 114 | { |
| 115 | $fs = $this->getServiceLocator()->get(FileSystemService::SERVICE_ID); |
| 116 | return $fs->getFileSystem($this->getOption(self::OPTION_FS)); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Create a new unique filename based on an existing filename |
| 121 | * |
| 122 | * @param string $fileName |
| 123 | * @return string |
| 124 | */ |
| 125 | protected function getUniqueFilename($fileName) |
| 126 | { |
| 127 | $returnValue = uniqid(hash('crc32', $fileName)); |
| 128 | |
| 129 | $ext = @pathinfo($fileName, PATHINFO_EXTENSION); |
| 130 | if (!empty($ext)) { |
| 131 | $returnValue .= '.' . $ext; |
| 132 | } |
| 133 | |
| 134 | return $returnValue; |
| 135 | } |
| 136 | } |