ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
FlySystemFileAccess.php
Go to the documentation of this file.
1<?php
2declare(strict_types=1);
3
5
12use League\Flysystem\FileExistsException;
13use League\Flysystem\FilesystemInterface;
14
25{
26
30 private $flySystemFS;
31
32
38 public function __construct(FilesystemInterface $flySystemFS)
39 {
40 $this->flySystemFS = $flySystemFS;
41 }
42
43
57 public function read(string $path) : string
58 {
59 try {
60 $result = $this->flySystemFS->read($path);
61
62 if ($result === false) {
63 throw new IOException("Could not access the file \"$path\".");
64 }
65
66 return $result;
67 } catch (\League\Flysystem\FileNotFoundException $ex) {
68 throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
69 }
70 }
71
72
83 public function has(string $path) : bool
84 {
85 return $this->flySystemFS->has($path);
86 }
87
88
99 public function getMimeType(string $path) : string
100 {
101 try {
102 $mimeType = $this->flySystemFS->getMimetype($path);
103 if ($mimeType === false) {
104 throw new IOException("Could not determine the MIME type of the file \"$path\".");
105 }
106
107 return $mimeType;
108 } catch (\League\Flysystem\FileNotFoundException $ex) {
109 throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
110 }
111 }
112
113
127 public function getTimestamp(string $path) : \DateTimeImmutable
128 {
129 try {
130 $rawTimestamp = $this->flySystemFS->getTimestamp($path);
131 if ($rawTimestamp === false) {
132 throw new IOException("Could not lookup timestamp of the file \"$path\".");
133 }
134
135 if (is_numeric($rawTimestamp)) {
136 $rawTimestamp = '@' . $rawTimestamp;
137 }
138
139 return new \DateTimeImmutable($rawTimestamp);
140 } catch (\League\Flysystem\FileNotFoundException $ex) {
141 throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
142 }
143 }
144
145
163 public function getSize(string $path, int $fileSizeUnit) : DataSize
164 {
165 try {
166 $byteSize = $this->flySystemFS->getSize($path);
167
168 //check if the fly system adapter failed
169 if ($byteSize === false) {
170 throw new IOException("Could not calculate the file size of the file \"$path\".");
171 }
172
173 $size = new DataSize($byteSize, $fileSizeUnit);
174 return $size;
175 } catch (\League\Flysystem\FileNotFoundException $ex) {
176 throw new FileNotFoundException("File \"$path\" not found.");
177 }
178 }
179
180
198 public function setVisibility(string $path, string $visibility) : bool
199 {
200 if ($this->has($path) === false) {
201 throw new FileNotFoundException("Path \"$path\" not found.");
202 }
203
204 $this->validateVisibility($visibility);
205
206 return $this->flySystemFS->setVisibility($path, $visibility);
207 }
208
209
219 private function validateVisibility(string $visibility)
220 {
221 if (strcmp($visibility, Visibility::PUBLIC_ACCESS) !== 0 && strcmp($visibility, Visibility::PRIVATE_ACCESS) !== 0) {
222 throw new \InvalidArgumentException("The access must be 'public' or 'private' but '$visibility' was given.");
223 }
224 }
225
226
244 public function getVisibility(string $path) : string
245 {
246 if ($this->has($path) === false) {
247 throw new FileNotFoundException("Path \"$path\" not found.");
248 }
249
250 $visibility = $this->flySystemFS->getVisibility($path);
251
252 if ($visibility === false) {
253 throw new IOException("Could not determine visibility for path '$path'.");
254 }
255
256 return $visibility;
257 }
258
259
274 public function write(string $path, string $content)
275 {
276 try {
277 if ($this->flySystemFS->write($path, $content) === false) {
278 throw new IOException("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
279 }
280 } catch (FileExistsException $ex) {
281 throw new FileAlreadyExistsException("File \"$path\" already exists.", 0, $ex);
282 }
283 }
284
285
301 public function update(string $path, string $newContent)
302 {
303 try {
304 if ($this->flySystemFS->update($path, $newContent) === false) {
305 throw new IOException("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
306 }
307 } catch (\League\Flysystem\FileNotFoundException $ex) {
308 throw new FileNotFoundException("File \"$path\" was not found update failed.", 0, $ex);
309 }
310 }
311
312
326 public function put(string $path, string $content)
327 {
328 if ($this->flySystemFS->put($path, $content) === false) {
329 throw new IOException("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
330 }
331 }
332
333
347 public function delete(string $path)
348 {
349 try {
350 if ($this->flySystemFS->delete($path) === false) {
351 throw new IOException("Could not delete file \"$path\" because a general IO error occurred. Please check that your target is writable.");
352 }
353 } catch (\League\Flysystem\FileNotFoundException $ex) {
354 throw new FileNotFoundException("File \"$path\" was not found delete operation failed.");
355 }
356 }
357
358
372 public function readAndDelete(string $path) : string
373 {
374 $content = $this->read($path);
375 $this->delete($path);
376
377 return $content;
378 }
379
380
396 public function rename(string $path, string $newPath)
397 {
398 try {
399 if ($this->flySystemFS->rename($path, $newPath) === false) {
400 throw new IOException("Could not move file from \"$path\" to \"$newPath\".");
401 }
402 } catch (FileExistsException $ex) {
403 throw new FileAlreadyExistsException("File \"$newPath\" already exists.");
404 } catch (\League\Flysystem\FileNotFoundException $ex) {
405 throw new FileNotFoundException("File \"$path\" not found.");
406 }
407 }
408
409
425 public function copy(string $path, string $copyPath)
426 {
427 try {
428 if ($this->flySystemFS->copy($path, $copyPath) === false) {
429 throw new IOException("Could not copy file \"$path\" to destination \"$copyPath\" because a general IO error occurred. Please check that your destination is writable.");
430 }
431 } catch (FileExistsException $ex) {
432 throw new FileAlreadyExistsException("File destination \"$copyPath\" already exists copy failed.");
433 } catch (\League\Flysystem\FileNotFoundException $ex) {
434 throw new FileNotFoundException("File source \"$path\" was not found copy failed.");
435 }
436 }
437}
$result
$size
Definition: RandomTest.php:84
An exception for terminatinating execution or to throw for unit testing.
Class DataSize.
Definition: DataSize.php:16
getSize()
The calculated data size.
Definition: DataSize.php:143
setVisibility(string $path, string $visibility)
Sets the visibility for a file.
update(string $path, string $newContent)
Updates the content of a file.
put(string $path, string $content)
Creates a file or updates an existing one.
has(string $path)
Checks whether a file exists.
readAndDelete(string $path)
Reads the entire file content into a string and removes the file afterwards.
write(string $path, string $content)
Writes the content to a new file.
validateVisibility(string $visibility)
Checks if the given visibility is valid an throws an exception otherwise.
copy(string $path, string $copyPath)
Copy the source file to a destination.
rename(string $path, string $newPath)
Moves a file from the source to the destination.
read(string $path)
Reads a file content to a string.
__construct(FilesystemInterface $flySystemFS)
FlySystemFileAccess constructor.
getTimestamp(string $path)
Get the timestamp (mtime) of the file.
getSize(string $path, int $fileSizeUnit)
Get the size of a file.
Interface Visibility.
Definition: Visibility.php:19
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:30
const PUBLIC_ACCESS
Public file visibility.
Definition: Visibility.php:25