Skip to content

Commit

Permalink
[TASK] Rename profile images using profile data
Browse files Browse the repository at this point in the history
  • Loading branch information
codemonkey1988 committed Sep 19, 2023
1 parent 121b0d2 commit 08cf8d1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
24 changes: 24 additions & 0 deletions Classes/Controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
]
);
}
Expand Down Expand Up @@ -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
);
}
}
34 changes: 30 additions & 4 deletions Classes/Property/TypeConverter/ProfileImageUploadConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -68,6 +69,7 @@ public function convertFrom(
$targetFolderIdentifier = null;
$maxFileSize = '0k';
$allowedMimeTypes = '';
$targetFileNameWithoutExtension = null;
if ($configuration !== null) {
$targetFolderIdentifier = $configuration->getConfigurationValue(
self::class,
Expand All @@ -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) {
Expand All @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit 08cf8d1

Please sign in to comment.