ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
FlySystemFileAccess.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
36 
44 {
45  public function __construct(
46  private FilesystemOperator $flysystem_operator
47  ) {
48  }
49 
50  public function read(string $path): string
51  {
52  try {
53  $path = Util::normalizeRelativePath($path);
54  if (!$this->flysystem_operator->has($path)) {
55  throw new \League\Flysystem\FileNotFoundException($path);
56  }
57  $result = $this->flysystem_operator->read($path);
58 
59  if (empty($result)) {
60  throw new IOException("Could not access the file \"$path\".");
61  }
62 
63  return $result;
64  } catch (\Throwable $ex) {
65  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
66  }
67  }
68 
69  public function has(string $path): bool
70  {
71  return $this->flysystem_operator->has($path);
72  }
73 
74  public function getMimeType(string $path): string
75  {
76  try {
77  $mimeType = $this->flysystem_operator->mimeType($path);
78  if ($mimeType === '') {
79  throw new IOException("Could not determine the MIME type of the file \"$path\".");
80  }
81 
82  return $mimeType;
83  } catch (UnableToRetrieveMetadata $ex) {
84  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
85  }
86  }
87 
88  public function getTimestamp(string $path): \DateTimeImmutable
89  {
90  try {
91  $last_modified = (int) $this->flysystem_operator->lastModified($path);
92 
93  return new \DateTimeImmutable(date('Y-m-d H:i:s', $last_modified));
94  } catch (UnableToRetrieveMetadata) {
95  throw new IOException("Could not lookup timestamp of the file \"$path\".");
96  } catch (FilesystemException $ex) {
97  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
98  }
99  }
100 
101  public function getSize(string $path, int $unit): DataSize
102  {
103  try {
104  $byte_size = $this->flysystem_operator->fileSize($path);
105  return new DataSize($byte_size, $unit);
106  } catch (UnableToRetrieveMetadata) {
107  throw new FileNotFoundException("File \"$path\" not found.");
108  }
109  }
110 
118  public function setVisibility(string $path, string $visibility): bool
119  {
120  if (!$this->has($path)) {
121  throw new FileNotFoundException("Path \"$path\" not found.");
122  }
123 
124  $this->validateVisibility($visibility);
125 
126  try {
127  $this->flysystem_operator->setVisibility($path, $visibility);
128  } catch (\Throwable) {
129  return false;
130  }
131  return true;
132  }
133 
142  private function validateVisibility(string $visibility): void
143  {
144  if (strcmp($visibility, Visibility::PUBLIC_ACCESS) === 0) {
145  return;
146  }
147  if (strcmp($visibility, Visibility::PRIVATE_ACCESS) === 0) {
148  return;
149  }
150  throw new \InvalidArgumentException("The access must be 'public' or 'private' but '$visibility' was given.");
151  }
152 
168  public function getVisibility(string $path): string
169  {
170  if (!$this->has($path)) {
171  throw new FileNotFoundException("Path \"$path\" not found.");
172  }
173 
174  $visibility = $this->flysystem_operator->getVisibility($path);
175 
176  if ($visibility === false) {
177  throw new IOException("Could not determine visibility for path '$path'.");
178  }
179 
180  return $visibility;
181  }
182 
194  public function write(string $path, string $content): void
195  {
196  if ($this->flysystem_operator->has($path)) {
197  throw new FileAlreadyExistsException("File \"$path\" already exists.");
198  }
199  try {
200  $this->flysystem_operator->write($path, $content);
201  } catch (FilesystemException) {
202  throw new IOException(
203  "Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable."
204  );
205  }
206  }
207 
220  public function update(string $path, string $new_content): void
221  {
222  try {
223  $this->flysystem_operator->write($path, $new_content);
224  } catch (UnableToWriteFile $ex) {
225  throw new IOException(
226  "Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.",
227  0,
228  $ex
229  );
230  } catch (UnableToRetrieveMetadata $ex) {
231  throw new FileNotFoundException("File \"$path\" was not found update failed.", 0, $ex);
232  }
233  }
234 
245  public function put(string $path, string $content): void
246  {
247  if ($this->flysystem_operator->has($path)) {
248  $this->update($path, $content);
249  return;
250  }
251  $this->write($path, $content);
252  }
253 
264  public function delete(string $path): void
265  {
266  try {
267  $this->flysystem_operator->delete($path);
268  } catch (UnableToRetrieveMetadata) {
269  throw new FileNotFoundException("File \"$path\" was not found delete operation failed.");
270  } catch (UnableToDeleteFile) {
271  throw new IOException(
272  "Could not delete file \"$path\" because a general IO error occurred. Please check that your target is writable."
273  );
274  }
275  }
276 
288  public function readAndDelete(string $path): string
289  {
290  $content = $this->read($path);
291  $this->delete($path);
292 
293  return $content;
294  }
295 
308  public function rename(string $path, string $new_path): void
309  {
310  if ($this->flysystem_operator->has($new_path)) {
311  throw new IOException("File \"$new_path\" already exists.");
312  }
313  try {
314  $this->flysystem_operator->move($path, $new_path);
315  } catch (UnableToMoveFile) {
316  throw new IOException("Could not move file from \"$path\" to \"$new_path\".");
317  } catch (UnableToRetrieveMetadata) {
318  throw new FileNotFoundException("File \"$path\" not found.");
319  }
320  }
321 
333  public function copy(string $path, string $copy_path): void
334  {
335  if ($this->flysystem_operator->has($copy_path)) {
336  throw new FileAlreadyExistsException("File \"$copy_path\" already exists.");
337  }
338  try {
339  $this->flysystem_operator->copy($path, $copy_path);
340  } catch (UnableToCopyFile) {
341  throw new IOException(
342  "Could not copy file \"$path\" to destination \"$copy_path\" because a general IO error occurred. Please check that your destination is writable."
343  );
344  } catch (UnableToRetrieveMetadata) {
345  throw new FileNotFoundException("File source \"$path\" was not found copy failed.");
346  }
347  }
348 }
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:29
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.