Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| PortableElementFileStorage | |
0.00% |
0 / 45 |
|
0.00% |
0 / 10 |
420 | |
0.00% |
0 / 1 |
| getFileStorage | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getAccessProvider | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPrefix | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getFileUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| sanitizeSourceAsDirectory | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| registerFiles | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
42 | |||
| unregisterFiles | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| unregisterAllFiles | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFileContentFromModelStorage | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getFileStream | |
0.00% |
0 / 4 |
|
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) 2016 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoQtiItem\model\portableElement\storage; |
| 23 | |
| 24 | use GuzzleHttp\Psr7\Stream; |
| 25 | use League\Flysystem\Filesystem; |
| 26 | use oat\oatbox\filesystem\FileSystemService; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | use oat\tao\model\websource\WebsourceManager; |
| 29 | use oat\taoQtiItem\model\portableElement\exception\PortableElementFileStorageException; |
| 30 | use oat\taoQtiItem\model\portableElement\model\PortableElementModelTrait; |
| 31 | use oat\taoQtiItem\model\portableElement\element\PortableElementObject; |
| 32 | |
| 33 | class PortableElementFileStorage extends ConfigurableService |
| 34 | { |
| 35 | use PortableElementModelTrait; |
| 36 | |
| 37 | public const SERVICE_ID = 'taoQtiItem/portableElementFileStorage'; |
| 38 | |
| 39 | public const OPTION_WEBSOURCE = 'websource'; |
| 40 | public const OPTION_FILESYSTEM = 'filesystem'; |
| 41 | |
| 42 | /** |
| 43 | * @return Filesystem |
| 44 | */ |
| 45 | public function getFileStorage() |
| 46 | { |
| 47 | return $this |
| 48 | ->getServiceLocator() |
| 49 | ->get(FileSystemService::SERVICE_ID) |
| 50 | ->getFileSystem($this->getOption(self::OPTION_FILESYSTEM)); |
| 51 | } |
| 52 | |
| 53 | protected function getAccessProvider() |
| 54 | { |
| 55 | return WebsourceManager::singleton()->getWebsource($this->getOption(self::OPTION_WEBSOURCE)); |
| 56 | } |
| 57 | |
| 58 | public function getPrefix(PortableElementObject $object) |
| 59 | { |
| 60 | $hashFile = DIRECTORY_SEPARATOR . md5($object->getTypeIdentifier() . $object->getVersion()) |
| 61 | . DIRECTORY_SEPARATOR; |
| 62 | |
| 63 | return $object->getModel()->getId() . $hashFile; |
| 64 | } |
| 65 | |
| 66 | public function getFileUrl(PortableElementObject $object, $relPath = '') |
| 67 | { |
| 68 | return $this->getAccessProvider()->getAccessUrl($this->getPrefix($object) . $relPath); |
| 69 | } |
| 70 | |
| 71 | protected function sanitizeSourceAsDirectory($source) |
| 72 | { |
| 73 | if (! is_dir($source)) { |
| 74 | throw new PortableElementFileStorageException('Unable to locate the source directory.'); |
| 75 | } |
| 76 | return rtrim($source, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Register files associated to a PCI track by $typeIdentifier |
| 81 | * |
| 82 | * @refactor improve response |
| 83 | * |
| 84 | * @param PortableElementObject $object |
| 85 | * @param string[] $files Relative path of portable element files |
| 86 | * @param string $source Location of temporary directory |
| 87 | * @return bool |
| 88 | * @throws PortableElementFileStorageException |
| 89 | */ |
| 90 | public function registerFiles(PortableElementObject $object, $files, $source) |
| 91 | { |
| 92 | $registered = false; |
| 93 | $fileSystem = $this->getFileStorage(); |
| 94 | $source = $this->sanitizeSourceAsDirectory($source); |
| 95 | |
| 96 | foreach ($files as $file) { |
| 97 | if (!$object->isRegistrableFile($file)) { |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | $filePath = $source . ltrim($file, DIRECTORY_SEPARATOR); |
| 102 | if (!file_exists($filePath) || ($resource = fopen($filePath, 'r')) === false) { |
| 103 | throw new PortableElementFileStorageException('File cannot be opened : ' . $filePath); |
| 104 | } |
| 105 | |
| 106 | $fileId = $this->getPrefix($object) . $object->getRegistrationFileId($file); |
| 107 | $fileSystem->writeStream($fileId, $resource); |
| 108 | $registered = true; |
| 109 | if (is_resource($resource)) { |
| 110 | fclose($resource); |
| 111 | } |
| 112 | \common_Logger::i('Portable element asset file "' . $fileId . '" copied.'); |
| 113 | } |
| 114 | return $registered; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Unregister files by removing them from FileSystem |
| 119 | * |
| 120 | * @param PortableElementObject $object |
| 121 | * @param $files |
| 122 | * @return bool |
| 123 | * @throws \common_Exception |
| 124 | */ |
| 125 | public function unregisterFiles(PortableElementObject $object, $files) |
| 126 | { |
| 127 | $filesystem = $this->getFileStorage(); |
| 128 | foreach ($files as $relPath) { |
| 129 | $fileId = $this->getPrefix($object) . $relPath; |
| 130 | if (!$filesystem->fileExists($fileId)) { |
| 131 | throw new \common_Exception('File does not exists in the filesystem: ' . $relPath); |
| 132 | } |
| 133 | $filesystem->delete($fileId); |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Remove all the portable element files and dir |
| 140 | * @param PortableElementObject $object |
| 141 | * @return bool |
| 142 | */ |
| 143 | public function unregisterAllFiles(PortableElementObject $object) |
| 144 | { |
| 145 | return $this->getFileStorage()->deleteDirectory($this->getPrefix($object)); |
| 146 | } |
| 147 | |
| 148 | public function getFileContentFromModelStorage(PortableElementObject $object, $file) |
| 149 | { |
| 150 | $filePath = $this->getPrefix($object) . $file; |
| 151 | if ($this->getFileStorage()->fileExists($filePath)) { |
| 152 | return $this->getFileStorage()->read($filePath); |
| 153 | } |
| 154 | throw new PortableElementFileStorageException('Unable to find file "' . $file . '"' . |
| 155 | ' related to Portable element ' . $object->getTypeIdentifier()); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @param PortableElementObject $object |
| 160 | * @param $file |
| 161 | * @return bool|false|resource |
| 162 | * @throws \common_Exception |
| 163 | */ |
| 164 | public function getFileStream(PortableElementObject $object, $file) |
| 165 | { |
| 166 | $filePath = $this->getPrefix($object) . $file; |
| 167 | if ($this->getFileStorage()->fileExists($filePath)) { |
| 168 | return new Stream($this->getFileStorage()->readStream($filePath)); |
| 169 | } |
| 170 | throw new PortableElementFileStorageException($filePath); |
| 171 | } |
| 172 | } |