namespace wcf\data\attachment;
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;
/**
$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'],
'lastDownloadTime' => $attachment->lastDownloadTime,
'uploadTime' => $attachment->uploadTime,
'showOrder' => $attachment->showOrder,
+ 'fileID' => $file?->fileID,
+ 'thumbnailID' => $thumbnailID,
+ 'tinyThumbnailID' => $tinyThumbnailID,
]);
- // copy attachment
- @\copy($attachment->getLocation(), $newAttachment->getLocation());
-
- if ($attachment->tinyThumbnailSize) {
- @\copy($attachment->getTinyThumbnailLocation(), $newAttachment->getTinyThumbnailLocation());
- }
- if ($attachment->thumbnailSize) {
- @\copy($attachment->getThumbnailLocation(), $newAttachment->getThumbnailLocation());
- }
-
$newAttachmentIDs[$attachment->attachmentID] = $newAttachment->attachmentID;
}
return $maximumFileSize;
}
+
+ public function copy(File $oldFile, string $objectType): File
+ {
+ $objectTypeObj = $this->getObjectType($objectType);
+ if ($objectTypeObj === null) {
+ throw new \InvalidArgumentException("The object type '{$objectType}' is invalid.");
+ }
+
+ $newFile = FileEditor::create([
+ 'filename' => $oldFile->filename,
+ 'fileSize' => $oldFile->fileSize,
+ 'fileHash' => $oldFile->fileHash,
+ 'fileExtension' => $oldFile->fileExtension,
+ 'secret' => \hex2bin(\random_bytes(10)),
+ 'objectTypeID' => $objectTypeObj->objectTypeID,
+ 'mimeType' => $oldFile->mimeType,
+ 'width' => $oldFile->width,
+ 'height' => $oldFile->height,
+ 'fileHashWebp' => $oldFile->fileHashWebp,
+ ]);
+
+ \copy($oldFile->getPathname(), $newFile->getPathname());
+
+ if ($oldFile->fileHashWebp !== null) {
+ \copy($oldFile->getPathnameWebp(), $newFile->getPathnameWebp());
+ }
+
+ $this->copyThumbnails($oldFile->fileID, $newFile->fileID);
+
+ return $newFile;
+ }
+
+ private function copyThumbnails(int $oldFileID, int $newFileID): void
+ {
+ $thumbnailList = new FileThumbnailList();
+ $thumbnailList->getConditionBuilder()->add("fileID = ?", [$oldFileID]);
+ $thumbnailList->readObjects();
+
+ foreach ($thumbnailList as $oldThumbnail) {
+ $newThumbnail = FileThumbnailEditor::create([
+ 'fileID' => $newFileID,
+ 'identifier' => $oldThumbnail->identifier,
+ 'fileHash' => $oldThumbnail->fileHash,
+ 'fileExtension' => $oldThumbnail->fileExtension,
+ 'width' => $oldThumbnail->width,
+ 'height' => $oldThumbnail->height,
+ 'formatChecksum' => $oldThumbnail->formatChecksum,
+ ]);
+
+ \copy(
+ $oldThumbnail->getPath() . $oldThumbnail->getSourceFilename(),
+ $newThumbnail->getPath() . $newThumbnail->getSourceFilename(),
+ );
+ }
+ }
}