Skip to content

Commit

Permalink
Add support for copying files
Browse files Browse the repository at this point in the history
  • Loading branch information
dtdesign committed Jun 8, 2024
1 parent 29f2187 commit 34bc25c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
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;

/**
Expand Down Expand Up @@ -91,6 +93,30 @@ public function copy()

$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'],
Expand All @@ -114,18 +140,11 @@ public function copy()
'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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,59 @@ public function getMaximumFileSize(): int

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(),
);
}
}
}

0 comments on commit 34bc25c

Please sign in to comment.