ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 UploadResult to the given destination.
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

 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 29 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 75 of file FileUploadImpl.php.

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

76  {
77  $this->processorManager = $processorManager;
78  $this->filesystems = $filesystems;
79  $this->globalHttpState = $globalHttpState;
80  $this->processed = false;
81  $this->moved = false;
82  $this->uploadResult = [];
83  $this->rejectedUploadResult = [];
84  }
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 351 of file FileUploadImpl.php.

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

352  {
353  $recursiveIterator = new RecursiveIteratorIterator(
355  $uploadedFiles,
356  RecursiveArrayIterator::CHILD_ARRAYS_ONLY
357  ),
358  RecursiveIteratorIterator::LEAVES_ONLY
359  );
360 
361  return iterator_to_array($recursiveIterator, false);
362  }
+ Here is the caller graph for this function:

◆ getResults()

ILIAS\FileUpload\FileUploadImpl::getResults ( )

Implements ILIAS\FileUpload\FileUpload.

Definition at line 321 of file FileUploadImpl.php.

322  {
323  if ($this->processed) {
324  return array_merge($this->uploadResult, $this->rejectedUploadResult);
325  }
326 
327  throw new IllegalStateException('Can not fetch results without processing the uploads.');
328  }

◆ hasBeenProcessed()

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

Implements ILIAS\FileUpload\FileUpload.

Definition at line 368 of file FileUploadImpl.php.

References ILIAS\FileUpload\FileUploadImpl\$processed.

369  {
370  return $this->processed;
371  }

◆ hasUploads()

ILIAS\FileUpload\FileUploadImpl::hasUploads ( )

Implements ILIAS\FileUpload\FileUpload.

Definition at line 334 of file FileUploadImpl.php.

References ILIAS\FileUpload\FileUploadImpl\flattenUploadedFiles().

335  {
336  if ($this->moved) {
337  return false;
338  }
339 
340  $uploadedFiles = $this->flattenUploadedFiles($this->globalHttpState->request()->getUploadedFiles());
341 
342  return (count($uploadedFiles) > 0);
343  }
+ Here is the call graph for this function:

◆ moveFilesTo()

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

Implements ILIAS\FileUpload\FileUpload.

Definition at line 117 of file FileUploadImpl.php.

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

118  {
119  if ($this->processed === false) {
120  throw new \RuntimeException('Can not move unprocessed files.');
121  }
122 
123  if ($this->moved === true) {
124  throw new \RuntimeException('Can not move the files a second time.');
125  }
126 
127  $filesystem = $this->selectFilesystem($location);
128  $tempResults = [];
129 
130  foreach ($this->uploadResult as $key => $uploadResult) {
131  if ($uploadResult->getStatus()->getCode() == ProcessingStatus::REJECTED) {
132  continue;
133  }
134 
135  try {
136  $path = $destination . '/' . $uploadResult->getName();
137  $filesystem->writeStream($path, Streams::ofPsr7Stream($this->uploadStreams[$key]));
138  $tempResults[] = $this->regenerateUploadResultWithPath($uploadResult, $path);
139  } catch (IOException $ex) {
140  $this->regenerateUploadResultWithCopyError($uploadResult, $ex->getMessage());
141  }
142  }
143 
144  $this->uploadResult = $tempResults;
145  $this->uploadStreams = null;
146  $this->moved = true;
147  }
const REJECTED
Upload got rejected by a processor.
$location
Definition: buildRTE.php:44
$destination
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.
$key
Definition: croninfo.php:18
static ofPsr7Stream(StreamInterface $stream)
Create a FileStream from a Psr7 compliant stream.
Definition: Streams.php:68
+ 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 UploadResult to the given destination.

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

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

90  {
91  if ($this->processed === false) {
92  throw new \RuntimeException('Can not move unprocessed files.');
93  }
94  $filesystem = $this->selectFilesystem($location);
95  $tempResults = [];
96 
97  if ($uploadResult->getStatus()->getCode() == ProcessingStatus::REJECTED) {
98  return false;
99  }
100 
101  try {
102  $path = rtrim($destination, "/") . '/' . ($file_name == "" ? $uploadResult->getName() : $file_name);
103  if ($override_existing && $filesystem->has($path)) {
104  $filesystem->delete($path);
105  }
106  $filesystem->writeStream($path, Streams::ofPsr7Stream($this->uploadStreams[$uploadResult->getPath()]));
107  $tempResults[] = $this->regenerateUploadResultWithPath($uploadResult, $path);
108  } catch (IOException $ex) {
109  $this->regenerateUploadResultWithCopyError($uploadResult, $ex->getMessage());
110  }
111  }
const REJECTED
Upload got rejected by a processor.
$location
Definition: buildRTE.php:44
$destination
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:68
+ 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 179 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().

180  {
181  return new UploadResult(
182  $result->getName(),
183  $result->getSize(),
184  $result->getMimeType(),
185  $result->getMetaData(),
186  new ProcessingStatus(ProcessingStatus::REJECTED, $errorReason),
187  ''
188  );
189  }
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 158 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().

159  {
160  return new UploadResult(
161  $result->getName(),
162  $result->getSize(),
163  $result->getMimeType(),
164  $result->getMetaData(),
165  $result->getStatus(),
166  $path
167  );
168  }
$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 232 of file FileUploadImpl.php.

References $file, $metadata, $result, GuzzleHttp\Psr7\$stream, ILIAS\FileUpload\FileUploadImpl\flattenUploadedFiles(), ILIAS\Filesystem\Stream\Streams\ofPsr7Stream(), ILIAS\FileUpload\FileUpload\process(), and ILIAS\FileUpload\FileUploadImpl\rejectFailedUpload().

233  {
234  if ($this->processed === false) {
235  $this->processorManager->with($preProcessor);
236  } else {
237  throw new IllegalStateException('Can not register processor after the upload was processed.');
238  }
239  }
+ 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 300 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().

301  {
302  //reject failed upload
303  $processingStatus = new ProcessingStatus(ProcessingStatus::REJECTED, 'Upload failed');
304  $extraMetadata = new ImmutableMapWrapper(new EntryLockingStringMap());
305  $result = new UploadResult(
306  $metadata->getFilename(),
307  $metadata->getUploadSize(),
308  $metadata->getMimeType(),
309  $extraMetadata,
310  $processingStatus,
311  ''
312  );
313 
314  $this->rejectedUploadResult[] = $result;
315  }
const REJECTED
Upload got rejected by a processor.
$result
$metadata['__DYNAMIC:1__']
+ 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 203 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().

204  {
205  switch ($location) {
207  return $this->filesystems->customizing();
208  case Location::STORAGE:
209  return $this->filesystems->storage();
210  case Location::WEB:
211  return $this->filesystems->web();
212  case Location::TEMPORARY:
213  return $this->filesystems->temp();
214  default:
215  throw new \InvalidArgumentException("No filesystem found for location code \"$location\"");
216  }
217  }
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 223 of file FileUploadImpl.php.

224  {
225  return \ilUtil::getUploadSizeLimitBytes();
226  }

Field Documentation

◆ $filesystems

Filesystems ILIAS\FileUpload\FileUploadImpl::$filesystems
private

Definition at line 39 of file FileUploadImpl.php.

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

◆ $globalHttpState

ILIAS\FileUpload\FileUploadImpl::$globalHttpState
private

Definition at line 43 of file FileUploadImpl.php.

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

◆ $moved

bool ILIAS\FileUpload\FileUploadImpl::$moved
private

Definition at line 51 of file FileUploadImpl.php.

◆ $processed

bool ILIAS\FileUpload\FileUploadImpl::$processed
private

Definition at line 47 of file FileUploadImpl.php.

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

◆ $processorManager

PreProcessorManager ILIAS\FileUpload\FileUploadImpl::$processorManager
private

Definition at line 35 of file FileUploadImpl.php.

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

◆ $rejectedUploadResult

ILIAS\FileUpload\FileUploadImpl::$rejectedUploadResult
private

Definition at line 59 of file FileUploadImpl.php.

◆ $uploadResult

ILIAS\FileUpload\FileUploadImpl::$uploadResult
private

Definition at line 55 of file FileUploadImpl.php.

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

◆ $uploadStreams

ILIAS\FileUpload\FileUploadImpl::$uploadStreams
private

Definition at line 63 of file FileUploadImpl.php.


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