ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
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...
 
 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 44 of file FlySystemFileAccess.php.

Constructor & Destructor Documentation

◆ __construct()

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

Definition at line 46 of file FlySystemFileAccess.php.

48 {
49 }

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 341 of file FlySystemFileAccess.php.

341 : void
342 {
343 if ($this->flysystem_operator->has($copy_path)) {
344 throw new FileAlreadyExistsException("File \"$copy_path\" already exists.");
345 }
346 try {
347 $this->flysystem_operator->copy($path, $copy_path);
348 } catch (UnableToCopyFile) {
349 throw new IOException(
350 "Could not copy file \"$path\" to destination \"$copy_path\" because a general IO error occurred. Please check that your destination is writable."
351 );
352 } catch (UnableToRetrieveMetadata) {
353 throw new FileNotFoundException("File source \"$path\" was not found copy failed.");
354 }
355 }

◆ 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 272 of file FlySystemFileAccess.php.

272 : void
273 {
274 try {
275 $this->flysystem_operator->delete($path);
276 } catch (UnableToRetrieveMetadata) {
277 throw new FileNotFoundException("File \"$path\" was not found delete operation failed.");
278 } catch (UnableToDeleteFile) {
279 throw new IOException(
280 "Could not delete file \"$path\" because a general IO error occurred. Please check that your target is writable."
281 );
282 }
283 }

◆ 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 82 of file FlySystemFileAccess.php.

82 : string
83 {
84 try {
85 $mimeType = $this->flysystem_operator->mimeType($path);
86 if ($mimeType === '') {
87 throw new IOException("Could not determine the MIME type of the file \"$path\".");
88 }
89
90 return $mimeType;
91 } catch (UnableToRetrieveMetadata $ex) {
92 throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
93 }
94 }

◆ 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 109 of file FlySystemFileAccess.php.

109 : DataSize
110 {
111 try {
112 $byte_size = $this->flysystem_operator->fileSize($path);
113 return new DataSize($byte_size, $unit);
114 } catch (UnableToRetrieveMetadata) {
115 throw new FileNotFoundException("File \"$path\" not found.");
116 }
117 }

◆ getTimestamp()

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

Get the timestamp of the file.

Parameters
string$pathThe path to the file.
Returns
\DateTimeImmutable 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 96 of file FlySystemFileAccess.php.

96 : \DateTimeImmutable
97 {
98 try {
99 $last_modified = (int) $this->flysystem_operator->lastModified($path);
100
101 return new \DateTimeImmutable(date('Y-m-d H:i:s', $last_modified));
102 } catch (UnableToRetrieveMetadata) {
103 throw new IOException("Could not lookup timestamp of the file \"$path\".");
104 } catch (FilesystemException $ex) {
105 throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
106 }
107 }

References ILIAS\UI\examples\Symbol\Glyph\Date\date(), and ILIAS\Repository\int().

+ 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 176 of file FlySystemFileAccess.php.

176 : string
177 {
178 if (!$this->has($path)) {
179 throw new FileNotFoundException("Path \"$path\" not found.");
180 }
181
182 $visibility = $this->flysystem_operator->getVisibility($path);
183
184 if ($visibility === false) {
185 throw new IOException("Could not determine visibility for path '$path'.");
186 }
187
188 return $visibility;
189 }
has(string $path)
Checks whether a file exists.

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

+ 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 70 of file FlySystemFileAccess.php.

70 : bool
71 {
72 try {
73 return $this->flysystem_operator->has($path);
74 } catch (CorruptedPathDetected) {
75 // Paths containing funky whitespace (e.g. a tab or a line break) are rejected
76 // by the path normalizer. Such a path can never be read, but it must not abort
77 // the caller either, since these paths do occur on disk.
78 return false;
79 }
80 }

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

+ 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 253 of file FlySystemFileAccess.php.

253 : void
254 {
255 if ($this->flysystem_operator->has($path)) {
256 $this->update($path, $content);
257 return;
258 }
259 $this->write($path, $content);
260 }
update(string $path, string $new_content)
Updates the content of a file.
write(string $path, string $content)
Writes the content to a new file.

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

+ 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 51 of file FlySystemFileAccess.php.

51 : string
52 {
53 try {
54 $path = Util::normalizeRelativePath($path);
55 if (!$this->flysystem_operator->has($path)) {
56 throw new \League\Flysystem\FileNotFoundException($path);
57 }
58 $result = $this->flysystem_operator->read($path);
59
60 if (empty($result)) {
61 throw new IOException("Could not access the file \"$path\".");
62 }
63
64 return $result;
65 } catch (\Throwable $ex) {
66 throw new FileNotFoundException("File \"$path\" not found.", 0, $ex);
67 }
68 }
static normalizeRelativePath(string $path)
Definition: Util.php:30

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

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

+ 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 296 of file FlySystemFileAccess.php.

296 : string
297 {
298 $content = $this->read($path);
299 $this->delete($path);
300
301 return $content;
302 }
read(string $path)
Reads a file content to a string.

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

+ 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 316 of file FlySystemFileAccess.php.

316 : void
317 {
318 if ($this->flysystem_operator->has($new_path)) {
319 throw new IOException("File \"$new_path\" already exists.");
320 }
321 try {
322 $this->flysystem_operator->move($path, $new_path);
323 } catch (UnableToMoveFile) {
324 throw new IOException("Could not move file from \"$path\" to \"$new_path\".");
325 } catch (UnableToRetrieveMetadata) {
326 throw new FileNotFoundException("File \"$path\" not found.");
327 }
328 }

◆ 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 126 of file FlySystemFileAccess.php.

126 : bool
127 {
128 if (!$this->has($path)) {
129 throw new FileNotFoundException("Path \"$path\" not found.");
130 }
131
132 $this->validateVisibility($visibility);
133
134 try {
135 $this->flysystem_operator->setVisibility($path, $visibility);
136 } catch (\Throwable) {
137 return false;
138 }
139 return true;
140 }
validateVisibility(string $visibility)
Checks if the given visibility is valid an throws an exception otherwise.

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

+ 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 228 of file FlySystemFileAccess.php.

228 : void
229 {
230 try {
231 $this->flysystem_operator->write($path, $new_content);
232 } catch (UnableToWriteFile $ex) {
233 throw new IOException(
234 "Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable.",
235 0,
236 $ex
237 );
238 } catch (UnableToRetrieveMetadata $ex) {
239 throw new FileNotFoundException("File \"$path\" was not found update failed.", 0, $ex);
240 }
241 }

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

+ 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

InvalidArgumentException Thrown if the given visibility was considered as invalid.

Definition at line 150 of file FlySystemFileAccess.php.

150 : void
151 {
152 if (strcmp($visibility, Visibility::PUBLIC_ACCESS) === 0) {
153 return;
154 }
155 if (strcmp($visibility, Visibility::PRIVATE_ACCESS) === 0) {
156 return;
157 }
158 throw new \InvalidArgumentException("The access must be 'public' or 'private' but '$visibility' was given.");
159 }
const PRIVATE_ACCESS
Private file visibility.
Definition: Visibility.php:38
const PUBLIC_ACCESS
Public file visibility.
Definition: Visibility.php:34

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

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

+ 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 202 of file FlySystemFileAccess.php.

202 : void
203 {
204 if ($this->flysystem_operator->has($path)) {
205 throw new FileAlreadyExistsException("File \"$path\" already exists.");
206 }
207 try {
208 $this->flysystem_operator->write($path, $content);
209 } catch (FilesystemException) {
210 throw new IOException(
211 "Could not write to file \"$path\" because a general IO error occurred. Please check that your destination is writable."
212 );
213 }
214 }

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

+ Here is the caller graph for this function:

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