diff --git a/Classes/Controller/JoblistController.php b/Classes/Controller/JoblistController.php index 909a737439f6a569fad7977ce982a8991875bc73..4571f744f5201f29b671ee5508ca6cbf15adc275 100644 --- a/Classes/Controller/JoblistController.php +++ b/Classes/Controller/JoblistController.php @@ -46,6 +46,7 @@ use TYPO3\CMS\Core\Core\Environment; use TYPO3\CMS\Core\Error\Http\PageNotFoundException; use TYPO3\CMS\Core\Exception\SiteNotFoundException; use TYPO3\CMS\Core\Http\ImmediateResponseException; +use TYPO3\CMS\Core\Resource\Exception\AbstractFileOperationException; use TYPO3\CMS\Core\Resource\Exception\ExistingTargetFileNameException; use TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException; use TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException; @@ -704,12 +705,13 @@ class JoblistController extends ActionController { * * @param string $folderName * @param JobApplication $applicationData - * @throws TypeConverterException * @throws ExistingTargetFileNameException * @throws ExistingTargetFolderException * @throws InsufficientFolderAccessPermissionsException * @throws InsufficientFolderReadPermissionsException * @throws InsufficientFolderWritePermissionsException + * @throws TypeConverterException + * @throws AbstractFileOperationException */ protected function moveTmpFolder(string $folderName, JobApplication $applicationData): void { $allowedFileExtensions = $this->getAllowedFileExtensions(); @@ -726,6 +728,7 @@ class JoblistController extends ActionController { } // Move uploaded files & csv fo real folder and delete the tmp folder + $uploadedFiles = []; foreach (self::UPLOADED_FILES as $singleFilePostKey) { if (!array_key_exists($singleFilePostKey, $_POST)) { throw new TypeConverterException( @@ -739,7 +742,6 @@ class JoblistController extends ActionController { // get the first uploaded document, should be prevented in the frontend to upload more than one $singleUploadedFile = current($_POST[$singleFilePostKey])['path']; - $filePathInfo = PathUtility::pathinfo($singleUploadedFile); if (!GeneralUtility::inList($allowedFileExtensions, strtolower($filePathInfo['extension']))) { throw new TypeConverterException( @@ -749,19 +751,25 @@ class JoblistController extends ActionController { } $basename = $filePathInfo['basename']; - if (!$newFolder->hasFile($basename)) { - /** @noinspection PhpUnreachableStatementInspection */ - $singleFileToMove = $storage->getFileInFolder($basename, $tempFolder); + $newFilename = strtolower($singleFilePostKey) . '.' . strtolower($filePathInfo['extension']); + if (array_key_exists($singleUploadedFile, $uploadedFiles)) { + // the same file was uploaded for different sources + $usableFile = $newFolder->getFile( + $uploadedFiles[$singleUploadedFile] . '.' . strtolower($filePathInfo['extension']) + ); + if (!$usableFile) { + throw new \RuntimeException('File not found (' . $singleFilePostKey . ')!'); + } + $usableFile = $storage->copyFile($usableFile, $newFolder, $newFilename); + } elseif (!$newFolder->hasFile($newFilename)) { // when we reload etc. this image might already be moved. - $usableFile = $storage->moveFile( - $singleFileToMove, - $newFolder, - $singleFilePostKey . '.' . $filePathInfo['extension'] - ); + /** @noinspection PhpUnreachableStatementInspection */ + $singleFileToMove = $storage->getFileInFolder($basename, $tempFolder); + $usableFile = $storage->moveFile($singleFileToMove, $newFolder, $newFilename); } else { /** @noinspection PhpUnreachableStatementInspection */ - $usableFile = $newFolder->getFile($basename); + $usableFile = $newFolder->getFile($newFilename); } $fileReference = $this->fileAndFolderService->createFileReferenceFromFalFileObject($usableFile); @@ -776,6 +784,8 @@ class JoblistController extends ActionController { if ($singleFilePostKey === 'certificate') { $applicationData->setCertificate($fileReference); } + + $uploadedFiles[$singleUploadedFile] = strtolower($singleFilePostKey); } } @@ -798,7 +808,7 @@ class JoblistController extends ActionController { $resourceFactory = $this->objectManager->get(ResourceFactory::class); $storage = $resourceFactory->getStorageObject(1); try { - $tempFolder = $storage->getFolder('/JobApplication/temp/' . $folderName); + $tempFolder = $storage->getFolder('/JobApplication/' . $folderName); $storage->deleteFolder($tempFolder, TRUE); } catch (\Exception $exception) { // folder is already deleted for some reason