コード例 #1
0
ファイル: File.php プロジェクト: electricretina/cicbase
    /**
     * @param mixed $source
     * @param string $targetType
     * @param array $convertedChildProperties
     * @param null|\TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration
     * @return null|object|\CIC\Cicbase\Domain\Model\File|\TYPO3\CMS\Extbase\Error\Error|\TYPO3\CMS\Extbase\Error\Error
     * @throws \TYPO3\CMS\Extbase\Configuration\Exception
     */
    public function convertFrom($source, $targetType, array $convertedChildProperties = array(), \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration = NULL)
    {
        $propertyPath = $configuration->getConfigurationValue('CIC\\Cicbase\\Property\\TypeConverter\\File', 'propertyPath');
        if (!$propertyPath) {
            $propertyPath = 'file';
            $key = '';
        } else {
            $key = $propertyPath;
        }
        if (!$this->fileFactory->wasUploadAttempted($propertyPath)) {
            $fileObject = $this->fileRepository->getHeld($key);
            if ($fileObject instanceof \CIC\Cicbase\Domain\Model\File) {
                return $fileObject;
            } else {
                // this is where we end up if no file upload was attempted (eg, form was submitted without a value
                // in the upload field, and we were unable to find a held file. In this case, return false, as though
                // nothing was ever posted.
                return NULL;
                // I thought this should return an error, at first, but instead we're going to treat this as though
                // nothing was posted at all... this allows for option file upload fields, I think.
                return new \TYPO3\CMS\Extbase\Error\Error('No file was uploaded.', 1336597083);
            }
        } else {
            // Otherwise, we create a new file object. Note that we use the fileFactory to turn $_FILE data into
            // a proper file object. Elsewhere, we use the fileRepository to retrieve file objects, even those that
            // haven't yet been persisted to the database;
            $allowedTypes = $configuration->getConfigurationValue('CIC\\Cicbase\\Property\\TypeConverter\\File', 'allowedTypes');
            $maxSize = $configuration->getConfigurationValue('CIC\\Cicbase\\Property\\TypeConverter\\File', 'maxSize');
            if (!$allowedTypes) {
                $allowedTypes = $this->settings['fileAllowedMime'];
            }
            if (!$maxSize) {
                $maxSize = $this->settings['fileMaxSize'];
            }
            // Too risky to use this type converter without some settings in place.
            if (!$maxSize) {
                throw new \TYPO3\CMS\Extbase\Configuration\Exception('Before you can use the file type converter, you must set a
				 fileMaxSize value in the settings section of your extension typoscript, or in the file type converter
				 configuration.', 1337043345);
            }
            if (!is_array($allowedTypes) && count($allowedTypes) == 0) {
                throw new \TYPO3\CMS\Extbase\Configuration\Exception('Before you can use the file type converter, you must configure
				 fileAllowedMime settings section of your extension typoscript, or in the file type converter
				 configuration.', 1337043346);
            }
            $result = $this->fileFactory->createFile($source, $propertyPath, $allowedTypes, $maxSize);
            return $result;
        }
    }
コード例 #2
0
 public function createFile(array $sourceData, $propertyPath, $allowedTypes, $maxSize)
 {
     $this->messages = new \TYPO3\CMS\Extbase\Error\Result();
     $this->propertyPath = $propertyPath;
     $key = $propertyPath ? $propertyPath : '';
     $this->settings = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS);
     $uploadedFileData = $this->getUploadedFileData();
     $this->handleUploadErrors($uploadedFileData);
     if ($this->messages->hasErrors()) {
         $this->fileRepository->clearHeld($key);
         return $this->messages->getFirstError();
     } else {
         if (!$this->settings['file']['dontValidateType']) {
             $this->validateType($uploadedFileData, $allowedTypes);
         }
         if (!$this->settings['file']['dontValidateName']) {
             $this->validateName($uploadedFileData);
         }
         if (!$this->settings['file']['dontValidateSize']) {
             $this->validateSize($uploadedFileData, $maxSize);
         }
     }
     if ($this->messages->hasErrors()) {
         $this->fileRepository->clearHeld($key);
         return $this->messages->getFirstError();
     } else {
         // ok to make a file object
         $pathInfo = pathinfo($uploadedFileData['tmp_name']);
         $fileObject = $this->objectManager->create('CIC\\Cicbase\\Domain\\Model\\File');
         $fileObject->setTitle($sourceData['title']);
         // TODO: Set a default title if it's not provided.
         $fileObject->setDescription($sourceData['description']);
         $fileObject->setIsSaved(false);
         $fileObject->setOwner($GLOBALS['TSFE']->fe_user->user['uid']);
         $fileObject->setSize($uploadedFileData['size']);
         $fileObject->setMimeType($uploadedFileData['type']);
         $fileObject->setOriginalFilename($uploadedFileData['name']);
         $fileObject->setPath($uploadedFileData['tmp_name']);
         $fileObject->setFilename($pathInfo['filename']);
         $fileObject->setCrdate(time());
         $results = $this->fileRepository->hold($fileObject, $key);
         return $results;
     }
 }