Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement canAdopt() in IFileProcessor and FileProcessor #6006

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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+}')]
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public function getAllowedFileExtensions(array $context): array
return $attachmentHandler->getAllowedExtensions();
}

#[\Override]
public function canAdopt(File $file, array $context): bool
{
return Attachment::findByFileID($file->fileID) === null;
}

#[\Override]
public function adopt(File $file, array $context): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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.upload.error.adopt',
['filename' => $file->filename]
)
);
}
}

parent::validate();
}

Expand Down
1 change: 1 addition & 0 deletions wcfsetup/install/lang/de.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5566,6 +5566,7 @@ Benachrichtigungen auf <a href="{link isHtmlEmail=true}{/link}">{PAGE_TITLE|phra
<item name="wcf.upload.error.fileExtensionNotPermitted"><![CDATA[Der Dateityp von „{$filename}“ ist unzulässig.]]></item>
<item name="wcf.upload.error.fileSizeTooLarge"><![CDATA[Die Datei ist zu groß.]]></item>
<item name="wcf.upload.error.maximumCountReached"><![CDATA[Es {plural value=$maximumCount 1='darf nur eine Datei' other='dürfen nur # Dateien'} hochgeladen werden.]]></item>
<item name="wcf.upload.error.adopt"><![CDATA[Die Datei „{$filename}“ kann nicht zugewiesen werden.]]></item>
</category>
</import>
<delete>
Expand Down
1 change: 1 addition & 0 deletions wcfsetup/install/lang/en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5568,6 +5568,7 @@ your notifications on <a href="{link isHtmlEmail=true}{/link}">{PAGE_TITLE|phras
<item name="wcf.upload.error.fileExtensionNotPermitted"><![CDATA[The file type of “{$filename}” is not permitted.]]></item>
<item name="wcf.upload.error.fileSizeTooLarge"><![CDATA[The file is too large.]]></item>
<item name="wcf.upload.error.maximumCountReached"><![CDATA[{plural value=$maximumCount 1='Only one file' other='Only up to # files'} may be uploaded.]]></item>
<item name="wcf.upload.error.adopt"><![CDATA[The file „{$filename}“ cannot be assigned.]]></item>
</category>
</import>
<delete>
Expand Down
Loading