ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
FlySystemFileAccess.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
38 
46 {
47  public function __construct(
48  private FilesystemOperator $flysystem_operator
49  ) {
50  }
51 
52  public function read(string $path): string
53  {
54  try {
55  $path = Util::normalizeRelativePath($path);
56  if (!$this->flysystem_operator->has($path)) {
57  throw new \League\Flysystem\FileNotFoundException($path);
58  }
59  $result = $this->flysystem_operator->read($path);
60 
61  if (empty($result)) {
62  throw new IOException("Could not access the file \"$path\".");
63  }
64 
65  return $result;
66  } catch (\Throwable $ex) {
67  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
68  }
69  }
70 
71  public function has(string $path): bool
72  {
73  return $this->flysystem_operator->has($path);
74  }
75 
76  public function getMimeType(string $path): string
77  {
78  try {
79  $mimeType = $this->flysystem_operator->mimeType($path);
80  if ($mimeType === '') {
81  throw new IOException("Could not determine the MIME type of the file \"$path\".");
82  }
83 
84  return $mimeType;
85  } catch (UnableToRetrieveMetadata $ex) {
86  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
87  }
88  }
89 
90  public function getTimestamp(string $path): \DateTimeImmutable
91  {
92  try {
93  $last_modified = (int) $this->flysystem_operator->lastModified($path);
94 
95  return new \DateTimeImmutable(date('Y-m-d H:i:s', $last_modified));
96  } catch (UnableToRetrieveMetadata $ex) {
97  throw new IOException("Could not lookup timestamp of the file \"$path\".");
98  } catch (FilesystemException $ex) {
99  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
100  }
101  }
102 
103  public function getSize(string $path, int $unit): DataSize
104  {
105  try {
106  $byte_size = $this->flysystem_operator->fileSize($path);
107  return new DataSize($byte_size, $unit);
108  } catch (UnableToRetrieveMetadata) {
109  throw new FileNotFoundException("File \"$path\" not found.");
110  }
111  }
112 
120  public function setVisibility(string $path, string $visibility): bool
121  {
122  if (!$this->has($path)) {
123  throw new FileNotFoundException("Path \"$path\" not found.");
124  }
125 
126  $this->validateVisibility($visibility);
127 
128  try {
129  $this->flysystem_operator->setVisibility($path, $visibility);
130  } catch (\Throwable) {
131  return false;
132  }
133  return true;
134  }
135 
144  private function validateVisibility(string $visibility): void
145  {
146  if (strcmp($visibility, Visibility::PUBLIC_ACCESS) === 0) {
147  return;
148  }
149  if (strcmp($visibility, Visibility::PRIVATE_ACCESS) === 0) {
150  return;
151  }
152  throw new \InvalidArgumentException("The access must be 'public' or 'private' but '$visibility' was given.");
153  }
154 
170  public function getVisibility(string $path): string
171  {
172  if (!$this->has($path)) {
173  throw new FileNotFoundException("Path \"$path\" not found.");
174  }
175 
176  $visibility = $this->flysystem_operator->getVisibility($path);
177 
178  if ($visibility === false) {
179  throw new IOException("Could not determine visibility for path '$path'.");
180  }
181 
182  return $visibility;
183  }
184 
196  public function write(string $path, string $content): void
197  {
198  if ($this->flysystem_operator->has($path)) {
199  throw new FileAlreadyExistsException("File \"$path\" already exists.");
200  }
201  try {
202  $this->flysystem_operator->write($path, $content);
203  } catch (FilesystemException) {
204  throw new IOException(
205  "Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable."
206  );
207  }
208  }
209 
222  public function update(string $path, string $new_content): void
223  {
224  try {
225  $this->flysystem_operator->write($path, $new_content);
226  } catch (UnableToWriteFile $ex) {
227  throw new IOException(
228  "Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.",
229  0,
230  $ex
231  );
232  } catch (UnableToRetrieveMetadata $ex) {
233  throw new FileNotFoundException("File \"$path\" was not found update failed.", 0, $ex);
234  }
235  }
236 
247  public function put(string $path, string $content): void
248  {
249  if ($this->flysystem_operator->has($path)) {
250  $this->update($path, $content);
251  return;
252  }
253  $this->write($path, $content);
254  }
255 
266  public function delete(string $path): void
267  {
268  try {
269  $this->flysystem_operator->delete($path);
270  } catch (UnableToRetrieveMetadata) {
271  throw new FileNotFoundException("File \"$path\" was not found delete operation failed.");
272  } catch (UnableToDeleteFile) {
273  throw new IOException(
274  "Could not delete file \"$path\" because a general IO error occurred. Please check that your target is writable."
275  );
276  }
277  }
278 
290  public function readAndDelete(string $path): string
291  {
292  $content = $this->read($path);
293  $this->delete($path);
294 
295  return $content;
296  }
297 
310  public function rename(string $path, string $new_path): void
311  {
312  if ($this->flysystem_operator->has($new_path)) {
313  throw new IOException("File \"$new_path\" already exists.");
314  }
315  try {
316  $this->flysystem_operator->move($path, $new_path);
317  } catch (UnableToMoveFile) {
318  throw new IOException("Could not move file from \"$path\" to \"$new_path\".");
319  } catch (UnableToRetrieveMetadata) {
320  throw new FileNotFoundException("File \"$path\" not found.");
321  }
322  }
323 
335  public function copy(string $path, string $copy_path): void
336  {
337  if ($this->flysystem_operator->has($copy_path)) {
338  throw new FileAlreadyExistsException("File \"$copy_path\" already exists.");
339  }
340  try {
341  $this->flysystem_operator->copy($path, $copy_path);
342  } catch (UnableToCopyFile) {
343  throw new IOException(
344  "Could not copy file \"$path\" to destination \"$copy_path\" because a general IO error occurred. Please check that your destination is writable."
345  );
346  } catch (UnableToRetrieveMetadata) {
347  throw new FileNotFoundException("File source \"$path\" was not found copy failed.");
348  }
349  }
350 }
put(string $path, string $content)
Creates a file or updates an existing one.
Indicates general problems with the input or output operations.
Definition: IOException.php:27
validateVisibility(string $visibility)
Checks if the given visibility is valid an throws an exception otherwise.
static normalizeRelativePath(string $path)
Definition: Util.php:30
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.
getVisibility(string $path)
Get the file visibility.
$path
Definition: ltiservices.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(private FilesystemOperator $flysystem_operator)
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:38
setVisibility(string $path, string $visibility)
Sets the visibility for a file.
update(string $path, string $new_content)
Updates the content of a file.
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.
Indicates that a file is missing or not found.
getSize(string $path, int $unit)
Get the size of a file.
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:34
getTimestamp(string $path)
Get the timestamp of the file.