From: Alexander Ebert Date: Fri, 3 May 2024 15:23:48 +0000 (+0200) Subject: Implement a cleanup on file delete X-Git-Tag: 6.1.0_Alpha_1~85^2^2~33 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=b592df138f4f9f89aa7e286ff2fb7cb8ba440937;p=GitHub%2FWoltLab%2FWCF.git Implement a cleanup on file delete --- diff --git a/wcfsetup/install/files/lib/data/attachment/AttachmentEditor.class.php b/wcfsetup/install/files/lib/data/attachment/AttachmentEditor.class.php index 311dc7d080..51915bdd7c 100644 --- a/wcfsetup/install/files/lib/data/attachment/AttachmentEditor.class.php +++ b/wcfsetup/install/files/lib/data/attachment/AttachmentEditor.class.php @@ -64,6 +64,10 @@ class AttachmentEditor extends DatabaseObjectEditor */ public function deleteFiles() { + if ($this->fileID !== null) { + return; + } + @\unlink($this->getLocation()); if ($this->tinyThumbnailType) { @\unlink($this->getTinyThumbnailLocation()); diff --git a/wcfsetup/install/files/lib/data/file/FileAction.class.php b/wcfsetup/install/files/lib/data/file/FileAction.class.php index 05a54e3212..001bcb064a 100644 --- a/wcfsetup/install/files/lib/data/file/FileAction.class.php +++ b/wcfsetup/install/files/lib/data/file/FileAction.class.php @@ -3,6 +3,7 @@ namespace wcf\data\file; use wcf\data\AbstractDatabaseObjectAction; +use wcf\system\file\processor\FileProcessor; /** * @author Alexander Ebert @@ -16,4 +17,18 @@ use wcf\data\AbstractDatabaseObjectAction; class FileAction extends AbstractDatabaseObjectAction { protected $className = FileEditor::class; + + #[\Override] + public function delete() + { + if ($this->objects === []) { + $this->readObjects(); + } + + if ($this->objects !== []) { + FileProcessor::getInstance()->delete($this->objects); + } + + return parent::delete(); + } } 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 7c90cbee12..c4bd099d2e 100644 --- a/wcfsetup/install/files/lib/system/file/processor/AttachmentFileProcessor.class.php +++ b/wcfsetup/install/files/lib/system/file/processor/AttachmentFileProcessor.class.php @@ -4,6 +4,7 @@ namespace wcf\system\file\processor; use CuyZ\Valinor\Mapper\MappingError; use wcf\data\attachment\Attachment; +use wcf\data\attachment\AttachmentAction; use wcf\data\attachment\AttachmentEditor; use wcf\data\file\File; use wcf\data\file\thumbnail\FileThumbnail; @@ -186,6 +187,12 @@ final class AttachmentFileProcessor implements IFileProcessor ]); } + #[\Override] + public function delete(array $fileIDs, array $thumbnailIDs): void + { + (new AttachmentAction($fileIDs, 'delete'))->executeAction(); + } + private function getAttachmentHandlerFromContext(array $context): ?AttachmentHandler { try { 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 88141f1f43..bc5808fa10 100644 --- a/wcfsetup/install/files/lib/system/file/processor/FileProcessor.class.php +++ b/wcfsetup/install/files/lib/system/file/processor/FileProcessor.class.php @@ -6,11 +6,13 @@ use wcf\data\file\File; use wcf\data\file\thumbnail\FileThumbnail; use wcf\data\file\thumbnail\FileThumbnailEditor; use wcf\data\file\thumbnail\FileThumbnailList; +use wcf\system\database\util\PreparedStatementConditionBuilder; use wcf\system\event\EventHandler; use wcf\system\file\processor\event\FileProcessorCollecting; use wcf\system\image\adapter\ImageAdapter; use wcf\system\image\ImageHandler; use wcf\system\SingletonFactory; +use wcf\system\WCF; use wcf\util\FileUtil; use wcf\util\JSON; use wcf\util\StringUtil; @@ -125,4 +127,23 @@ final class FileProcessor extends SingletonFactory $processor->adoptThumbnail($fileThumbnail); } } + + public function delete(array $files): void + { + $fileIDs = \array_column($files, 'fileID'); + + $conditions = new PreparedStatementConditionBuilder(); + $conditions->add('fileID IN (?)', [$fileIDs]); + + $sql = "SELECT thumbnailID + FROM wcf1_file_thumbnail + {$conditions}"; + $statement = WCF::getDB()->prepare($sql); + $statement->execute($conditions->getParameters()); + $thumbnailIDs = $statement->fetchAll(\PDO::FETCH_COLUMN); + + foreach ($this->processors as $processor) { + $processor->delete($fileIDs, $thumbnailIDs); + } + } } 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 56d49368ab..4ea0128651 100644 --- a/wcfsetup/install/files/lib/system/file/processor/IFileProcessor.class.php +++ b/wcfsetup/install/files/lib/system/file/processor/IFileProcessor.class.php @@ -23,6 +23,12 @@ interface IFileProcessor public function canDownload(File $file): bool; + /** + * @param list $files + * @param list $thumbnails + */ + public function delete(array $fileIDs, array $thumbnailIDs): void; + public function getAllowedFileExtensions(array $context): array; public function getResizeConfiguration(): ResizeConfiguration; diff --git a/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeImg.class.php b/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeImg.class.php index 47dd8cd1b8..fc26fb1bbc 100644 --- a/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeImg.class.php +++ b/wcfsetup/install/files/lib/system/html/input/node/HtmlInputNodeImg.class.php @@ -108,8 +108,8 @@ class HtmlInputNodeImg extends AbstractHtmlInputNode $float = 'none'; $thumbnail = false; - - if (\strpos($element->getAttribute('src'), 'thumbnail=1') !== false) { + $src = $element->getAttribute('src'); + if (\str_contains($src, 'thumbnail=1') || \str_ends_with($src, '.webp')) { $thumbnail = true; }