ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
FlySystemFileAccess.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
15 
16 /******************************************************************************
17  *
18  * This file is part of ILIAS, a powerful learning management system.
19  *
20  * ILIAS is licensed with the GPL-3.0, you should have received a copy
21  * of said license along with the source code.
22  *
23  * If this is not the case or you just want to try ILIAS, you'll find
24  * us at:
25  * https://www.ilias.de
26  * https://github.com/ILIAS-eLearning
27  *
28  *****************************************************************************/
39 {
40  private FilesystemInterface $flySystemFS;
41 
42 
48  public function __construct(FilesystemInterface $flySystemFS)
49  {
50  $this->flySystemFS = $flySystemFS;
51  }
52 
53 
67  public function read(string $path): string
68  {
69  try {
70  $path = Util::normalizeRelativePath($path);
71  $adapter = $this->flySystemFS->getAdapter();
72  if (!$adapter->has($path)) {
73  throw new \League\Flysystem\FileNotFoundException($path);
74  }
75  $object = $adapter->read($path);
76  $result = $object['contents'];
77 
78  if ($result === false) {
79  throw new IOException("Could not access the file \"$path\".");
80  }
81 
82  return $result;
83  } catch (\League\Flysystem\FileNotFoundException $ex) {
84  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
85  }
86  }
87 
88 
99  public function has(string $path): bool
100  {
101  return $this->flySystemFS->has($path);
102  }
103 
104 
115  public function getMimeType(string $path): string
116  {
117  try {
118  $mimeType = $this->flySystemFS->getMimetype($path);
119  if ($mimeType === false) {
120  throw new IOException("Could not determine the MIME type of the file \"$path\".");
121  }
122 
123  return $mimeType;
124  } catch (\League\Flysystem\FileNotFoundException $ex) {
125  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
126  }
127  }
128 
129 
143  public function getTimestamp(string $path): \DateTimeImmutable
144  {
145  try {
146  $rawTimestamp = $this->flySystemFS->getTimestamp($path);
147  if ($rawTimestamp === false) {
148  throw new IOException("Could not lookup timestamp of the file \"$path\".");
149  }
150 
151  if (is_numeric($rawTimestamp)) {
152  $rawTimestamp = '@' . $rawTimestamp;
153  }
154 
155  return new \DateTimeImmutable($rawTimestamp);
156  } catch (\League\Flysystem\FileNotFoundException $ex) {
157  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
158  }
159  }
160 
161 
179  public function getSize(string $path, int $fileSizeUnit): DataSize
180  {
181  try {
182  $byteSize = $this->flySystemFS->getSize($path);
183 
184  //check if the fly system adapter failed
185  if ($byteSize === false) {
186  throw new IOException("Could not calculate the file size of the file \"$path\".");
187  }
188 
189  $size = new DataSize($byteSize, $fileSizeUnit);
190  return $size;
191  } catch (\League\Flysystem\FileNotFoundException $ex) {
192  throw new FileNotFoundException("File \"$path\" not found.");
193  }
194  }
195 
196 
214  public function setVisibility(string $path, string $visibility): bool
215  {
216  if ($this->has($path) === false) {
217  throw new FileNotFoundException("Path \"$path\" not found.");
218  }
219 
220  $this->validateVisibility($visibility);
221 
222  return $this->flySystemFS->setVisibility($path, $visibility);
223  }
224 
225 
234  private function validateVisibility(string $visibility): void
235  {
236  if (strcmp($visibility, Visibility::PUBLIC_ACCESS) !== 0 && strcmp($visibility, Visibility::PRIVATE_ACCESS) !== 0) {
237  throw new \InvalidArgumentException("The access must be 'public' or 'private' but '$visibility' was given.");
238  }
239  }
240 
241 
259  public function getVisibility(string $path): string
260  {
261  if ($this->has($path) === false) {
262  throw new FileNotFoundException("Path \"$path\" not found.");
263  }
264 
265  $visibility = $this->flySystemFS->getVisibility($path);
266 
267  if ($visibility === false) {
268  throw new IOException("Could not determine visibility for path '$path'.");
269  }
270 
271  return $visibility;
272  }
273 
274 
288  public function write(string $path, string $content): void
289  {
290  try {
291  if ($this->flySystemFS->write($path, $content) === false) {
292  throw new IOException("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
293  }
294  } catch (FileExistsException $ex) {
295  throw new FileAlreadyExistsException("File \"$path\" already exists.", 0, $ex);
296  }
297  }
298 
299 
314  public function update(string $path, string $new_content): void
315  {
316  try {
317  if ($this->flySystemFS->update($path, $new_content) === false) {
318  throw new IOException("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
319  }
320  } catch (\League\Flysystem\FileNotFoundException $ex) {
321  throw new FileNotFoundException("File \"$path\" was not found update failed.", 0, $ex);
322  }
323  }
324 
325 
338  public function put(string $path, string $content): void
339  {
340  if ($this->flySystemFS->put($path, $content) === false) {
341  throw new IOException("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
342  }
343  }
344 
345 
358  public function delete(string $path): void
359  {
360  try {
361  if ($this->flySystemFS->delete($path) === false) {
362  throw new IOException("Could not delete file \"$path\" because a general IO error occurred. Please check that your target is writable.");
363  }
364  } catch (\League\Flysystem\FileNotFoundException $ex) {
365  throw new FileNotFoundException("File \"$path\" was not found delete operation failed.");
366  }
367  }
368 
369 
383  public function readAndDelete(string $path): string
384  {
385  $content = $this->read($path);
386  $this->delete($path);
387 
388  return $content;
389  }
390 
391 
406  public function rename(string $path, string $new_path): void
407  {
408  try {
409  if ($this->flySystemFS->rename($path, $new_path) === false) {
410  throw new IOException("Could not move file from \"$path\" to \"$new_path\".");
411  }
412  } catch (FileExistsException $ex) {
413  throw new FileAlreadyExistsException("File \"$new_path\" already exists.");
414  } catch (\League\Flysystem\FileNotFoundException $ex) {
415  throw new FileNotFoundException("File \"$path\" not found.");
416  }
417  }
418 
419 
434  public function copy(string $path, string $copy_path): void
435  {
436  try {
437  if ($this->flySystemFS->copy($path, $copy_path) === false) {
438  throw new IOException("Could not copy file \"$path\" to destination \"$copy_path\" because a general IO error occurred. Please check that your destination is writable.");
439  }
440  } catch (FileExistsException $ex) {
441  throw new FileAlreadyExistsException("File destination \"$copy_path\" already exists copy failed.");
442  } catch (\League\Flysystem\FileNotFoundException $ex) {
443  throw new FileNotFoundException("File source \"$path\" was not found copy failed.");
444  }
445  }
446 }
put(string $path, string $content)
Creates a file or updates an existing one.
validateVisibility(string $visibility)
Checks if the given visibility is valid an throws an exception otherwise.
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:30
read(string $path)
Reads a file content to a string.
readAndDelete(string $path)
Reads the entire file content into a string and removes the file afterwards.
getSize(string $path, int $fileSizeUnit)
Get the size of a file.
getVisibility(string $path)
Get the file visibility.
$path
Definition: ltiservices.php:32
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:42
setVisibility(string $path, string $visibility)
Sets the visibility for a file.
update(string $path, string $new_content)
Updates the content of a file.
getSize()
The calculated data size.
Definition: DataSize.php:119
rename(string $path, string $new_path)
Moves a file from the source to the destination.
copy(string $path, string $copy_path)
Copy the source file to a destination.
has(string $path)
Checks whether a file exists.
write(string $path, string $content)
Writes the content to a new file.
const PUBLIC_ACCESS
Public file visibility.
Definition: Visibility.php:37
getTimestamp(string $path)
Get the timestamp (mtime) of the file.
__construct(FilesystemInterface $flySystemFS)
FlySystemFileAccess constructor.