Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 68 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
| File | |
0.00% |
0 / 68 |
|
0.00% |
0 / 11 |
930 | |
0.00% |
0 / 1 |
| getBasename | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMimeType | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| getSize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| write | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
110 | |||
| update | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| put | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| read | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| readStream | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| readPsrStream | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| exists | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| delete | |
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) 2016-2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\oatbox\filesystem; |
| 23 | |
| 24 | use common_Logger; |
| 25 | use GuzzleHttp\Psr7\Stream; |
| 26 | use GuzzleHttp\Psr7\StreamWrapper; |
| 27 | use Psr\Http\Message\StreamInterface; |
| 28 | use tao_helpers_File; |
| 29 | |
| 30 | class File extends FileSystemHandler |
| 31 | { |
| 32 | public function getBasename(): string |
| 33 | { |
| 34 | return basename($this->getPrefix()); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get mimetype of $this file |
| 39 | * |
| 40 | * @return string |
| 41 | */ |
| 42 | public function getMimeType() |
| 43 | { |
| 44 | try { |
| 45 | $mimeType = $this->getFileSystem()->mimeType($this->getPrefix()); |
| 46 | $suffix = substr($this->getPrefix(), -4); |
| 47 | if ($mimeType === 'text/plain' && $suffix === '.css') { |
| 48 | $mimeType = 'text/css'; |
| 49 | } |
| 50 | |
| 51 | if (in_array($suffix, ['.svg', 'svgz'])) { |
| 52 | $mimeType = tao_helpers_File::MIME_SVG; |
| 53 | } |
| 54 | |
| 55 | return $mimeType; |
| 56 | } catch (FilesystemException $e) { |
| 57 | $this->logWarning($e->getMessage()); |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get size of $this file |
| 64 | * |
| 65 | * @return bool|false|int |
| 66 | */ |
| 67 | public function getSize() |
| 68 | { |
| 69 | return $this->getFileSystem()->fileSize($this->getPrefix()); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Write a content into $this file, if not exists |
| 74 | * $mixed content has to be string, resource, or PSR Stream |
| 75 | * In case of Stream, $mixed has to be seekable and readable |
| 76 | * |
| 77 | * @param string|Resource|StreamInterface $mixed |
| 78 | * @param null $mimeType |
| 79 | * @return bool |
| 80 | * @throws \common_Exception |
| 81 | */ |
| 82 | public function write($mixed, $mimeType = null) |
| 83 | { |
| 84 | $config = (is_null($mimeType)) ? [] : ['ContentType' => $mimeType]; |
| 85 | |
| 86 | try { |
| 87 | if (is_string($mixed)) { |
| 88 | $this->getFileSystem()->write($this->getPrefix(), $mixed, $config); |
| 89 | } elseif (is_resource($mixed)) { |
| 90 | $this->getFileSystem()->writeStream($this->getPrefix(), $mixed, $config); |
| 91 | } elseif ($mixed instanceof StreamInterface) { |
| 92 | if (!$mixed->isReadable()) { |
| 93 | throw new \common_Exception('Stream is not readable. Write to filesystem aborted.'); |
| 94 | } |
| 95 | if ($mixed->isSeekable()) { |
| 96 | $mixed->rewind(); |
| 97 | } elseif ($mixed->eof()) { |
| 98 | throw new \common_Exception( |
| 99 | 'Stream is not seekable and is already processed. Write to filesystem aborted.' |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | $resource = StreamWrapper::getResource($mixed); |
| 104 | if (!is_resource($resource)) { |
| 105 | throw new \common_Exception( |
| 106 | 'Unable to create resource from the given stream. Write to filesystem aborted.' |
| 107 | ); |
| 108 | } |
| 109 | $this->getFileSystem()->writeStream($this->getPrefix(), $resource, $config); |
| 110 | } else { |
| 111 | throw new \InvalidArgumentException(sprintf( |
| 112 | 'Value to be written has to be: string, resource or StreamInterface, "%s" given.', |
| 113 | gettype($mixed) |
| 114 | )); |
| 115 | } |
| 116 | } catch (FilesystemException $e) { |
| 117 | $this->logWarning($e->getMessage()); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Update a content into $this file, if exists |
| 126 | * $mixed content has to be string, resource, or PSR Stream |
| 127 | * In case of Stream, $mixed has to be seekable and readable |
| 128 | * |
| 129 | * @param $mixed |
| 130 | * @param null $mimeType |
| 131 | * @return bool |
| 132 | * @throws \common_Exception |
| 133 | */ |
| 134 | public function update($mixed, $mimeType = null) |
| 135 | { |
| 136 | if (!$this->exists()) { |
| 137 | throw new \RuntimeException('File "' . $this->getPrefix() . '" not found."'); |
| 138 | } |
| 139 | |
| 140 | common_Logger::i('Writing in ' . $this->getPrefix()); |
| 141 | |
| 142 | return $this->write($mixed, $mimeType); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Put a content into $this file, if exists or not |
| 147 | * $mixed content has to be string, resource, or PSR Stream |
| 148 | * In case of Stream, $mixed has to be seekable and readable |
| 149 | * |
| 150 | * @param string|Resource|StreamInterface $mixed |
| 151 | * @param null $mimeType |
| 152 | * @return bool |
| 153 | * @throws \common_Exception |
| 154 | */ |
| 155 | public function put($mixed, $mimeType = null) |
| 156 | { |
| 157 | common_Logger::i('Writting in ' . $this->getPrefix()); |
| 158 | |
| 159 | return $this->write($mixed, $mimeType); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Return content of file as string |
| 164 | * |
| 165 | * @return false|string |
| 166 | */ |
| 167 | public function read() |
| 168 | { |
| 169 | try { |
| 170 | return $this->getFileSystem()->read($this->getPrefix()); |
| 171 | } catch (FilesystemException $e) { |
| 172 | $this->logWarning($e->getMessage()); |
| 173 | } |
| 174 | |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Return content of file as PHP stream (resource) |
| 180 | * |
| 181 | * @return false|resource |
| 182 | */ |
| 183 | public function readStream() |
| 184 | { |
| 185 | try { |
| 186 | return $this->getFileSystem()->readStream($this->getPrefix()); |
| 187 | } catch (FilesystemException $e) { |
| 188 | $this->logWarning($e->getMessage()); |
| 189 | } |
| 190 | |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Return content of file as PSR-7 stream |
| 196 | * |
| 197 | * @return StreamInterface |
| 198 | */ |
| 199 | public function readPsrStream() |
| 200 | { |
| 201 | $resource = null; |
| 202 | try { |
| 203 | $resource = $this->getFileSystem()->readStream($this->getPrefix()); |
| 204 | } catch (FilesystemException $e) { |
| 205 | $this->logWarning($e->getMessage()); |
| 206 | } |
| 207 | |
| 208 | return new Stream($resource); |
| 209 | } |
| 210 | |
| 211 | public function exists(): bool |
| 212 | { |
| 213 | try { |
| 214 | return $this->getFileSystem()->fileExists($this->getPrefix()); |
| 215 | } catch (FilesystemException $e) { |
| 216 | $this->logWarning($e->getMessage()); |
| 217 | } |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | public function delete(): bool |
| 222 | { |
| 223 | try { |
| 224 | $this->getFileSystem()->delete($this->getPrefix()); |
| 225 | return true; |
| 226 | } catch (FilesystemException $e) { |
| 227 | $this->logWarning($e->getMessage()); |
| 228 | } |
| 229 | |
| 230 | return false; |
| 231 | } |
| 232 | } |