From e016d750a683964786fd2b5cda639959270799da Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Tue, 1 Oct 2024 12:14:48 +0200 Subject: [PATCH 1/4] Implement `canAdopt()` in `IFileProcessor` and `FileProcessor` to check if the file can be assigned to the given `$context` --- .../core/files/upload/SaveChunk.class.php | 7 +++++-- .../processor/AttachmentFileProcessor.class.php | 12 ++++++++++++ .../system/file/processor/FileProcessor.class.php | 10 ++++++++++ .../system/file/processor/IFileProcessor.class.php | 12 +++++++++++- .../builder/field/FileProcessorFormField.class.php | 14 +++++++++++++- wcfsetup/install/lang/de.xml | 1 + wcfsetup/install/lang/en.xml | 1 + 7 files changed, 53 insertions(+), 4 deletions(-) diff --git a/wcfsetup/install/files/lib/system/endpoint/controller/core/files/upload/SaveChunk.class.php b/wcfsetup/install/files/lib/system/endpoint/controller/core/files/upload/SaveChunk.class.php index 0a03944b78..8983b774f0 100644 --- a/wcfsetup/install/files/lib/system/endpoint/controller/core/files/upload/SaveChunk.class.php +++ b/wcfsetup/install/files/lib/system/endpoint/controller/core/files/upload/SaveChunk.class.php @@ -12,6 +12,7 @@ use wcf\system\endpoint\IController; use wcf\system\endpoint\PostRequest; use wcf\system\exception\UserInputException; +use wcf\system\file\processor\FileProcessor; use wcf\system\io\File; #[PostRequest('/core/files/upload/{identifier}/chunk/{sequenceNo:\d+}')] @@ -118,9 +119,11 @@ public function __invoke(ServerRequestInterface $request, array $variables): Res throw new UserInputException('validation', $exception->getType()); } - } - $processor?->adopt($file, $context); + if (FileProcessor::getInstance()->canAdopt($processor, $file, $context)) { + $processor->adopt($file, $context); + } + } $generateThumbnails = false; if ($processor !== null && $file->isImage()) { diff --git a/wcfsetup/install/files/lib/system/file/processor/AttachmentFileProcessor.class.php b/wcfsetup/install/files/lib/system/file/processor/AttachmentFileProcessor.class.php index bfc054b3dc..c3ec1624a2 100644 --- a/wcfsetup/install/files/lib/system/file/processor/AttachmentFileProcessor.class.php +++ b/wcfsetup/install/files/lib/system/file/processor/AttachmentFileProcessor.class.php @@ -40,6 +40,18 @@ public function getAllowedFileExtensions(array $context): array return $attachmentHandler->getAllowedExtensions(); } + #[\Override] + public function canAdopt(File $file, array $context): bool + { + $attachment = Attachment::findByFileID($file->fileID); + + if ($attachment === null) { + return true; + } + + return false; + } + #[\Override] public function adopt(File $file, array $context): void { diff --git a/wcfsetup/install/files/lib/system/file/processor/FileProcessor.class.php b/wcfsetup/install/files/lib/system/file/processor/FileProcessor.class.php index e09a201fb8..8ecd6b751e 100644 --- a/wcfsetup/install/files/lib/system/file/processor/FileProcessor.class.php +++ b/wcfsetup/install/files/lib/system/file/processor/FileProcessor.class.php @@ -125,6 +125,16 @@ public function getHtmlElement(IFileProcessor $fileProcessor, array $context): s ); } + public function canAdopt(IFileProcessor $fileProcessor, File $file, array $context): bool + { + $objectType = $this->getObjectType($fileProcessor->getObjectTypeName()); + if ($objectType->objectTypeID !== $file->objectTypeID) { + return false; + } + + return $fileProcessor->canAdopt($file, $context); + } + public function generateWebpVariant(File $file): void { $canGenerateThumbnail = match ($file->mimeType) { diff --git a/wcfsetup/install/files/lib/system/file/processor/IFileProcessor.class.php b/wcfsetup/install/files/lib/system/file/processor/IFileProcessor.class.php index bb8f9b01d1..701d22ce1a 100644 --- a/wcfsetup/install/files/lib/system/file/processor/IFileProcessor.class.php +++ b/wcfsetup/install/files/lib/system/file/processor/IFileProcessor.class.php @@ -36,12 +36,22 @@ public function acceptUpload(string $filename, int $fileSize, array $context): F */ public function validateUpload(File $file): void; + /** + * Checks if the given `$file` can be assigned to the object referenced by the information + * contained in `$context`. + * + * The `$file` can be assigned if one of the following conditions is met: + * - The file has not yet been assigned to any object + * - The file is already assigned to the object referenced by `$context` + */ + public function canAdopt(File $file, array $context): bool; + /** * Notifies the file processor that the upload of a file has been completed * that belongs to this type. * * `$context` are the exact same values that have previously been passed to - * `acceptUpload()` before. + * `canAdopt()` before. */ public function adopt(File $file, array $context): void; diff --git a/wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php index 500353548f..2093900ac9 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php @@ -194,7 +194,7 @@ public function validate() $this->addValidationError( new FormFieldValidationError( 'maximumFiles', - 'wcf.form.field.fileProcessor.error.maximumFiles', + 'wcf.upload.error.maximumCountReached', [ 'maximumCount' => $fileProcessor->getMaximumCount($this->context), 'count' => \count($this->files), @@ -203,6 +203,18 @@ public function validate() ); } + foreach ($this->files as $file) { + if (!FileProcessor::getInstance()->canAdopt($fileProcessor, $file, $this->context)) { + $this->addValidationError( + new FormFieldValidationError( + 'adopt', + 'wcf.form.field.fileProcessor.error.adopt', + ['file' => $file] + ) + ); + } + } + parent::validate(); } diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index d478f069b3..dc89e19e1a 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -4099,6 +4099,7 @@ Dateianhänge: getFilename()}“ darf maximal {#$maximumImageHeight} Pixel hoch sein.]]> 1}{#$minimum} Dateien{else}eine Datei{/if} hochladen.]]> 1}{#$maximum} Dateien{else}eine Datei{/if} hochladen.]]> + filename}“ kann nicht zugewiesen werden.]]> diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index ca0f31be04..da60a803d6 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -4045,6 +4045,7 @@ Attachments: getFilename()}” may have a maximum height of {#$maximumImageHeight} pixels.]]> 1}{#$minimum} files{else}one file{/if}.]]> 1}{#$maximum} files{else}one file{/if}.]]> + filename}“ cannot be assigned.]]> From 05221d784d63ff9e71bc9f472f7e5d0b9290894f Mon Sep 17 00:00:00 2001 From: Olaf Braun Date: Tue, 1 Oct 2024 14:57:22 +0200 Subject: [PATCH 2/4] Update wcfsetup/install/files/lib/system/file/processor/AttachmentFileProcessor.class.php Co-authored-by: Alexander Ebert --- .../file/processor/AttachmentFileProcessor.class.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/wcfsetup/install/files/lib/system/file/processor/AttachmentFileProcessor.class.php b/wcfsetup/install/files/lib/system/file/processor/AttachmentFileProcessor.class.php index c3ec1624a2..40e7f8587f 100644 --- a/wcfsetup/install/files/lib/system/file/processor/AttachmentFileProcessor.class.php +++ b/wcfsetup/install/files/lib/system/file/processor/AttachmentFileProcessor.class.php @@ -43,13 +43,7 @@ public function getAllowedFileExtensions(array $context): array #[\Override] public function canAdopt(File $file, array $context): bool { - $attachment = Attachment::findByFileID($file->fileID); - - if ($attachment === null) { - return true; - } - - return false; + return Attachment::findByFileID($file->fileID) === null; } #[\Override] From 15e1dc707908f01e157e52a408ec0beb713ba2a3 Mon Sep 17 00:00:00 2001 From: Olaf Braun Date: Tue, 1 Oct 2024 15:03:45 +0200 Subject: [PATCH 3/4] Rename language variable to `wcf.upload.error.adopt` --- .../system/form/builder/field/FileProcessorFormField.class.php | 2 +- wcfsetup/install/lang/de.xml | 2 +- wcfsetup/install/lang/en.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php index 2093900ac9..c1a229ad56 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php @@ -208,7 +208,7 @@ public function validate() $this->addValidationError( new FormFieldValidationError( 'adopt', - 'wcf.form.field.fileProcessor.error.adopt', + 'wcf.upload.error.adopt', ['file' => $file] ) ); diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index dc89e19e1a..461b3fff93 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -4099,7 +4099,6 @@ Dateianhänge: getFilename()}“ darf maximal {#$maximumImageHeight} Pixel hoch sein.]]> 1}{#$minimum} Dateien{else}eine Datei{/if} hochladen.]]> 1}{#$maximum} Dateien{else}eine Datei{/if} hochladen.]]> - filename}“ kann nicht zugewiesen werden.]]> @@ -5567,6 +5566,7 @@ Benachrichtigungen auf {PAGE_TITLE|phra + filename}“ kann nicht zugewiesen werden.]]> diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index da60a803d6..c6877efa42 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -4045,7 +4045,6 @@ Attachments: getFilename()}” may have a maximum height of {#$maximumImageHeight} pixels.]]> 1}{#$minimum} files{else}one file{/if}.]]> 1}{#$maximum} files{else}one file{/if}.]]> - filename}“ cannot be assigned.]]> @@ -5569,6 +5568,7 @@ your notifications on {PAGE_TITLE|phras + filename}“ cannot be assigned.]]> From a2d2b9a4757ce5190cb919ba613258fa83773af6 Mon Sep 17 00:00:00 2001 From: Olaf Braun Date: Tue, 1 Oct 2024 15:06:32 +0200 Subject: [PATCH 4/4] Use `$filename` instead of `$file->filename` --- .../system/form/builder/field/FileProcessorFormField.class.php | 2 +- wcfsetup/install/lang/de.xml | 2 +- wcfsetup/install/lang/en.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php index c1a229ad56..0e2808100b 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php @@ -209,7 +209,7 @@ public function validate() new FormFieldValidationError( 'adopt', 'wcf.upload.error.adopt', - ['file' => $file] + ['filename' => $file->filename] ) ); } diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 461b3fff93..73de4d180d 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -5566,7 +5566,7 @@ Benachrichtigungen auf {PAGE_TITLE|phra - filename}“ kann nicht zugewiesen werden.]]> + diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index c6877efa42..ad4f90c2da 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -5568,7 +5568,7 @@ your notifications on {PAGE_TITLE|phras - filename}“ cannot be assigned.]]> +