Neos\Flow\ResourceManagement\ResourceManager::importUploadedResource PHP Метод

importUploadedResource() публичный Метод

On a successful import this method returns a PersistentResource object representing the newly imported persistent resource.
public importUploadedResource ( array $uploadInfo, string $collectionName = self::DEFAULT_PERSISTENT_COLLECTION_NAME ) : PersistentResource
$uploadInfo array An array detailing the resource to import (expected keys: name, tmp_name)
$collectionName string Name of the collection this uploaded resource should be added to
Результат PersistentResource A resource object representing the imported resource
    public function importUploadedResource(array $uploadInfo, $collectionName = self::DEFAULT_PERSISTENT_COLLECTION_NAME)
    {
        $this->initialize();
        if (!isset($this->collections[$collectionName])) {
            throw new Exception(sprintf('Tried to import an uploaded file into the resource collection "%s" but no such collection exists. Please check your settings and HTML forms.', $collectionName), 1375197544);
        }
        /* @var CollectionInterface $collection */
        $collection = $this->collections[$collectionName];
        try {
            $uploadedFile = $this->prepareUploadedFileForImport($uploadInfo);
            $resource = $collection->importResource($uploadedFile['filepath']);
            $resource->setFilename($uploadedFile['filename']);
        } catch (Exception $exception) {
            throw new Exception(sprintf('Importing an uploaded file into the resource collection "%s" failed.', $collectionName), 1375197680, $exception);
        }
        $this->resourceRepository->add($resource);
        $this->systemLogger->log(sprintf('Successfully imported the uploaded file "%s" into the resource collection "%s" (storage: "%s", a %s. SHA1: %s)', $resource->getFilename(), $collectionName, $this->collections[$collectionName]->getStorage()->getName(), get_class($this->collections[$collectionName]->getStorage()), $resource->getSha1()), LOG_DEBUG);
        return $resource;
    }

Usage Example

 /**
  * @param array $source
  * @param PropertyMappingConfigurationInterface $configuration
  * @return PersistentResource|FlowError
  * @throws \Exception
  */
 protected function handleFileUploads(array $source, PropertyMappingConfigurationInterface $configuration = null)
 {
     if (!isset($source['error']) || $source['error'] === \UPLOAD_ERR_NO_FILE) {
         if (isset($source['originallySubmittedResource']) && isset($source['originallySubmittedResource']['__identity'])) {
             return $this->persistenceManager->getObjectByIdentifier($source['originallySubmittedResource']['__identity'], PersistentResource::class);
         }
         return null;
     }
     if ($source['error'] !== \UPLOAD_ERR_OK) {
         switch ($source['error']) {
             case \UPLOAD_ERR_INI_SIZE:
             case \UPLOAD_ERR_FORM_SIZE:
             case \UPLOAD_ERR_PARTIAL:
                 return new FlowError(Files::getUploadErrorMessage($source['error']), 1264440823);
             default:
                 $this->systemLogger->log(sprintf('A server error occurred while converting an uploaded resource: "%s"', Files::getUploadErrorMessage($source['error'])), LOG_ERR);
                 return new FlowError('An error occurred while uploading. Please try again or contact the administrator if the problem remains', 1340193849);
         }
     }
     if (isset($this->convertedResources[$source['tmp_name']])) {
         return $this->convertedResources[$source['tmp_name']];
     }
     try {
         $resource = $this->resourceManager->importUploadedResource($source, $this->getCollectionName($source, $configuration));
         $this->convertedResources[$source['tmp_name']] = $resource;
         return $resource;
     } catch (\Exception $exception) {
         $this->systemLogger->log('Could not import an uploaded file', LOG_WARNING);
         $this->systemLogger->logException($exception);
         return new FlowError('During import of an uploaded file an error occurred. See log for more details.', 1264517906);
     }
 }