ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ILIAS\FileUpload\FileUploadImpl Class Reference

Class FileUploadImpl. More...

+ Inheritance diagram for ILIAS\FileUpload\FileUploadImpl:
+ Collaboration diagram for ILIAS\FileUpload\FileUploadImpl:

Public Member Functions

 __construct (PreProcessorManager $processorManager, Filesystems $filesystems, GlobalHttpState $globalHttpState)
 FileUploadImpl constructor. More...
 
 moveOneFileTo (UploadResult $uploadResult, string $destination, int $location=Location::STORAGE, string $file_name='', bool $override_existing=false)
 Moves a single File (the attributes, metadata and upload-status of which are contained in UploadResult) to the given destination.The destination is a relative path which refers to the path of the location.
Parameters
UploadResult$uploadResultWhich upload result do you want to move?
string$destinationWhere do you want to move the file?
int$locationLocation::[STORAGE|WEB|CUSTOMIZING]
string$file_nameDo you want to rename the file?
bool$override_existingOverride existing file with same name
More...
 
 moveFilesTo (string $destination, int $location=Location::STORAGE)
 
 uploadSizeLimit ()
 
 register (PreProcessor $preProcessor)
 
 process ()
 
 getResults ()
 
 hasUploads ()
 
 hasBeenProcessed ()
 Return (bool)true if the current upload has already been processed. More...
 

Protected Member Functions

 flattenUploadedFiles (array $uploadedFiles)
 

Private Member Functions

 hardRemoveUpload (string $identifier, string $rejection_message)
 This is the very last thing we can do if a preprocessor DENIEs an upload. More...
 
 regenerateUploadResultWithPath (UploadResult $result, string $path)
 Generate an exact copy of the result with the given path. More...
 
 regenerateUploadResultWithCopyError (UploadResult $result, string $errorReason)
 Creates a clone of the given result and set the status to rejected with the passed error message. More...
 
 selectFilesystem (int $location)
 Selects the correct filesystem by the given Location constant. More...
 
 rejectFailedUpload (Metadata $metadata)
 Reject a failed upload with the given metadata. More...
 

Private Attributes

PreProcessorManager $processorManager
 
Filesystems $filesystems
 
GlobalHttpState $globalHttpState
 
bool $processed
 
bool $moved
 
array $uploadResult
 
array $rejectedUploadResult
 
array $uploadStreams = null
 

Detailed Description

Class FileUploadImpl.

Author
Nicolas Schäfli ns@st.nosp@m.uder.nosp@m.-raim.nosp@m.ann..nosp@m.ch
Since
5.3
Version
1.0.0

Definition at line 48 of file FileUploadImpl.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\FileUpload\FileUploadImpl::__construct ( PreProcessorManager  $processorManager,
Filesystems  $filesystems,
GlobalHttpState  $globalHttpState 
)

FileUploadImpl constructor.

Parameters
PreProcessorManager$processorManagerThe processor manager which should be used.
Filesystems$filesystemsThe Filesystems implementation which should be used.
GlobalHttpState$globalHttpStateThe http implementation which should be used to detect the uploaded files.

Definition at line 78 of file FileUploadImpl.php.

References ILIAS\FileUpload\FileUploadImpl\$filesystems, ILIAS\FileUpload\FileUploadImpl\$globalHttpState, ILIAS\FileUpload\FileUploadImpl\$processorManager, and ILIAS\Filesystem\filesystems().

79  {
80  $this->processorManager = $processorManager;
81  $this->filesystems = $filesystems;
82  $this->globalHttpState = $globalHttpState;
83  $this->processed = false;
84  $this->moved = false;
85  $this->uploadResult = [];
86  $this->rejectedUploadResult = [];
87  }
PreProcessorManager $processorManager
static filesystems()
Returns the loaded filesystems.
+ Here is the call graph for this function:

Member Function Documentation

◆ flattenUploadedFiles()

ILIAS\FileUpload\FileUploadImpl::flattenUploadedFiles ( array  $uploadedFiles)
protected

Definition at line 366 of file FileUploadImpl.php.

Referenced by ILIAS\FileUpload\FileUploadImpl\hasUploads(), and ILIAS\FileUpload\FileUploadImpl\process().

366  : array
367  {
368  $recursiveIterator = new RecursiveIteratorIterator(
370  $uploadedFiles,
371  RecursiveArrayIterator::CHILD_ARRAYS_ONLY
372  ),
373  RecursiveIteratorIterator::LEAVES_ONLY
374  );
375 
376  return iterator_to_array($recursiveIterator, false);
377  }
+ Here is the caller graph for this function:

◆ getResults()

ILIAS\FileUpload\FileUploadImpl::getResults ( )

Implements ILIAS\FileUpload\FileUpload.

Definition at line 341 of file FileUploadImpl.php.

341  : array
342  {
343  if ($this->processed) {
344  return array_merge($this->uploadResult, $this->rejectedUploadResult);
345  }
346 
347  throw new IllegalStateException('Can not fetch results without processing the uploads.');
348  }

◆ hardRemoveUpload()

ILIAS\FileUpload\FileUploadImpl::hardRemoveUpload ( string  $identifier,
string  $rejection_message 
)
private

This is the very last thing we can do if a preprocessor DENIEs an upload.

This is a hard removal, not beautiful, but it works.

Definition at line 93 of file FileUploadImpl.php.

Referenced by ILIAS\FileUpload\FileUploadImpl\process().

93  : void
94  {
95  // we delete the file from the temporary directory and remove it from the global $_FILES array
96  $file_stream = $this->uploadStreams[$identifier];
97  $uri = $file_stream->getMetadata('uri');
98  $file_stream->close();
99  unlink($uri);
100  unset($this->uploadStreams[$identifier]);
101  unset($_FILES[$identifier]);
102  throw new IllegalStateException($rejection_message);
103  }
+ Here is the caller graph for this function:

◆ hasBeenProcessed()

ILIAS\FileUpload\FileUploadImpl::hasBeenProcessed ( )

Return (bool)true if the current upload has already been processed.

Since
5.3

Implements ILIAS\FileUpload\FileUpload.

Definition at line 380 of file FileUploadImpl.php.

References ILIAS\FileUpload\FileUploadImpl\$processed.

380  : bool
381  {
382  return $this->processed;
383  }

◆ hasUploads()

ILIAS\FileUpload\FileUploadImpl::hasUploads ( )

Implements ILIAS\FileUpload\FileUpload.

Definition at line 354 of file FileUploadImpl.php.

References ILIAS\FileUpload\FileUploadImpl\flattenUploadedFiles().

354  : bool
355  {
356  if ($this->moved) {
357  return false;
358  }
359 
360  $uploadedFiles = $this->flattenUploadedFiles($this->globalHttpState->request()->getUploadedFiles());
361 
362  return ($uploadedFiles !== []);
363  }
flattenUploadedFiles(array $uploadedFiles)
+ Here is the call graph for this function:

◆ moveFilesTo()

ILIAS\FileUpload\FileUploadImpl::moveFilesTo ( string  $destination,
int  $location = Location::STORAGE 
)

Implements ILIAS\FileUpload\FileUpload.

Definition at line 138 of file FileUploadImpl.php.

References ILIAS\LTI\ToolProvider\$key, $location, $path, ILIAS\Filesystem\Stream\Streams\ofPsr7Stream(), ILIAS\FileUpload\FileUploadImpl\regenerateUploadResultWithCopyError(), ILIAS\FileUpload\FileUploadImpl\regenerateUploadResultWithPath(), ILIAS\FileUpload\DTO\ProcessingStatus\REJECTED, and ILIAS\FileUpload\FileUploadImpl\selectFilesystem().

138  : void
139  {
140  if (!$this->processed) {
141  throw new \RuntimeException('Can not move unprocessed files.');
142  }
143 
144  if ($this->moved) {
145  throw new \RuntimeException('Can not move the files a second time.');
146  }
147 
148  $filesystem = $this->selectFilesystem($location);
149  $tempResults = [];
150 
151  foreach ($this->uploadResult as $key => $uploadResult) {
152  if ($uploadResult->getStatus()->getCode() == ProcessingStatus::REJECTED) {
153  continue;
154  }
155 
156  try {
157  $path = $destination . '/' . $uploadResult->getName();
158  $filesystem->writeStream($path, Streams::ofPsr7Stream($this->uploadStreams[$key]));
159  $tempResults[] = $this->regenerateUploadResultWithPath($uploadResult, $path);
160  } catch (IOException $ex) {
161  $this->regenerateUploadResultWithCopyError($uploadResult, $ex->getMessage());
162  }
163  }
164 
165  $this->uploadResult = $tempResults;
166  $this->uploadStreams = null;
167  $this->moved = true;
168  }
const REJECTED
Upload got rejected by a processor.
regenerateUploadResultWithCopyError(UploadResult $result, string $errorReason)
Creates a clone of the given result and set the status to rejected with the passed error message...
$location
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: buildRTE.php:22
selectFilesystem(int $location)
Selects the correct filesystem by the given Location constant.
$path
Definition: ltiservices.php:32
string $key
Consumer key/client ID value.
Definition: System.php:193
regenerateUploadResultWithPath(UploadResult $result, string $path)
Generate an exact copy of the result with the given path.
static ofPsr7Stream(StreamInterface $stream)
Create a FileStream from a Psr7 compliant stream.
Definition: Streams.php:82
+ Here is the call graph for this function:

◆ moveOneFileTo()

ILIAS\FileUpload\FileUploadImpl::moveOneFileTo ( UploadResult  $uploadResult,
string  $destination,
int  $location = Location::STORAGE,
string  $file_name = '',
bool  $override_existing = false 
)

Moves a single File (the attributes, metadata and upload-status of which are contained in UploadResult) to the given destination.The destination is a relative path which refers to the path of the location.

Parameters
UploadResult$uploadResultWhich upload result do you want to move?
string$destinationWhere do you want to move the file?
int$locationLocation::[STORAGE|WEB|CUSTOMIZING]
string$file_nameDo you want to rename the file?
bool$override_existingOverride existing file with same name

Implements ILIAS\FileUpload\FileUpload.

Definition at line 108 of file FileUploadImpl.php.

References $location, $path, ILIAS\FileUpload\DTO\UploadResult\getName(), ILIAS\FileUpload\DTO\UploadResult\getPath(), ILIAS\FileUpload\DTO\UploadResult\getStatus(), ILIAS\Filesystem\Stream\Streams\ofPsr7Stream(), ILIAS\FileUpload\FileUploadImpl\regenerateUploadResultWithCopyError(), ILIAS\FileUpload\FileUploadImpl\regenerateUploadResultWithPath(), ILIAS\FileUpload\DTO\ProcessingStatus\REJECTED, and ILIAS\FileUpload\FileUploadImpl\selectFilesystem().

108  : bool
109  {
110  if (!$this->processed) {
111  throw new \RuntimeException('Can not move unprocessed files.');
112  }
113  $filesystem = $this->selectFilesystem($location);
114  $tempResults = [];
115 
116  if ($uploadResult->getStatus()->getCode() == ProcessingStatus::REJECTED) {
117  return false;
118  }
119 
120  try {
121  $path = rtrim($destination, "/") . '/' . ($file_name == "" ? $uploadResult->getName() : $file_name);
122  if ($override_existing && $filesystem->has($path)) {
123  $filesystem->delete($path);
124  }
125  $filesystem->writeStream($path, Streams::ofPsr7Stream($this->uploadStreams[$uploadResult->getPath()]));
126  $tempResults[] = $this->regenerateUploadResultWithPath($uploadResult, $path);
127  } catch (IOException $ex) {
128  $this->regenerateUploadResultWithCopyError($uploadResult, $ex->getMessage());
129  }
130 
131  return true;
132  }
const REJECTED
Upload got rejected by a processor.
regenerateUploadResultWithCopyError(UploadResult $result, string $errorReason)
Creates a clone of the given result and set the status to rejected with the passed error message...
$location
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: buildRTE.php:22
selectFilesystem(int $location)
Selects the correct filesystem by the given Location constant.
$path
Definition: ltiservices.php:32
regenerateUploadResultWithPath(UploadResult $result, string $path)
Generate an exact copy of the result with the given path.
static ofPsr7Stream(StreamInterface $stream)
Create a FileStream from a Psr7 compliant stream.
Definition: Streams.php:82
+ Here is the call graph for this function:

◆ process()

ILIAS\FileUpload\FileUploadImpl::process ( )

Implements ILIAS\FileUpload\FileUpload.

Definition at line 264 of file FileUploadImpl.php.

References Vendor\Package\$e, ILIAS\FileUpload\DTO\ProcessingStatus\DENIED, ILIAS\FileUpload\FileUploadImpl\flattenUploadedFiles(), ILIAS\FileUpload\FileUploadImpl\hardRemoveUpload(), ILIAS\Filesystem\Stream\Streams\ofPsr7Stream(), and ILIAS\FileUpload\FileUploadImpl\rejectFailedUpload().

264  : void
265  {
266  if ($this->processed) {
267  throw new IllegalStateException('Can not reprocess the uploaded files.');
268  }
269 
270  $uploadedFiles = $this->globalHttpState->request()->getUploadedFiles();
271  $collectFilesFromNestedFields = $this->flattenUploadedFiles($uploadedFiles);
272  foreach ($collectFilesFromNestedFields as $file) {
273  $metadata = new Metadata($file->getClientFilename(), $file->getSize(), $file->getClientMediaType());
274  try {
275  $stream = Streams::ofPsr7Stream($file->getStream());
276  } catch (\RuntimeException $e) {
277  $this->rejectFailedUpload($metadata);
278  continue;
279  }
280 
281  // we take the temporary file name as an identifier as it is the only unique attribute.
282  $identifier = $file->getStream()->getMetadata('uri');
283 
284  $identifier = is_array($identifier) ? '' : $identifier;
285 
286  $this->uploadStreams[$identifier] = $stream;
287 
288  if ($file->getError() === UPLOAD_ERR_OK) {
289  $processingResult = $this->processorManager->process($stream, $metadata);
290 
291  // we do discard if the result is a DENIED that there is no further pissibility to process the file.
292  if ($processingResult->getCode() === ProcessingStatus::DENIED) {
293  $this->hardRemoveUpload($identifier, $processingResult->getMessage());
294  continue;
295  }
296 
297  $result = new UploadResult(
298  $metadata->getFilename(),
299  $metadata->getUploadSize(),
300  $metadata->getMimeType(),
301  $metadata->additionalMetaData(),
302  $processingResult,
303  is_string($identifier) ? $identifier : ''
304  );
305  $this->uploadResult[$identifier] = $result;
306  } else {
307  $this->rejectFailedUpload($metadata);
308  }
309  }
310 
311  $this->processed = true;
312  }
flattenUploadedFiles(array $uploadedFiles)
hardRemoveUpload(string $identifier, string $rejection_message)
This is the very last thing we can do if a preprocessor DENIEs an upload.
rejectFailedUpload(Metadata $metadata)
Reject a failed upload with the given metadata.
const DENIED
Upload got denied by a processor, the upload will be removed immediately.
static ofPsr7Stream(StreamInterface $stream)
Create a FileStream from a Psr7 compliant stream.
Definition: Streams.php:82
+ Here is the call graph for this function:

◆ regenerateUploadResultWithCopyError()

ILIAS\FileUpload\FileUploadImpl::regenerateUploadResultWithCopyError ( UploadResult  $result,
string  $errorReason 
)
private

Creates a clone of the given result and set the status to rejected with the passed error message.

Parameters
UploadResult$resultThe result which should be cloned.
string$errorReasonThe reason why the error occurred.
Returns
UploadResult The newly cloned rejected result.

Definition at line 200 of file FileUploadImpl.php.

References ILIAS\FileUpload\DTO\UploadResult\getMetaData(), ILIAS\FileUpload\DTO\UploadResult\getMimeType(), ILIAS\FileUpload\DTO\UploadResult\getName(), ILIAS\FileUpload\DTO\UploadResult\getSize(), and ILIAS\FileUpload\DTO\ProcessingStatus\REJECTED.

Referenced by ILIAS\FileUpload\FileUploadImpl\moveFilesTo(), and ILIAS\FileUpload\FileUploadImpl\moveOneFileTo().

200  : UploadResult
201  {
202  return new UploadResult(
203  $result->getName(),
204  $result->getSize(),
205  $result->getMimeType(),
206  $result->getMetaData(),
207  new ProcessingStatus(ProcessingStatus::REJECTED, $errorReason),
208  ''
209  );
210  }
const REJECTED
Upload got rejected by a processor.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ regenerateUploadResultWithPath()

ILIAS\FileUpload\FileUploadImpl::regenerateUploadResultWithPath ( UploadResult  $result,
string  $path 
)
private

Generate an exact copy of the result with the given path.

Parameters
UploadResult$resultThe result which should be cloned.
string$pathThe path which should be set on the result clone.
Returns
UploadResult The cloned result with the given path.

Definition at line 179 of file FileUploadImpl.php.

References $path, ILIAS\FileUpload\DTO\UploadResult\getMetaData(), ILIAS\FileUpload\DTO\UploadResult\getMimeType(), ILIAS\FileUpload\DTO\UploadResult\getName(), ILIAS\FileUpload\DTO\UploadResult\getSize(), and ILIAS\FileUpload\DTO\UploadResult\getStatus().

Referenced by ILIAS\FileUpload\FileUploadImpl\moveFilesTo(), and ILIAS\FileUpload\FileUploadImpl\moveOneFileTo().

179  : UploadResult
180  {
181  return new UploadResult(
182  $result->getName(),
183  $result->getSize(),
184  $result->getMimeType(),
185  $result->getMetaData(),
186  $result->getStatus(),
187  $path
188  );
189  }
$path
Definition: ltiservices.php:32
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ register()

ILIAS\FileUpload\FileUploadImpl::register ( PreProcessor  $preProcessor)

Implements ILIAS\FileUpload\FileUpload.

Definition at line 251 of file FileUploadImpl.php.

251  : void
252  {
253  if (!$this->processed) {
254  $this->processorManager->with($preProcessor);
255  } else {
256  throw new IllegalStateException('Can not register processor after the upload was processed.');
257  }
258  }

◆ rejectFailedUpload()

ILIAS\FileUpload\FileUploadImpl::rejectFailedUpload ( Metadata  $metadata)
private

Reject a failed upload with the given metadata.

Parameters
Metadata$metadataThe metadata used to create the rejected result.

Definition at line 320 of file FileUploadImpl.php.

References ILIAS\FileUpload\DTO\Metadata\getFilename(), ILIAS\FileUpload\DTO\Metadata\getMimeType(), ILIAS\FileUpload\DTO\Metadata\getUploadSize(), and ILIAS\FileUpload\DTO\ProcessingStatus\REJECTED.

Referenced by ILIAS\FileUpload\FileUploadImpl\process().

320  : void
321  {
322  //reject failed upload
323  $processingStatus = new ProcessingStatus(ProcessingStatus::REJECTED, 'Upload failed');
324  $extraMetadata = new ImmutableMapWrapper(new EntryLockingStringMap());
325  $result = new UploadResult(
326  $metadata->getFilename(),
327  $metadata->getUploadSize(),
328  $metadata->getMimeType(),
329  $extraMetadata,
330  $processingStatus,
331  ''
332  );
333 
334  $this->rejectedUploadResult[] = $result;
335  }
const REJECTED
Upload got rejected by a processor.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ selectFilesystem()

ILIAS\FileUpload\FileUploadImpl::selectFilesystem ( int  $location)
private

Selects the correct filesystem by the given Location constant.

Parameters
int$locationThe storage location constant defined within the Location interface.
See also
Location
Exceptions

Definition at line 222 of file FileUploadImpl.php.

References ILIAS\FileUpload\Location\CUSTOMIZING, ILIAS\Filesystem\filesystems(), ILIAS\FileUpload\Location\STORAGE, ILIAS\FileUpload\Location\TEMPORARY, and ILIAS\FileUpload\Location\WEB.

Referenced by ILIAS\FileUpload\FileUploadImpl\moveFilesTo(), and ILIAS\FileUpload\FileUploadImpl\moveOneFileTo().

222  : Filesystem
223  {
224  switch ($location) {
226  return $this->filesystems->customizing();
227  case Location::STORAGE:
228  return $this->filesystems->storage();
229  case Location::WEB:
230  return $this->filesystems->web();
231  case Location::TEMPORARY:
232  return $this->filesystems->temp();
233  default:
234  throw new \InvalidArgumentException("No filesystem found for location code \"$location\"");
235  }
236  }
const STORAGE
The filesystem outside of the ilias web root.
Definition: Location.php:40
$location
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: buildRTE.php:22
static filesystems()
Returns the loaded filesystems.
const CUSTOMIZING
The filesystem within the web root where all the skins and plugins are saved.
Definition: Location.php:45
const TEMPORARY
The ILIAS temporary directory.
Definition: Location.php:50
Class FlySystemFileAccessTest disabled disabled disabled.
const WEB
The filesystem within the ilias web root.
Definition: Location.php:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ uploadSizeLimit()

ILIAS\FileUpload\FileUploadImpl::uploadSizeLimit ( )

Implements ILIAS\FileUpload\FileUpload.

Definition at line 242 of file FileUploadImpl.php.

References ilFileUtils\getUploadSizeLimitBytes().

242  : int
243  {
245  }
static getUploadSizeLimitBytes()
+ Here is the call graph for this function:

Field Documentation

◆ $filesystems

Filesystems ILIAS\FileUpload\FileUploadImpl::$filesystems
private

Definition at line 51 of file FileUploadImpl.php.

Referenced by ILIAS\FileUpload\FileUploadImpl\__construct().

◆ $globalHttpState

GlobalHttpState ILIAS\FileUpload\FileUploadImpl::$globalHttpState
private

Definition at line 52 of file FileUploadImpl.php.

Referenced by ILIAS\FileUpload\FileUploadImpl\__construct().

◆ $moved

bool ILIAS\FileUpload\FileUploadImpl::$moved
private

Definition at line 54 of file FileUploadImpl.php.

◆ $processed

bool ILIAS\FileUpload\FileUploadImpl::$processed
private

Definition at line 53 of file FileUploadImpl.php.

Referenced by ILIAS\FileUpload\FileUploadImpl\hasBeenProcessed().

◆ $processorManager

PreProcessorManager ILIAS\FileUpload\FileUploadImpl::$processorManager
private

Definition at line 50 of file FileUploadImpl.php.

Referenced by ILIAS\FileUpload\FileUploadImpl\__construct().

◆ $rejectedUploadResult

array ILIAS\FileUpload\FileUploadImpl::$rejectedUploadResult
private

Definition at line 62 of file FileUploadImpl.php.

◆ $uploadResult

array ILIAS\FileUpload\FileUploadImpl::$uploadResult
private

Definition at line 58 of file FileUploadImpl.php.

◆ $uploadStreams

array ILIAS\FileUpload\FileUploadImpl::$uploadStreams = null
private

Definition at line 66 of file FileUploadImpl.php.


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