ILIAS  release_7 Revision v7.30-3-g800a261c036
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
23{
24
28 private $flySystemFS;
29
34 public function __construct(FilesystemInterface $flySystemFS)
35 {
36 $this->flySystemFS = $flySystemFS;
37 }
38
48 public function read(string $path) : string
49 {
50 try {
51 $path = Util::normalizeRelativePath($path);
52 $adapter = $this->flySystemFS->getAdapter();
53 if (!$adapter->has($path)) {
54 throw new \League\Flysystem\FileNotFoundException($path);
55 }
56 $object = $adapter->read($path);
57 $result = $object['contents'];
58
59 if ($result === false) {
60 throw new IOException("Could not access the file \"$path\".");
61 }
62
63 return $result;
64 } catch (\League\Flysystem\FileNotFoundException $ex) {
65 throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
66 }
67 }
68
76 public function has(string $path) : bool
77 {
78 return $this->flySystemFS->has($path);
79 }
80
88 public function getMimeType(string $path) : string
89 {
90 try {
91 $mimeType = $this->flySystemFS->getMimetype($path);
92 if ($mimeType === false) {
93 throw new IOException("Could not determine the MIME type of the file \"$path\".");
94 }
95
96 return $mimeType;
97 } catch (\League\Flysystem\FileNotFoundException $ex) {
98 throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
99 }
100 }
101
111 public function getTimestamp(string $path) : \DateTimeImmutable
112 {
113 try {
114 $rawTimestamp = $this->flySystemFS->getTimestamp($path);
115 if ($rawTimestamp === false) {
116 throw new IOException("Could not lookup timestamp of the file \"$path\".");
117 }
118
119 if (is_numeric($rawTimestamp)) {
120 $rawTimestamp = '@' . $rawTimestamp;
121 }
122
123 return new \DateTimeImmutable($rawTimestamp);
124 } catch (\League\Flysystem\FileNotFoundException $ex) {
125 throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
126 }
127 }
128
141 public function getSize(string $path, int $fileSizeUnit) : DataSize
142 {
143 try {
144 $byteSize = $this->flySystemFS->getSize($path);
145
146 //check if the fly system adapter failed
147 if ($byteSize === false) {
148 throw new IOException("Could not calculate the file size of the file \"$path\".");
149 }
150
151 $size = new DataSize($byteSize, $fileSizeUnit);
152 return $size;
153 } catch (\League\Flysystem\FileNotFoundException $ex) {
154 throw new FileNotFoundException("File \"$path\" not found.");
155 }
156 }
157
171 public function setVisibility(string $path, string $visibility) : bool
172 {
173 if ($this->has($path) === false) {
174 throw new FileNotFoundException("Path \"$path\" not found.");
175 }
176
177 $this->validateVisibility($visibility);
178
179 return $this->flySystemFS->setVisibility($path, $visibility);
180 }
181
189 private function validateVisibility(string $visibility)
190 {
191 if (strcmp($visibility, Visibility::PUBLIC_ACCESS) !== 0 && strcmp($visibility,
193 throw new \InvalidArgumentException("The access must be 'public' or 'private' but '$visibility' was given.");
194 }
195 }
196
209 public function getVisibility(string $path) : string
210 {
211 if ($this->has($path) === false) {
212 throw new FileNotFoundException("Path \"$path\" not found.");
213 }
214
215 $visibility = $this->flySystemFS->getVisibility($path);
216
217 if ($visibility === false) {
218 throw new IOException("Could not determine visibility for path '$path'.");
219 }
220
221 return $visibility;
222 }
223
234 public function write(string $path, string $content)
235 {
236 try {
237 if ($this->flySystemFS->write($path, $content) === false) {
238 throw new IOException("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
239 }
240 } catch (FileExistsException $ex) {
241 throw new FileAlreadyExistsException("File \"$path\" already exists.", 0, $ex);
242 }
243 }
244
256 public function update(string $path, string $newContent)
257 {
258 try {
259 if ($this->flySystemFS->update($path, $newContent) === false) {
260 throw new IOException("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
261 }
262 } catch (\League\Flysystem\FileNotFoundException $ex) {
263 throw new FileNotFoundException("File \"$path\" was not found update failed.", 0, $ex);
264 }
265 }
266
276 public function put(string $path, string $content)
277 {
278 if ($this->flySystemFS->put($path, $content) === false) {
279 throw new IOException("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
280 }
281 }
282
292 public function delete(string $path)
293 {
294 try {
295 if ($this->flySystemFS->delete($path) === false) {
296 throw new IOException("Could not delete file \"$path\" because a general IO error occurred. Please check that your target is writable.");
297 }
298 } catch (\League\Flysystem\FileNotFoundException $ex) {
299 throw new FileNotFoundException("File \"$path\" was not found delete operation failed.");
300 }
301 }
302
312 public function readAndDelete(string $path) : string
313 {
314 $content = $this->read($path);
315 $this->delete($path);
316
317 return $content;
318 }
319
331 public function rename(string $path, string $newPath)
332 {
333 try {
334 if ($this->flySystemFS->rename($path, $newPath) === false) {
335 throw new IOException("Could not move file from \"$path\" to \"$newPath\".");
336 }
337 } catch (FileExistsException $ex) {
338 throw new FileAlreadyExistsException("File \"$newPath\" already exists.");
339 } catch (\League\Flysystem\FileNotFoundException $ex) {
340 throw new FileNotFoundException("File \"$path\" not found.");
341 }
342 }
343
355 public function copy(string $path, string $copyPath)
356 {
357 try {
358 if ($this->flySystemFS->copy($path, $copyPath) === false) {
359 throw new IOException("Could not copy file \"$path\" to destination \"$copyPath\" because a general IO error occurred. Please check that your destination is writable.");
360 }
361 } catch (FileExistsException $ex) {
362 throw new FileAlreadyExistsException("File destination \"$copyPath\" already exists copy failed.");
363 } catch (\League\Flysystem\FileNotFoundException $ex) {
364 throw new FileNotFoundException("File source \"$path\" was not found copy failed.");
365 }
366 }
367}
$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
Class FileNotFoundException Indicates that a file is missing or not found.
Class IOException Indicates general problems with the input or output operations.
Definition: IOException.php:13
Class FlySystemFileAccess Fly system file access implementation.
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 FileAccess The FileAccess interface defines all file operations.
Definition: FileAccess.php:15
Interface Visibility This interface provides the available options for the filesystem right managemen...
Definition: Visibility.php:16
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:27
const PUBLIC_ACCESS
Public file visibility.
Definition: Visibility.php:22