Skip to content

Commit

Permalink
Implement canAdopt() in IFileProcessor and FileProcessor to che…
Browse files Browse the repository at this point in the history
…ck if the file can be assigned to the given `$context`
  • Loading branch information
Cyperghost committed Oct 1, 2024
1 parent 05bf465 commit e016d75
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 deletions.
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,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
{
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.form.field.fileProcessor.error.adopt',
['file' => $file]
)
);
}
}

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 @@ -4099,6 +4099,7 @@ Dateianhänge:
<item name="wcf.form.field.upload.error.maximumImageHeight"><![CDATA[Die Datei „{$file->getFilename()}“ darf maximal {#$maximumImageHeight} Pixel hoch sein.]]></item>
<item name="wcf.form.field.upload.error.minimum"><![CDATA[{if LANGUAGE_USE_INFORMAL_VARIANT}Du musst{else}Sie müssen{/if} mindestens {if $minimum > 1}{#$minimum} Dateien{else}eine Datei{/if} hochladen.]]></item>
<item name="wcf.form.field.upload.error.maximum"><![CDATA[{if LANGUAGE_USE_INFORMAL_VARIANT}Du darfst{else}Sie dürfen{/if} maximal {if $maximum > 1}{#$maximum} Dateien{else}eine Datei{/if} hochladen.]]></item>
<item name="wcf.form.field.fileProcessor.error.adopt"><![CDATA[Die Datei „{$file->filename}“ kann nicht zugewiesen werden.]]></item>
</category>
<category name="wcf.image">
<item name="wcf.image.coverPhoto"><![CDATA[Titelbild]]></item>
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 @@ -4045,6 +4045,7 @@ Attachments:
<item name="wcf.form.field.upload.error.maximumImageHeight"><![CDATA[The file “{$file->getFilename()}” may have a maximum height of {#$maximumImageHeight} pixels.]]></item>
<item name="wcf.form.field.upload.error.minimum"><![CDATA[You must upload at least {if $minimum > 1}{#$minimum} files{else}one file{/if}.]]></item>
<item name="wcf.form.field.upload.error.maximum"><![CDATA[You can upload a maximum of {if $maximum > 1}{#$maximum} files{else}one file{/if}.]]></item>
<item name="wcf.form.field.fileProcessor.error.adopt"><![CDATA[The file „{$file->filename}“ cannot be assigned.]]></item>
</category>
<category name="wcf.image">
<item name="wcf.image.coverPhoto"><![CDATA[Cover Photo]]></item>
Expand Down

0 comments on commit e016d75

Please sign in to comment.