ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
FlySystemFileAccess.php
Go to the documentation of this file.
1 <?php
2 
4 
13 
24 {
25 
29  private $flySystemFS;
30 
31 
38  {
39  $this->flySystemFS = $flySystemFS;
40  }
41 
42 
56  public function read($path)
57  {
58  $this->nonNull($path, 'path');
59 
60  try {
61  $result = $this->flySystemFS->read($path);
62 
63  if ($result === false) {
64  throw new IOException("Could not access the file \"$path\".");
65  }
66 
67  return $result;
68  } catch (\League\Flysystem\FileNotFoundException $ex) {
69  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
70  }
71  }
72 
73 
84  public function has($path)
85  {
86  $this->nonNull($path, 'path');
87 
88  return $this->flySystemFS->has($path);
89  }
90 
91 
102  public function getMimeType($path)
103  {
104  $this->nonNull($path, 'path');
105 
106  try {
107  $mimeType = $this->flySystemFS->getMimetype($path);
108  if ($mimeType === false) {
109  throw new IOException("Could not determine the MIME type of the file \"$path\".");
110  }
111 
112  return $mimeType;
113  } catch (\League\Flysystem\FileNotFoundException $ex) {
114  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
115  }
116  }
117 
118 
132  public function getTimestamp($path)
133  {
134  $this->nonNull($path, 'path');
135 
136  try {
137  $rawTimestamp = $this->flySystemFS->getTimestamp($path);
138  if ($rawTimestamp === false) {
139  throw new IOException("Could not lookup timestamp of the file \"$path\".");
140  }
141 
142  return new \DateTime($rawTimestamp);
143  } catch (\League\Flysystem\FileNotFoundException $ex) {
144  throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
145  }
146  }
147 
148 
166  public function getSize($path, $fileSizeUnit)
167  {
168  $this->nonNull($path, 'path');
169  $this->nonNull($fileSizeUnit, 'fileSizeUnit');
170 
171  try {
172  $byteSize = $this->flySystemFS->getSize($path);
173 
174  //check if the fly system adapter failed
175  if ($byteSize === false) {
176  throw new IOException("Could not calculate the file size of the file \"$path\".");
177  }
178 
179  $size = new DataSize($byteSize, $fileSizeUnit);
180  return $size;
181  } catch (\League\Flysystem\FileNotFoundException $ex) {
182  throw new FileNotFoundException("File \"$path\" not found.");
183  }
184  }
185 
186 
204  public function setVisibility($path, $visibility)
205  {
206  $this->nonNull($path, 'path');
207  $this->nonNull($visibility, 'visibility');
208 
209  if ($this->has($path) === false) {
210  throw new FileNotFoundException("Path \"$path\" not found.");
211  }
212 
213  $this->validateVisibility($visibility);
214 
215  return $this->flySystemFS->setVisibility($path, $visibility);
216  }
217 
218 
228  private function validateVisibility($visibility)
229  {
230  if (strcmp($visibility, Visibility::PUBLIC_ACCESS) !== 0 && strcmp($visibility, Visibility::PRIVATE_ACCESS) !== 0) {
231  throw new \InvalidArgumentException("The access must be 'public' or 'private' but '$visibility' was given.");
232  }
233  }
234 
235 
253  public function getVisibility($path)
254  {
255  $this->nonNull($path, 'path');
256 
257  if ($this->has($path) === false) {
258  throw new FileNotFoundException("Path \"$path\" not found.");
259  }
260 
261  $visibility = $this->flySystemFS->getVisibility($path);
262 
263  if ($visibility === false) {
264  throw new IOException("Could not determine visibility for path '$path'.");
265  }
266 
267  return $visibility;
268  }
269 
270 
285  public function write($path, $content)
286  {
287  $this->nonNull($path, 'path');
288  $this->nonNull($content, 'content');
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 
315  public function update($path, $newContent)
316  {
317  $this->nonNull($path, 'path');
318  $this->nonNull($newContent, 'newContent');
319 
320  try {
321  if ($this->flySystemFS->update($path, $newContent) === false) {
322  throw new IOException("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
323  }
324  } catch (\League\Flysystem\FileNotFoundException $ex) {
325  throw new FileNotFoundException("File \"$path\" was not found update failed.", 0, $ex);
326  }
327  }
328 
329 
343  public function put($path, $content)
344  {
345  $this->nonNull($path, 'path');
346  $this->nonNull($content, 'content');
347 
348  if ($this->flySystemFS->put($path, $content) === false) {
349  throw new IOException("Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.");
350  }
351  }
352 
353 
367  public function delete($path)
368  {
369  $this->nonNull($path, 'path');
370 
371  try {
372  if ($this->flySystemFS->delete($path) === false) {
373  throw new IOException("Could not delete file \"$path\" because a general IO error occurred. Please check that your target is writable.");
374  }
375  } catch (\League\Flysystem\FileNotFoundException $ex) {
376  throw new FileNotFoundException("File \"$path\" was not found delete operation failed.");
377  }
378  }
379 
380 
394  public function readAndDelete($path)
395  {
396  $this->nonNull($path, 'path');
397 
398  $content = $this->read($path);
399  $this->delete($path);
400 
401  return $content;
402  }
403 
404 
420  public function rename($path, $newPath)
421  {
422  $this->nonNull($path, 'path');
423  $this->nonNull($newPath, 'newPath');
424 
425  try {
426  if ($this->flySystemFS->rename($path, $newPath) === false) {
427  throw new IOException("Could not move file from \"$path\" to \"$newPath\".");
428  }
429  } catch (FileExistsException $ex) {
430  throw new FileAlreadyExistsException("File \"$newPath\" already exists.");
431  } catch (\League\Flysystem\FileNotFoundException $ex) {
432  throw new FileNotFoundException("File \"$path\" not found.");
433  }
434  }
435 
436 
452  public function copy($path, $copyPath)
453  {
454  $this->nonNull($path, 'path');
455  $this->nonNull($copyPath, 'copyPath');
456 
457  try {
458  if ($this->flySystemFS->copy($path, $copyPath) === false) {
459  throw new IOException("Could not copy file \"$path\" to destination \"$copyPath\" because a general IO error occurred. Please check that your destination is writable.");
460  }
461  } catch (FileExistsException $ex) {
462  throw new FileAlreadyExistsException("File destination \"$copyPath\" already exists copy failed.");
463  } catch (\League\Flysystem\FileNotFoundException $ex) {
464  throw new FileNotFoundException("File source \"$path\" was not found copy failed.");
465  }
466  }
467 
468 
478  private function nonNull($toCheck, $parameterName)
479  {
480  if (is_null($toCheck)) {
481  throw new \InvalidArgumentException("The parameter \"$parameterName\" must not be null.");
482  }
483  }
484 }
readAndDelete($path)
Reads the entire file content into a string and removes the file afterwards.
$size
Definition: RandomTest.php:84
$result
getTimestamp($path)
Get the timestamp (mtime) of the file.
copy($path, $copyPath)
Copy the source file to a destination.
Class DataSize.
Definition: DataSize.php:15
setVisibility($path, $visibility)
Sets the visibility for a file.
rename($path, $newPath)
Moves a file from the source to the destination.
put($path, $content)
Creates a file or updates an existing one.
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:30
update($path, $newContent)
Updates the content of a file.
getSize($path, $fileSizeUnit)
Get the size of a file.
write($path, $content)
Writes the content to a new file.
const PUBLIC_ACCESS
Public file visibility.
Definition: Visibility.php:25
__construct(FilesystemInterface $flySystemFS)
FlySystemFileAccess constructor.
nonNull($toCheck, $parameterName)
Validates that the passed argument is not null or an exception is thrown.
validateVisibility($visibility)
Checks if the given visibility is valid an throws an exception otherwise.