TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
FileUpload.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Domain\Model\FormElements;
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
24 
31 {
32 
37  public function initializeFormElement()
38  {
39  $this->setDataType('TYPO3\CMS\Extbase\Domain\Model\FileReference');
40  }
41 
59  public function onBuildingFinished()
60  {
62  $propertyMappingConfiguration = $this->getRootForm()->getProcessingRule($this->getIdentifier())->getPropertyMappingConfiguration();
63 
64  $mimeTypeValidator = GeneralUtility::makeInstance(ObjectManager::class)
65  ->get(MimeTypeValidator::class, ['allowedMimeTypes' => $this->properties['allowedMimeTypes']]);
66  $uploadConfiguration = [
69  ];
70 
71  $saveToFileMountIdentifier = (isset($this->properties['saveToFileMount'])) ? $this->properties['saveToFileMount'] : null;
72  if ($this->checkSaveFileMountAccess($saveToFileMountIdentifier)) {
73  $uploadConfiguration[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
74  } else {
75  $persistenceIdentifier = $this->getRootForm()->getPersistenceIdentifier();
76  if (!empty($persistenceIdentifier)) {
77  $pathinfo = PathUtility::pathinfo($persistenceIdentifier);
78  $saveToFileMountIdentifier = $pathinfo['dirname'];
79  if ($this->checkSaveFileMountAccess($saveToFileMountIdentifier)) {
80  $uploadConfiguration[UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_FOLDER] = $saveToFileMountIdentifier;
81  }
82  }
83  }
84 
85  $propertyMappingConfiguration->setTypeConverterOptions(UploadedFileReferenceConverter::class, $uploadConfiguration);
86  }
87 
93  protected function checkSaveFileMountAccess(string $saveToFileMountIdentifier): bool
94  {
95  if (empty($saveToFileMountIdentifier)) {
96  return false;
97  }
98 
99  $resourceFactory = GeneralUtility::makeInstance(ObjectManager::class)
100  ->get(ResourceFactory::class);
101 
102  try {
103  $resourceFactory->getFolderObjectFromCombinedIdentifier($saveToFileMountIdentifier);
104  return true;
105  } catch (\InvalidArgumentException $e) {
106  return false;
107  }
108  }
109 }
checkSaveFileMountAccess(string $saveToFileMountIdentifier)
Definition: FileUpload.php:93
static pathinfo($path, $options=null)
static makeInstance($className,...$constructorArguments)