ILIAS  release_7 Revision v7.30-3-g800a261c036
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, $destination, $location=Location::STORAGE, $file_name='', $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
Returns
void
More...
 
 moveFilesTo ($destination, $location=Location::STORAGE)
 
 uploadSizeLimit ()
 
 register (PreProcessor $preProcessor)
 
 getResults ()
 
 hasUploads ()
 
 hasBeenProcessed ()
 
- Public Member Functions inherited from ILIAS\FileUpload\FileUpload
 process ()
 Invokes all preprocessors for each uploaded file in the sequence they got registered. More...
 

Protected Member Functions

 flattenUploadedFiles ($uploadedFiles)
 

Private Member Functions

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

Private Attributes

 $processorManager
 
 $filesystems
 
 $globalHttpState
 
 $processed
 
 $moved
 
 $uploadResult
 
 $rejectedUploadResult
 
 $uploadStreams
 

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 45 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 91 of file FileUploadImpl.php.

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

92  {
93  $this->processorManager = $processorManager;
94  $this->filesystems = $filesystems;
95  $this->globalHttpState = $globalHttpState;
96  $this->processed = false;
97  $this->moved = false;
98  $this->uploadResult = [];
99  $this->rejectedUploadResult = [];
100  }
static filesystems()
Returns the loaded filesystems.
+ Here is the call graph for this function:

Member Function Documentation

◆ flattenUploadedFiles()

ILIAS\FileUpload\FileUploadImpl::flattenUploadedFiles (   $uploadedFiles)
protected
Parameters
array$uploadedFiles
Returns
UploadedFileInterface[]

Definition at line 390 of file FileUploadImpl.php.

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

391  {
392  $recursiveIterator = new RecursiveIteratorIterator(
394  $uploadedFiles,
395  RecursiveArrayIterator::CHILD_ARRAYS_ONLY
396  ),
397  RecursiveIteratorIterator::LEAVES_ONLY
398  );
399 
400  return iterator_to_array($recursiveIterator, false);
401  }
+ Here is the caller graph for this function:

◆ getResults()

ILIAS\FileUpload\FileUploadImpl::getResults ( )

Implements ILIAS\FileUpload\FileUpload.

Definition at line 360 of file FileUploadImpl.php.

361  {
362  if ($this->processed) {
363  return array_merge($this->uploadResult, $this->rejectedUploadResult);
364  }
365 
366  throw new IllegalStateException('Can not fetch results without processing the uploads.');
367  }

◆ hardRemoveUpload()

ILIAS\FileUpload\FileUploadImpl::hardRemoveUpload ( string  $identifier)
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 106 of file FileUploadImpl.php.

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

106  : void
107  {
108  // we delete the file from the temporary directory and remove it from the global $_FILES array
109  $file_stream = $this->uploadStreams[$identifier];
110  $uri = $file_stream->getMetadata('uri');
111  $file_stream->close();
112  unlink($uri);
113  unset($this->uploadStreams[$identifier]);
114  unset($_FILES[$identifier]);
115  throw new IllegalStateException("File upload removed due to security reasons.");
116  }
+ Here is the caller graph for this function:

◆ hasBeenProcessed()

ILIAS\FileUpload\FileUploadImpl::hasBeenProcessed ( )
Returns
bool

Implements ILIAS\FileUpload\FileUpload.

Definition at line 407 of file FileUploadImpl.php.

References ILIAS\FileUpload\FileUploadImpl\$processed.

408  {
409  return $this->processed;
410  }

◆ hasUploads()

ILIAS\FileUpload\FileUploadImpl::hasUploads ( )

Implements ILIAS\FileUpload\FileUpload.

Definition at line 373 of file FileUploadImpl.php.

References ILIAS\FileUpload\FileUploadImpl\flattenUploadedFiles().

374  {
375  if ($this->moved) {
376  return false;
377  }
378 
379  $uploadedFiles = $this->flattenUploadedFiles($this->globalHttpState->request()->getUploadedFiles());
380 
381  return (count($uploadedFiles) > 0);
382  }
+ Here is the call graph for this function:

◆ moveFilesTo()

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

Implements ILIAS\FileUpload\FileUpload.

Definition at line 149 of file FileUploadImpl.php.

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

150  {
151  if ($this->processed === false) {
152  throw new \RuntimeException('Can not move unprocessed files.');
153  }
154 
155  if ($this->moved === true) {
156  throw new \RuntimeException('Can not move the files a second time.');
157  }
158 
159  $filesystem = $this->selectFilesystem($location);
160  $tempResults = [];
161 
162  foreach ($this->uploadResult as $key => $uploadResult) {
163  if ($uploadResult->getStatus()->getCode() == ProcessingStatus::REJECTED) {
164  continue;
165  }
166 
167  try {
168  $path = $destination . '/' . $uploadResult->getName();
169  $filesystem->writeStream($path, Streams::ofPsr7Stream($this->uploadStreams[$key]));
170  $tempResults[] = $this->regenerateUploadResultWithPath($uploadResult, $path);
171  } catch (IOException $ex) {
172  $this->regenerateUploadResultWithCopyError($uploadResult, $ex->getMessage());
173  }
174  }
175 
176  $this->uploadResult = $tempResults;
177  $this->uploadStreams = null;
178  $this->moved = true;
179  }
const REJECTED
Upload got rejected by a processor.
$location
Definition: buildRTE.php:44
regenerateUploadResultWithCopyError(UploadResult $result, $errorReason)
Creates a clone of the given result and set the status to rejected with the passed error message...
selectFilesystem($location)
Selects the correct filesystem by the given Location constant.
regenerateUploadResultWithPath(UploadResult $result, $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:58
+ Here is the call graph for this function:

◆ moveOneFileTo()

ILIAS\FileUpload\FileUploadImpl::moveOneFileTo ( UploadResult  $uploadResult,
  $destination,
  $location = Location::STORAGE,
  $file_name = '',
  $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
Returns
void

Implements ILIAS\FileUpload\FileUpload.

Definition at line 121 of file FileUploadImpl.php.

References $location, 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().

122  {
123  if ($this->processed === false) {
124  throw new \RuntimeException('Can not move unprocessed files.');
125  }
126  $filesystem = $this->selectFilesystem($location);
127  $tempResults = [];
128 
129  if ($uploadResult->getStatus()->getCode() == ProcessingStatus::REJECTED) {
130  return false;
131  }
132 
133  try {
134  $path = rtrim($destination, "/") . '/' . ($file_name == "" ? $uploadResult->getName() : $file_name);
135  if ($override_existing && $filesystem->has($path)) {
136  $filesystem->delete($path);
137  }
138  $filesystem->writeStream($path, Streams::ofPsr7Stream($this->uploadStreams[$uploadResult->getPath()]));
139  $tempResults[] = $this->regenerateUploadResultWithPath($uploadResult, $path);
140  } catch (IOException $ex) {
141  $this->regenerateUploadResultWithCopyError($uploadResult, $ex->getMessage());
142  }
143  }
const REJECTED
Upload got rejected by a processor.
$location
Definition: buildRTE.php:44
regenerateUploadResultWithCopyError(UploadResult $result, $errorReason)
Creates a clone of the given result and set the status to rejected with the passed error message...
selectFilesystem($location)
Selects the correct filesystem by the given Location constant.
regenerateUploadResultWithPath(UploadResult $result, $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:58
+ Here is the call graph for this function:

◆ regenerateUploadResultWithCopyError()

ILIAS\FileUpload\FileUploadImpl::regenerateUploadResultWithCopyError ( UploadResult  $result,
  $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 211 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().

212  {
213  return new UploadResult(
214  $result->getName(),
215  $result->getSize(),
216  $result->getMimeType(),
217  $result->getMetaData(),
218  new ProcessingStatus(ProcessingStatus::REJECTED, $errorReason),
219  ''
220  );
221  }
const REJECTED
Upload got rejected by a processor.
$result
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ regenerateUploadResultWithPath()

ILIAS\FileUpload\FileUploadImpl::regenerateUploadResultWithPath ( UploadResult  $result,
  $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 190 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\UploadResult\getStatus().

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

191  {
192  return new UploadResult(
193  $result->getName(),
194  $result->getSize(),
195  $result->getMimeType(),
196  $result->getMetaData(),
197  $result->getStatus(),
198  $path
199  );
200  }
$result
+ 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 264 of file FileUploadImpl.php.

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

265  {
266  if ($this->processed === false) {
267  $this->processorManager->with($preProcessor);
268  } else {
269  throw new IllegalStateException('Can not register processor after the upload was processed.');
270  }
271  }
+ Here is the call graph for this function:

◆ rejectFailedUpload()

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

Reject a failed upload with the given metadata.

Parameters
UploadedFileInterface$file
Metadata$metadataThe metadata used to create the rejected result.
Returns
void

Definition at line 339 of file FileUploadImpl.php.

References $result, 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\register().

340  {
341  //reject failed upload
342  $processingStatus = new ProcessingStatus(ProcessingStatus::REJECTED, 'Upload failed');
343  $extraMetadata = new ImmutableMapWrapper(new EntryLockingStringMap());
344  $result = new UploadResult(
345  $metadata->getFilename(),
346  $metadata->getUploadSize(),
347  $metadata->getMimeType(),
348  $extraMetadata,
349  $processingStatus,
350  ''
351  );
352 
353  $this->rejectedUploadResult[] = $result;
354  }
const REJECTED
Upload got rejected by a processor.
$result
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ selectFilesystem()

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

Selects the correct filesystem by the given Location constant.

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

Definition at line 235 of file FileUploadImpl.php.

References $location, 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().

236  {
237  switch ($location) {
239  return $this->filesystems->customizing();
240  case Location::STORAGE:
241  return $this->filesystems->storage();
242  case Location::WEB:
243  return $this->filesystems->web();
244  case Location::TEMPORARY:
245  return $this->filesystems->temp();
246  default:
247  throw new \InvalidArgumentException("No filesystem found for location code \"$location\"");
248  }
249  }
const STORAGE
The filesystem outside of the ilias web root.
Definition: Location.php:28
$location
Definition: buildRTE.php:44
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:33
const TEMPORARY
The ILIAS temporary directory.
Definition: Location.php:38
const WEB
The filesystem within the ilias web root.
Definition: Location.php:23
+ 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 255 of file FileUploadImpl.php.

256  {
257  return \ilUtil::getUploadSizeLimitBytes();
258  }

Field Documentation

◆ $filesystems

Filesystems ILIAS\FileUpload\FileUploadImpl::$filesystems
private

Definition at line 55 of file FileUploadImpl.php.

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

◆ $globalHttpState

ILIAS\FileUpload\FileUploadImpl::$globalHttpState
private

Definition at line 59 of file FileUploadImpl.php.

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

◆ $moved

bool ILIAS\FileUpload\FileUploadImpl::$moved
private

Definition at line 67 of file FileUploadImpl.php.

◆ $processed

bool ILIAS\FileUpload\FileUploadImpl::$processed
private

Definition at line 63 of file FileUploadImpl.php.

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

◆ $processorManager

PreProcessorManager ILIAS\FileUpload\FileUploadImpl::$processorManager
private

Definition at line 51 of file FileUploadImpl.php.

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

◆ $rejectedUploadResult

ILIAS\FileUpload\FileUploadImpl::$rejectedUploadResult
private

Definition at line 75 of file FileUploadImpl.php.

◆ $uploadResult

ILIAS\FileUpload\FileUploadImpl::$uploadResult
private

Definition at line 71 of file FileUploadImpl.php.

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

◆ $uploadStreams

ILIAS\FileUpload\FileUploadImpl::$uploadStreams
private

Definition at line 79 of file FileUploadImpl.php.


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