ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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

 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.

76 {
77 $this->processorManager = $processorManager;
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.

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

352 {
353 $recursiveIterator = new RecursiveIteratorIterator(
354 new RecursiveArrayIterator(
355 $uploadedFiles,
356 RecursiveArrayIterator::CHILD_ARRAYS_ONLY
357 ),
358 RecursiveIteratorIterator::LEAVES_ONLY
359 );
360
361 return iterator_to_array($recursiveIterator, false);
362 }

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

◆ hasUploads()

ILIAS\FileUpload\FileUploadImpl::hasUploads ( )

@inheritDoc

Implements ILIAS\FileUpload\FileUpload.

Definition at line 334 of file FileUploadImpl.php.

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 }

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

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 }
$path
Definition: aliased.php:25
$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:69
$key
Definition: croninfo.php:18
$destination

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

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

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 }

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

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

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 }
$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 158 of file FileUploadImpl.php.

159 {
160 return new UploadResult(
161 $result->getName(),
162 $result->getSize(),
163 $result->getMimeType(),
164 $result->getMetaData(),
165 $result->getStatus(),
166 $path
167 );
168 }

References $path, and $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 232 of file FileUploadImpl.php.

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 }

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

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 }
$metadata['__DYNAMIC:1__']

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

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

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

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

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