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)
 @inheritDoc More...
 
 uploadSizeLimit ()
 @inheritDoc More...
 
 register (PreProcessor $preProcessor)
 @inheritDoc More...
 
 getResults ()
 @inheritDoc More...
 
 hasUploads ()
 @inheritDoc More...
 
 hasBeenProcessed ()
 
- Public Member Functions inherited from ILIAS\FileUpload\FileUpload
 moveFilesTo ($destination, $location=Location::STORAGE)
 Moves all uploaded files to the given destination after the processors had processed the files. 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. More...
 
 uploadSizeLimit ()
 Returns the current upload size limit in bytes. More...
 
 register (PreProcessor $preProcessor)
 Register a new preprocessor instance. More...
 
 process ()
 Invokes all preprocessors for each uploaded file in the sequence they got registered. More...
 
 getResults ()
 Returns the results of the processing and moving operation of the uploaded files. More...
 
 hasUploads ()
 Return (bool)true if one ore more file-uploads are in the current request, (bool)false if not. More...
 
 hasBeenProcessed ()
 Return (bool)true if the current upload has already been processed. More...
 

Protected Member Functions

 flattenUploadedFiles ($uploadedFiles)
 

Private Member Functions

 hardRemoveUpload (string $identifier)
 @description 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.

92 {
93 $this->processorManager = $processorManager;
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.

References ILIAS\FileUpload\FileUploadImpl\$filesystems, ILIAS\FileUpload\FileUploadImpl\$globalHttpState, ILIAS\FileUpload\FileUploadImpl\$processorManager, and ILIAS\Filesystem\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.

391 {
392 $recursiveIterator = new RecursiveIteratorIterator(
393 new RecursiveArrayIterator(
394 $uploadedFiles,
395 RecursiveArrayIterator::CHILD_ARRAYS_ONLY
396 ),
397 RecursiveIteratorIterator::LEAVES_ONLY
398 );
399
400 return iterator_to_array($recursiveIterator, false);
401 }

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

+ Here is the caller graph for this function:

◆ getResults()

ILIAS\FileUpload\FileUploadImpl::getResults ( )

@inheritDoc

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

@description 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.

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 }

◆ hasBeenProcessed()

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

Implements ILIAS\FileUpload\FileUpload.

Definition at line 407 of file FileUploadImpl.php.

References ILIAS\FileUpload\FileUploadImpl\$processed.

◆ hasUploads()

ILIAS\FileUpload\FileUploadImpl::hasUploads ( )

@inheritDoc

Implements ILIAS\FileUpload\FileUpload.

Definition at line 373 of file FileUploadImpl.php.

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 }

References ILIAS\FileUpload\FileUploadImpl\flattenUploadedFiles().

+ Here is the call graph for this function:

◆ moveFilesTo()

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

@inheritDoc

Implements ILIAS\FileUpload\FileUpload.

Definition at line 149 of file FileUploadImpl.php.

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 }
$location
Definition: buildRTE.php:44
const REJECTED
Upload got rejected by a processor.
regenerateUploadResultWithPath(UploadResult $result, $path)
Generate an exact copy of the result with the given path.
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.
static ofPsr7Stream(StreamInterface $stream)
Create a FileStream from a Psr7 compliant stream.
Definition: Streams.php:58

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().

+ 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.

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 }

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().

+ 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.

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 }
$result

References $result, and ILIAS\FileUpload\DTO\ProcessingStatus\REJECTED.

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

+ 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.

191 {
192 return new UploadResult(
193 $result->getName(),
194 $result->getSize(),
195 $result->getMimeType(),
196 $result->getMetaData(),
197 $result->getStatus(),
198 $path
199 );
200 }

References $result.

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

+ Here is the caller graph for this function:

◆ register()

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

@inheritDoc

Implements ILIAS\FileUpload\FileUpload.

Definition at line 264 of file FileUploadImpl.php.

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 }

◆ 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.

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 }

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

+ Here is the call 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
\ILIAS\Filesystem\Filesystem
See also
Location
Exceptions

InvalidArgumentException Thrown if the location is not a valid Location constant.

Definition at line 235 of file FileUploadImpl.php.

236 {
237 switch ($location) {
239 return $this->filesystems->customizing();
241 return $this->filesystems->storage();
242 case Location::WEB:
243 return $this->filesystems->web();
245 return $this->filesystems->temp();
246 default:
247 throw new \InvalidArgumentException("No filesystem found for location code \"$location\"");
248 }
249 }
const TEMPORARY
The ILIAS temporary directory.
Definition: Location.php:38
const CUSTOMIZING
The filesystem within the web root where all the skins and plugins are saved.
Definition: Location.php:33
const WEB
The filesystem within the ilias web root.
Definition: Location.php:23
const STORAGE
The filesystem outside of the ilias web root.
Definition: Location.php:28

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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ uploadSizeLimit()

ILIAS\FileUpload\FileUploadImpl::uploadSizeLimit ( )

@inheritDoc

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

UploadResult[] ILIAS\FileUpload\FileUploadImpl::$uploadResult
private

◆ $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: