diff --git a/Classes/Controller/ProfileController.php b/Classes/Controller/ProfileController.php index c6f0179..dab46b6 100644 --- a/Classes/Controller/ProfileController.php +++ b/Classes/Controller/ProfileController.php @@ -176,6 +176,13 @@ public function initializeSaveProfileAction(): void $allowedImeTypes = $this->settings['editForm']['profileImage']['validation']['allowedMimeTypes'] ?? ''; $profileImageTypeConverter = GeneralUtility::makeInstance(ProfileImageUploadConverter::class); + $profileUid = 0; + $body = $this->request->getParsedBody(); + if (is_array($body)) { + $profileUid = (int)($body['tx_academicpersonsedit_profileediting']['profile']['__identity'] ?? 0); + } + $targetFileName = $this->buildProfileImageNameWithoutExtension($profileUid); + $this->arguments ->getArgument('profile') ->getPropertyMappingConfiguration() @@ -187,6 +194,7 @@ public function initializeSaveProfileAction(): void ProfileImageUploadConverter::CONFIGURATION_TARGET_DIRECTORY_COMBINED_IDENTIFIER => $targetFolderIdentifier, ProfileImageUploadConverter::CONFIGURATION_MAX_UPLOAD_SIZE => $maxFilesize, ProfileImageUploadConverter::CONFIGURATION_ALLOWED_MIME_TYPES => $allowedImeTypes, + ProfileImageUploadConverter::CONFIGURATION_TARGET_FILE_NAME_WITHOUT_EXTENSION => $targetFileName, ] ); } @@ -390,4 +398,20 @@ private function checkProfileEditAccess(int $profileUid): void throw new AccessDeniedException('User not allowed to edit this profile.', 1695046903); } } + + private function buildProfileImageNameWithoutExtension(int $profileUid): string + { + /** @var Profile|null $profile */ + $profile = $this->profileRepository->findByUid($profileUid); + if ($profile === null) { + return ''; + } + + return sprintf( + '%s-%s-%d', + $profile->getFirstName(), + $profile->getLastName(), + $profileUid + ); + } } diff --git a/Classes/Property/TypeConverter/ProfileImageUploadConverter.php b/Classes/Property/TypeConverter/ProfileImageUploadConverter.php index 481da58..6222b50 100644 --- a/Classes/Property/TypeConverter/ProfileImageUploadConverter.php +++ b/Classes/Property/TypeConverter/ProfileImageUploadConverter.php @@ -36,6 +36,7 @@ final class ProfileImageUploadConverter extends AbstractTypeConverter implements public const CONFIGURATION_TARGET_DIRECTORY_COMBINED_IDENTIFIER = 'targetFolderCombinedIdentifier'; public const CONFIGURATION_MAX_UPLOAD_SIZE = 'maxUploadSize'; public const CONFIGURATION_ALLOWED_MIME_TYPES = 'allowedMimeTypes'; + public const CONFIGURATION_TARGET_FILE_NAME_WITHOUT_EXTENSION = 'targetFileNameWithoutExtension'; protected $sourceTypes = ['array']; @@ -68,6 +69,7 @@ public function convertFrom( $targetFolderIdentifier = null; $maxFileSize = '0k'; $allowedMimeTypes = ''; + $targetFileNameWithoutExtension = null; if ($configuration !== null) { $targetFolderIdentifier = $configuration->getConfigurationValue( self::class, @@ -81,6 +83,10 @@ public function convertFrom( self::class, self::CONFIGURATION_ALLOWED_MIME_TYPES ); + $targetFileNameWithoutExtension = $configuration->getConfigurationValue( + self::class, + self::CONFIGURATION_TARGET_FILE_NAME_WITHOUT_EXTENSION + ); } if (!isset($uploadedFileInformation['error']) || $uploadedFileInformation['error'] === \UPLOAD_ERR_NO_FILE) { @@ -95,13 +101,24 @@ public function convertFrom( ); } + if (empty($targetFileNameWithoutExtension) || !is_string($targetFileNameWithoutExtension)) { + return GeneralUtility::makeInstance( + Error::class, + 'Target filename could not be generated.', + 1695106476 + ); + } + if (!isset($uploadedFileInformation['tmp_name']) || !isset($uploadedFileInformation['name'])) { return null; } + $fileExtension = pathinfo($uploadedFileInformation['name'], PATHINFO_EXTENSION); + $targetFileName = strtolower(sprintf('%s.%s', $targetFileNameWithoutExtension, $fileExtension)); + try { $this->validateUploadedFile($uploadedFileInformation, $maxFileSize, $allowedMimeTypes); - return $this->importUploadedResource($uploadedFileInformation, $targetFolderIdentifier); + return $this->importUploadedResource($uploadedFileInformation, $targetFolderIdentifier, $targetFileName); } catch (TypeConverterException $e) { return GeneralUtility::makeInstance( Error::class, @@ -115,14 +132,23 @@ public function convertFrom( * @param array{name: string, tmp_name: string, __identity?: string} $uploadedFileInformation * @throws TypeConverterException */ - private function importUploadedResource(array $uploadedFileInformation, string $targetFolderIdentifier): ExtbaseFileReference - { + private function importUploadedResource( + array $uploadedFileInformation, + string $targetFolderIdentifier, + string $targetFileName + ): ExtbaseFileReference { if (!GeneralUtility::makeInstance(FileNameValidator::class)->isValid($uploadedFileInformation['name'])) { throw new TypeConverterException('Uploading files with PHP file extensions is not allowed!', 1690525745); } $targetFolder = $this->getOrCreateTargetFolder($targetFolderIdentifier); - $uploadedFile = $targetFolder->addUploadedFile($uploadedFileInformation, DuplicationBehavior::REPLACE); + /** @var File $uploadedFile */ + $uploadedFile = $targetFolder->getStorage()->addUploadedFile( + $uploadedFileInformation, + $targetFolder, + $targetFileName, + DuplicationBehavior::REPLACE + ); $resourcePointer = isset($uploadedFileInformation['__identity']) ? (int)$uploadedFileInformation['__identity'] : null; return $this->createFileReferenceFromFalFileObject($uploadedFile, $resourcePointer);