From e794d53afe7115404acbf46b76dc81c10f8a5a4e Mon Sep 17 00:00:00 2001 From: Krystian Szymukowicz Date: Sat, 19 Oct 2024 16:31:11 +0200 Subject: [PATCH] [BUGFIX] Remove usage of getStorageObjectFromCombinedIdentifier on file upload. Fix upload test. Force exit on first fail of postman tests instead of running other postman test files. --- Build/postman/run.sh | 5 ++++- Classes/Service/FileUploadService.php | 24 +++++++++++++++++++----- Tests/Postman/t3apinews.crud.json | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Build/postman/run.sh b/Build/postman/run.sh index cc9ff13..3d0321d 100644 --- a/Build/postman/run.sh +++ b/Build/postman/run.sh @@ -2,6 +2,9 @@ source .ddev/test/utils.sh +set +x +set -e + POSTMAN_BUILD_PATH=/var/www/html/Build/postman/ cd ${POSTMAN_BUILD_PATH} || exit @@ -46,7 +49,7 @@ else ./node_modules/.bin/newman run "../../Tests/Postman/$TEST_FILE" --verbose --bail --env-var "baseUrl=$DOMAIN" else for TEST_FILE in ../../Tests/Postman/*.json; do - ./node_modules/.bin/newman run "$TEST_FILE" --verbose --bail --env-var "baseUrl=$DOMAIN" + ./node_modules/.bin/newman run "$TEST_FILE" --verbose --bail --env-var "baseUrl=$DOMAIN" done fi done diff --git a/Classes/Service/FileUploadService.php b/Classes/Service/FileUploadService.php index 1a68001..b087878 100644 --- a/Classes/Service/FileUploadService.php +++ b/Classes/Service/FileUploadService.php @@ -12,19 +12,23 @@ use TYPO3\CMS\Core\Resource\Exception\ExistingTargetFolderException; use TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException; use TYPO3\CMS\Core\Resource\Exception\InsufficientFolderWritePermissionsException; -use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException; use TYPO3\CMS\Core\Resource\File; use TYPO3\CMS\Core\Resource\Folder; use TYPO3\CMS\Core\Resource\ResourceFactory; use TYPO3\CMS\Core\Resource\ResourceStorage; use TYPO3\CMS\Core\Resource\Security\FileNameValidator; +use TYPO3\CMS\Core\Resource\StorageRepository; use TYPO3\CMS\Core\SingletonInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Core\Utility\MathUtility; use TYPO3\CMS\Core\Utility\PathUtility; class FileUploadService implements SingletonInterface { - public function __construct(protected readonly ResourceFactory $resourceFactory) {} + public function __construct( + protected readonly ResourceFactory $resourceFactory, + protected readonly StorageRepository $storageRepository + ) {} /** * @throws Exception @@ -33,6 +37,7 @@ public function process(OperationInterface $operation, Request $request): File { /** @var UploadedFile $uploadedFile */ $uploadedFile = $request->files->get('originalResource'); + $uploadSettings = $operation->getUploadSettings(); $this->verifyFileExtension($uploadSettings, $uploadedFile); @@ -95,11 +100,20 @@ protected function getUploadFolder(UploadSettings $uploadSettings): Folder $uploadFolder = $this->resourceFactory->getFolderObjectFromCombinedIdentifier( $uploadSettings->getFolder() ); - } catch (ResourceDoesNotExistException $resourceDoesNotExistException) { - $resource = $this->resourceFactory->getStorageObjectFromCombinedIdentifier( - $uploadSettings->getFolder() + } catch (\Throwable $e) { + + [$storageId, $objectIdentifier] = array_pad( + GeneralUtility::trimExplode(':', $uploadSettings->getFolder()), + 2, + null ); + if ($objectIdentifier === null && !MathUtility::canBeInterpretedAsInteger($storageId)) { + $resource = $this->storageRepository->findByUid(0); + } else { + $resource = $this->storageRepository->findByUid((int)$storageId); + } + if (!$resource instanceof ResourceStorage) { throw new \InvalidArgumentException( sprintf('Invalid upload path (`%s`). Storage does not exist?', $uploadSettings->getFolder()), diff --git a/Tests/Postman/t3apinews.crud.json b/Tests/Postman/t3apinews.crud.json index 6904a35..ba3d81b 100644 --- a/Tests/Postman/t3apinews.crud.json +++ b/Tests/Postman/t3apinews.crud.json @@ -548,7 +548,7 @@ { "key": "Content-Type", "name": "Content-Type", - "value": "application/x-www-form-urlencoded", + "value": "multipart/form-data", "type": "text" } ],