|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace wcf\command\attachment; |
| 4 | + |
| 5 | +use wcf\data\attachment\Attachment; |
| 6 | +use wcf\data\attachment\AttachmentEditor; |
| 7 | +use wcf\data\attachment\AttachmentList; |
| 8 | +use wcf\data\file\thumbnail\FileThumbnailList; |
| 9 | +use wcf\data\object\type\ObjectType; |
| 10 | +use wcf\data\object\type\ObjectTypeCache; |
| 11 | +use wcf\system\file\processor\FileProcessor; |
| 12 | + |
| 13 | +/** |
| 14 | + * Copies attachments from one object to another. |
| 15 | + * Returns an array of old attachmentIDs as keys and new attachmentIDs as values. |
| 16 | + * |
| 17 | + * @author Olaf Braun |
| 18 | + * @copyright 2001-2025 WoltLab GmbH |
| 19 | + * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 20 | + * @since 6.3 |
| 21 | + */ |
| 22 | +final class CopyAttachments |
| 23 | +{ |
| 24 | + public function __construct( |
| 25 | + private readonly string $sourceObjectType, |
| 26 | + private readonly int $sourceObjectID, |
| 27 | + private readonly string $targetObjectType, |
| 28 | + private readonly int $targetObjectID, |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @return array<int, int> |
| 34 | + */ |
| 35 | + public function __invoke(): array |
| 36 | + { |
| 37 | + $sourceObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName( |
| 38 | + 'com.woltlab.wcf.attachment.objectType', |
| 39 | + $this->sourceObjectType |
| 40 | + ); |
| 41 | + $targetObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName( |
| 42 | + 'com.woltlab.wcf.attachment.objectType', |
| 43 | + $this->targetObjectType |
| 44 | + ); |
| 45 | + |
| 46 | + $attachments = $this->getAttachments($sourceObjectType, $this->sourceObjectID); |
| 47 | + |
| 48 | + $newAttachmentIDs = []; |
| 49 | + foreach ($attachments as $attachment) { |
| 50 | + $newAttachment = $this->copyAttachment($targetObjectType, $attachment); |
| 51 | + |
| 52 | + $newAttachmentIDs[$attachment->attachmentID] = $newAttachment->attachmentID; |
| 53 | + } |
| 54 | + |
| 55 | + return $newAttachmentIDs; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return Attachment[] |
| 60 | + */ |
| 61 | + private function getAttachments(ObjectType $sourceObjectType, int $sourceObjectID): array |
| 62 | + { |
| 63 | + $attachmentList = new AttachmentList(); |
| 64 | + $attachmentList->getConditionBuilder()->add("attachment.objectTypeID = ?", [$sourceObjectType->objectTypeID]); |
| 65 | + $attachmentList->getConditionBuilder()->add("attachment.objectID = ?", [$sourceObjectID]); |
| 66 | + $attachmentList->readObjects(); |
| 67 | + |
| 68 | + return $attachmentList->getObjects(); |
| 69 | + } |
| 70 | + |
| 71 | + private function copyAttachment(ObjectType $targetObjectType, Attachment $oldAttachment): Attachment |
| 72 | + { |
| 73 | + $file = $oldAttachment->getFile(); |
| 74 | + $thumbnailID = null; |
| 75 | + $tinyThumbnailID = null; |
| 76 | + |
| 77 | + if ($file !== null) { |
| 78 | + $file = FileProcessor::getInstance()->copy($file, 'com.woltlab.wcf.attachment'); |
| 79 | + |
| 80 | + if ($oldAttachment->thumbnailID !== null || $oldAttachment->tinyThumbnailID !== null) { |
| 81 | + $thumbnailList = new FileThumbnailList(); |
| 82 | + $thumbnailList->getConditionBuilder()->add('fileID = ?', [$file->fileID]); |
| 83 | + $thumbnailList->readObjects(); |
| 84 | + |
| 85 | + foreach ($thumbnailList->getObjects() as $thumbnail) { |
| 86 | + match ($thumbnail->identifier) { |
| 87 | + '' => $thumbnailID = $thumbnail->thumbnailID, |
| 88 | + 'tiny' => $tinyThumbnailID = $thumbnail->thumbnailID, |
| 89 | + }; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return AttachmentEditor::create([ |
| 95 | + 'objectTypeID' => $targetObjectType->objectTypeID, |
| 96 | + 'objectID' => $this->targetObjectID, |
| 97 | + 'userID' => $oldAttachment->userID, |
| 98 | + 'filename' => $oldAttachment->filename, |
| 99 | + 'filesize' => $oldAttachment->filesize, |
| 100 | + 'fileType' => $oldAttachment->fileType, |
| 101 | + 'fileHash' => $oldAttachment->fileHash, |
| 102 | + 'isImage' => $oldAttachment->isImage, |
| 103 | + 'width' => $oldAttachment->width, |
| 104 | + 'height' => $oldAttachment->height, |
| 105 | + 'tinyThumbnailType' => $oldAttachment->tinyThumbnailType, |
| 106 | + 'tinyThumbnailSize' => $oldAttachment->tinyThumbnailSize, |
| 107 | + 'tinyThumbnailWidth' => $oldAttachment->tinyThumbnailWidth, |
| 108 | + 'tinyThumbnailHeight' => $oldAttachment->tinyThumbnailHeight, |
| 109 | + 'thumbnailType' => $oldAttachment->thumbnailType, |
| 110 | + 'thumbnailSize' => $oldAttachment->thumbnailSize, |
| 111 | + 'thumbnailWidth' => $oldAttachment->thumbnailWidth, |
| 112 | + 'thumbnailHeight' => $oldAttachment->thumbnailHeight, |
| 113 | + 'downloads' => $oldAttachment->downloads, |
| 114 | + 'lastDownloadTime' => $oldAttachment->lastDownloadTime, |
| 115 | + 'uploadTime' => $oldAttachment->uploadTime, |
| 116 | + 'showOrder' => $oldAttachment->showOrder, |
| 117 | + 'fileID' => $file?->fileID, |
| 118 | + 'thumbnailID' => $thumbnailID, |
| 119 | + 'tinyThumbnailID' => $tinyThumbnailID, |
| 120 | + ]); |
| 121 | + } |
| 122 | +} |
0 commit comments