Skip to content
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
@@ -0,0 +1,122 @@
<?php

namespace wcf\command\attachment;

use wcf\data\attachment\Attachment;
use wcf\data\attachment\AttachmentEditor;
use wcf\data\attachment\AttachmentList;
use wcf\data\file\thumbnail\FileThumbnailList;
use wcf\data\object\type\ObjectType;
use wcf\data\object\type\ObjectTypeCache;
use wcf\system\file\processor\FileProcessor;

/**
* Copies attachments from one object to another.
* Returns an array of old attachmentIDs as keys and new attachmentIDs as values.
*
* @author Olaf Braun
* @copyright 2001-2025 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.3
*/
final class CopyAttachments
{
public function __construct(
private readonly string $sourceObjectType,
private readonly int $sourceObjectID,
private readonly string $targetObjectType,
private readonly int $targetObjectID,
) {
}

/**
* @return array<int, int>
*/
public function __invoke(): array
{
$sourceObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName(
'com.woltlab.wcf.attachment.objectType',
$this->sourceObjectType
);
$targetObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName(
'com.woltlab.wcf.attachment.objectType',
$this->targetObjectType
);

$attachments = $this->getAttachments($sourceObjectType, $this->sourceObjectID);

$newAttachmentIDs = [];
foreach ($attachments as $attachment) {
$newAttachment = $this->copyAttachment($targetObjectType, $attachment);

$newAttachmentIDs[$attachment->attachmentID] = $newAttachment->attachmentID;
}

return $newAttachmentIDs;
}

/**
* @return Attachment[]
*/
private function getAttachments(ObjectType $sourceObjectType, int $sourceObjectID): array
{
$attachmentList = new AttachmentList();
$attachmentList->getConditionBuilder()->add("attachment.objectTypeID = ?", [$sourceObjectType->objectTypeID]);
$attachmentList->getConditionBuilder()->add("attachment.objectID = ?", [$sourceObjectID]);
$attachmentList->readObjects();

return $attachmentList->getObjects();
}

private function copyAttachment(ObjectType $targetObjectType, Attachment $oldAttachment): Attachment
{
$file = $oldAttachment->getFile();
$thumbnailID = null;
$tinyThumbnailID = null;

if ($file !== null) {
$file = FileProcessor::getInstance()->copy($file, 'com.woltlab.wcf.attachment');

if ($oldAttachment->thumbnailID !== null || $oldAttachment->tinyThumbnailID !== null) {
$thumbnailList = new FileThumbnailList();
$thumbnailList->getConditionBuilder()->add('fileID = ?', [$file->fileID]);
$thumbnailList->readObjects();

foreach ($thumbnailList->getObjects() as $thumbnail) {
match ($thumbnail->identifier) {
'' => $thumbnailID = $thumbnail->thumbnailID,
'tiny' => $tinyThumbnailID = $thumbnail->thumbnailID,
};
}
}
}

return AttachmentEditor::create([
'objectTypeID' => $targetObjectType->objectTypeID,
'objectID' => $this->targetObjectID,
'userID' => $oldAttachment->userID,
'filename' => $oldAttachment->filename,
'filesize' => $oldAttachment->filesize,
'fileType' => $oldAttachment->fileType,
'fileHash' => $oldAttachment->fileHash,
'isImage' => $oldAttachment->isImage,
'width' => $oldAttachment->width,
'height' => $oldAttachment->height,
'tinyThumbnailType' => $oldAttachment->tinyThumbnailType,
'tinyThumbnailSize' => $oldAttachment->tinyThumbnailSize,
'tinyThumbnailWidth' => $oldAttachment->tinyThumbnailWidth,
'tinyThumbnailHeight' => $oldAttachment->tinyThumbnailHeight,
'thumbnailType' => $oldAttachment->thumbnailType,
'thumbnailSize' => $oldAttachment->thumbnailSize,
'thumbnailWidth' => $oldAttachment->thumbnailWidth,
'thumbnailHeight' => $oldAttachment->thumbnailHeight,
'downloads' => $oldAttachment->downloads,
'lastDownloadTime' => $oldAttachment->lastDownloadTime,
'uploadTime' => $oldAttachment->uploadTime,
'showOrder' => $oldAttachment->showOrder,
'fileID' => $file?->fileID,
'thumbnailID' => $thumbnailID,
'tinyThumbnailID' => $tinyThumbnailID,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace wcf\data\attachment;

use wcf\command\attachment\CopyAttachments;
use wcf\data\AbstractDatabaseObjectAction;
use wcf\data\file\thumbnail\FileThumbnailList;
use wcf\data\object\type\ObjectTypeCache;
use wcf\system\exception\PermissionDeniedException;
use wcf\system\exception\UserInputException;
use wcf\system\file\processor\FileProcessor;
use wcf\system\WCF;

/**
Expand Down Expand Up @@ -75,82 +74,18 @@ public function generateThumbnails()
* Copies attachments from one object id to another.
*
* @return array{attachmentIDs: array<int, int>}
*
* @deprecated 6.3 use `CopyAttachments` instead.
*/
public function copy()
{
$sourceObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName(
'com.woltlab.wcf.attachment.objectType',
$this->parameters['sourceObjectType']
);
$targetObjectType = ObjectTypeCache::getInstance()->getObjectTypeByName(
'com.woltlab.wcf.attachment.objectType',
$this->parameters['targetObjectType']
);

$attachmentList = new AttachmentList();
$attachmentList->getConditionBuilder()->add("attachment.objectTypeID = ?", [$sourceObjectType->objectTypeID]);
$attachmentList->getConditionBuilder()->add("attachment.objectID = ?", [$this->parameters['sourceObjectID']]);
$attachmentList->readObjects();

$newAttachmentIDs = [];
foreach ($attachmentList as $attachment) {
$file = $attachment->getFile();
if ($file !== null) {
$file = FileProcessor::getInstance()->copy($file, 'com.woltlab.wcf.attachment');

$thumbnailID = $tinyThumbnailID = null;
if ($attachment->thumbnailID !== null || $attachment->tinyThumbnailID !== null) {
$thumbnailList = new FileThumbnailList();
$thumbnailList->getConditionBuilder()->add('fileID = ?', [$file->fileID]);
$thumbnailList->readObjects();

foreach ($thumbnailList as $thumbnail) {
switch ($thumbnail->identifier) {
case '':
$thumbnailID = $thumbnail->thumbnailID;
break;

case 'tiny':
$tinyThumbnailID = $thumbnail->thumbnailID;
break;
}
}
}
}

$newAttachment = AttachmentEditor::create([
'objectTypeID' => $targetObjectType->objectTypeID,
'objectID' => $this->parameters['targetObjectID'],
'userID' => $attachment->userID,
'filename' => $attachment->filename,
'filesize' => $attachment->filesize,
'fileType' => $attachment->fileType,
'fileHash' => $attachment->fileHash,
'isImage' => $attachment->isImage,
'width' => $attachment->width,
'height' => $attachment->height,
'tinyThumbnailType' => $attachment->tinyThumbnailType,
'tinyThumbnailSize' => $attachment->tinyThumbnailSize,
'tinyThumbnailWidth' => $attachment->tinyThumbnailWidth,
'tinyThumbnailHeight' => $attachment->tinyThumbnailHeight,
'thumbnailType' => $attachment->thumbnailType,
'thumbnailSize' => $attachment->thumbnailSize,
'thumbnailWidth' => $attachment->thumbnailWidth,
'thumbnailHeight' => $attachment->thumbnailHeight,
'downloads' => $attachment->downloads,
'lastDownloadTime' => $attachment->lastDownloadTime,
'uploadTime' => $attachment->uploadTime,
'showOrder' => $attachment->showOrder,
'fileID' => $file?->fileID,
'thumbnailID' => $thumbnailID ?? null,
'tinyThumbnailID' => $tinyThumbnailID ?? null,
]);

$newAttachmentIDs[$attachment->attachmentID] = $newAttachment->attachmentID;
}

return [
'attachmentIDs' => $newAttachmentIDs,
'attachmentIDs' => (new CopyAttachments(
$this->parameters['sourceObjectType'],
$this->parameters['sourceObjectID'],
$this->parameters['targetObjectType'],
$this->parameters['targetObjectID']
))()
];
}
}