ILIAS  trunk Revision v11.0_alpha-2645-g16283d3b3f8
ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess Class Reference

Fly system file access implementation. More...

+ Inheritance diagram for ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess:
+ Collaboration diagram for ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess:

Public Member Functions

 __construct (private FilesystemOperator $flysystem_operator)
 
 read (string $path)
 Reads a file content to a string. More...
 
 has (string $path)
 Checks whether a file exists. More...
 
 getMimeType (string $path)
 Get a files mime-type. More...
 
 getTimestamp (string $path)
 Get the timestamp of the file. More...
 
 getSize (string $path, int $unit)
 Get the size of a file. More...
 
 setVisibility (string $path, string $visibility)
 Sets the visibility for a file. More...
 
 getVisibility (string $path)
 Get the file visibility. More...
 
 write (string $path, string $content)
 Writes the content to a new file. More...
 
 update (string $path, string $new_content)
 Updates the content of a file. More...
 
 put (string $path, string $content)
 Creates a file or updates an existing one. More...
 
 delete (string $path)
 Deletes a file. More...
 
 readAndDelete (string $path)
 Reads the entire file content into a string and removes the file afterwards. More...
 
 rename (string $path, string $new_path)
 Moves a file from the source to the destination. More...
 
 copy (string $path, string $copy_path)
 Copy the source file to a destination. More...
 

Private Member Functions

 validateVisibility (string $visibility)
 Checks if the given visibility is valid an throws an exception otherwise. More...
 

Detailed Description

Fly system file access implementation.

Author
Nicolas Schäfli ns@st.nosp@m.uder.nosp@m.-raim.nosp@m.ann..nosp@m.ch
Fabian Schmid fabia.nosp@m.n@sr.nosp@m..solu.nosp@m.tion.nosp@m.s

Definition at line 43 of file FlySystemFileAccess.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::__construct ( private FilesystemOperator  $flysystem_operator)

Definition at line 45 of file FlySystemFileAccess.php.

47  {
48  }

Member Function Documentation

◆ copy()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::copy ( string  $path,
string  $copy_path 
)

Copy the source file to a destination.

Parameters
string$pathThe source path to the file which should be copied.
string$copy_pathThe destination path of the file copy.
Exceptions
FileNotFoundExceptionIf the source file does not exist.
FileAlreadyExistsExceptionIf the destination file already exists.
IOExceptionIf the file could not be copied to the destination.

Implements ILIAS\Filesystem\Provider\FileWriteAccess.

Definition at line 333 of file FlySystemFileAccess.php.

333  : 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  }
$path
Definition: ltiservices.php:29

◆ delete()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::delete ( string  $path)

Deletes a file.

Parameters
string$pathThe path to the file which should be deleted.
Exceptions
FileNotFoundExceptionIf the file was not found.
IOExceptionIf the file was found but the delete operation finished with errors.

Implements ILIAS\Filesystem\Provider\FileWriteAccess.

Definition at line 264 of file FlySystemFileAccess.php.

References $path.

264  : 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  }
$path
Definition: ltiservices.php:29

◆ getMimeType()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::getMimeType ( string  $path)

Get a files mime-type.

Parameters
string$pathThe file which should be used to get the mime-type.
Returns
string The mime-type of the file.
Exceptions
FileNotFoundExceptionIf the file is not found.
IOExceptionIf the mime-type could not be determined.

Implements ILIAS\Filesystem\Provider\FileReadAccess.

Definition at line 74 of file FlySystemFileAccess.php.

74  : 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  }
$path
Definition: ltiservices.php:29

◆ getSize()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::getSize ( string  $path,
int  $unit 
)

Get the size of a file.

The file size units are provided by the DataSize class.

Parameters
string$pathThe path to the file.
int$unitThe unit of the file size, which are defined in the DataSize class.
Exceptions
IOExceptionThrown if the file is not accessible or the underlying filesystem adapter failed.
FileNotFoundExceptionThrown if the specified file was not found.
See also
DataSize

Implements ILIAS\Filesystem\Provider\FileReadAccess.

Definition at line 101 of file FlySystemFileAccess.php.

101  : 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  }
$path
Definition: ltiservices.php:29

◆ getTimestamp()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::getTimestamp ( string  $path)

Get the timestamp of the file.

Parameters
string$pathThe path to the file.
Returns
The timestamp of the file.
Exceptions
FileNotFoundExceptionIf the file is not found.
IOExceptionIf the file can not be red.

Implements ILIAS\Filesystem\Provider\FileReadAccess.

Definition at line 88 of file FlySystemFileAccess.php.

References ILIAS\Repository\int().

88  : \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  }
$path
Definition: ltiservices.php:29
+ Here is the call graph for this function:

◆ getVisibility()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::getVisibility ( string  $path)

Get the file visibility.

The file visibility could be 'public' or 'private'.

Please note that the Visibility interface defines two constants PUBLIC_ACCESS and PRIVATE_ACCESS to ease the development process.

Parameters
string$pathThe path to the file which should be used.
Returns
string The string 'public' or 'private'.
Exceptions
FileNotFoundExceptionIf the file could not be found.
IOExceptionIf the underlying adapter failed to determine the visibility.

Implements ILIAS\Filesystem\Provider\FileReadAccess.

Definition at line 168 of file FlySystemFileAccess.php.

References ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess\has().

168  : 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  }
$path
Definition: ltiservices.php:29
has(string $path)
Checks whether a file exists.
+ Here is the call graph for this function:

◆ has()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::has ( string  $path)

Checks whether a file exists.

Parameters
string$pathThe file path which should be checked.
Returns
bool True if the file exists, otherwise false.

Implements ILIAS\Filesystem\Provider\FileReadAccess.

Definition at line 69 of file FlySystemFileAccess.php.

Referenced by ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess\getVisibility(), and ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess\setVisibility().

69  : bool
70  {
71  return $this->flysystem_operator->has($path);
72  }
$path
Definition: ltiservices.php:29
+ Here is the caller graph for this function:

◆ put()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::put ( string  $path,
string  $content 
)

Creates a file or updates an existing one.

Parameters
string$pathThe path to the file which should be created or updated.
string$contentThe content which should be written to the file.
Exceptions
IOExceptionIf the file could not be created or updated.

Implements ILIAS\Filesystem\Provider\FileWriteAccess.

Definition at line 245 of file FlySystemFileAccess.php.

References ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess\update(), and ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess\write().

245  : void
246  {
247  if ($this->flysystem_operator->has($path)) {
248  $this->update($path, $content);
249  return;
250  }
251  $this->write($path, $content);
252  }
$path
Definition: ltiservices.php:29
update(string $path, string $new_content)
Updates the content of a file.
write(string $path, string $content)
Writes the content to a new file.
+ Here is the call graph for this function:

◆ read()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::read ( string  $path)

Reads a file content to a string.

Parameters
string$pathThe path to the file which should be read.
Returns
string The file content.
Exceptions
FileNotFoundExceptionIf the file doesn't exist.
IOExceptionIf the file could not be red.

Implements ILIAS\Filesystem\Provider\FileReadAccess.

Definition at line 50 of file FlySystemFileAccess.php.

References ILIAS\Filesystem\Provider\FlySystem\Util\normalizeRelativePath().

Referenced by ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess\readAndDelete().

50  : string
51  {
52  try {
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  }
static normalizeRelativePath(string $path)
Definition: Util.php:30
$path
Definition: ltiservices.php:29
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readAndDelete()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::readAndDelete ( string  $path)

Reads the entire file content into a string and removes the file afterwards.

Parameters
string$pathThe file which should be red and removed.
Returns
string The entire file content.
Exceptions
FileNotFoundExceptionIf the file was not found.
IOExceptionIf the file could not red or deleted.

Implements ILIAS\Filesystem\Provider\FileWriteAccess.

Definition at line 288 of file FlySystemFileAccess.php.

References $path, and ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess\read().

288  : string
289  {
290  $content = $this->read($path);
291  $this->delete($path);
292 
293  return $content;
294  }
read(string $path)
Reads a file content to a string.
$path
Definition: ltiservices.php:29
+ Here is the call graph for this function:

◆ rename()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::rename ( string  $path,
string  $new_path 
)

Moves a file from the source to the destination.

Parameters
string$pathThe current path of the file which should be moved.
string$new_pathThe new path of the file.
Exceptions
FileNotFoundExceptionIf the source file is not found.
FileAlreadyExistsExceptionIf the destination file is already existing.
IOExceptionIf the file could not be moved.

Implements ILIAS\Filesystem\Provider\FileWriteAccess.

Definition at line 308 of file FlySystemFileAccess.php.

308  : 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  }
$path
Definition: ltiservices.php:29

◆ setVisibility()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::setVisibility ( string  $path,
string  $visibility 
)

Sets the visibility for a file.

Please note that the $visibility must 'public' or 'private'.

The Visibility interface provides two constants PUBLIC_ACCESS and PRIVATE_ACCESS. We strongly encourage the consumers of this API to use the constants.

Implements ILIAS\Filesystem\Provider\FileReadAccess.

Definition at line 118 of file FlySystemFileAccess.php.

References ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess\has(), and ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess\validateVisibility().

118  : 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  }
validateVisibility(string $visibility)
Checks if the given visibility is valid an throws an exception otherwise.
$path
Definition: ltiservices.php:29
has(string $path)
Checks whether a file exists.
+ Here is the call graph for this function:

◆ update()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::update ( string  $path,
string  $new_content 
)

Updates the content of a file.

Replaces the file content with a new one.

Parameters
string$pathThe path to the file which should be updated.
string$new_contentThe new file content.
Exceptions
FileNotFoundExceptionIf the file is not found.
IOExceptionIf the file could not be updated.

Implements ILIAS\Filesystem\Provider\FileWriteAccess.

Definition at line 220 of file FlySystemFileAccess.php.

Referenced by ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess\put().

220  : 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  }
$path
Definition: ltiservices.php:29
+ Here is the caller graph for this function:

◆ validateVisibility()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::validateVisibility ( string  $visibility)
private

Checks if the given visibility is valid an throws an exception otherwise.

If the visibility is valid no further actions are taken.

Parameters
string$visibilityThe visibility which should be validated.
Exceptions

Definition at line 142 of file FlySystemFileAccess.php.

References ILIAS\Filesystem\Visibility\PRIVATE_ACCESS, and ILIAS\Filesystem\Visibility\PUBLIC_ACCESS.

Referenced by ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess\setVisibility().

142  : 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  }
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:38
const PUBLIC_ACCESS
Public file visibility.
Definition: Visibility.php:34
+ Here is the caller graph for this function:

◆ write()

ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess::write ( string  $path,
string  $content 
)

Writes the content to a new file.

Parameters
string$pathThe path to the file which should be created.
string$contentThe content which should be written to the new file.
Exceptions
FileAlreadyExistsExceptionIf the file already exists.
IOExceptionIf the file could not be created or written.

Implements ILIAS\Filesystem\Provider\FileWriteAccess.

Definition at line 194 of file FlySystemFileAccess.php.

Referenced by ILIAS\Filesystem\Provider\FlySystem\FlySystemFileAccess\put().

194  : 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  }
$path
Definition: ltiservices.php:29
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: