Skip to content

Commit

Permalink
Add support for attachment thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
dtdesign committed Mar 27, 2024
1 parent ab1a938 commit 040aad8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 24 deletions.
52 changes: 37 additions & 15 deletions wcfsetup/install/files/lib/data/attachment/Attachment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,21 @@ final protected function getLocationHelper($location)
*/
public function getThumbnailLink($size = '')
{
$parameters = [
'object' => $this,
];
$file = $this->getFile();
if ($file === null) {
return '';
}

if ($size == 'tiny') {
$parameters['tiny'] = 1;
} elseif ($size == 'thumbnail') {
$parameters['thumbnail'] = 1;
if ($size === '') {
return $file->getLink();
}

$thumbnail = $file->getThumbnail($size !== 'tiny' ? '' : $size);
if ($this === null) {
return '';
}

return LinkHandler::getInstance()->getLink('Attachment', $parameters);
return $thumbnail->getLink();
}

/**
Expand Down Expand Up @@ -373,15 +377,33 @@ public function setFile(File $file): void
public function __get($name)
{
$file = $this->getFile();
if ($file !== null) {
return match ($name) {
'filename' => $file->filename,
'filesize' => $file->fileSize,
default => parent::__get($name),
};
if ($file === null) {
return parent::__get($name);
}

return parent::__get($name);
return match ($name) {
'filename' => $file->filename,
'filesize' => $file->fileSize,
'fileType' => $file->mimeType,
'isImage' => $file->isImage(),

// TODO: Do we want to cache this data?
'height' => \getimagesize($file->getPath() . $file->getSourceFilename())[1],
// TODO: Do we want to cache this data?
'width' => \getimagesize($file->getPath() . $file->getSourceFilename())[0],

// TODO: This is awful.
'thumbnailType' => $file->getThumbnail('') ? $file->mimeType : '',
'thumbnailWidth' => $file->getThumbnail('') ? \getimagesize($file->getThumbnail('')->getPath() . $file->getThumbnail('')->getSourceFilename())[0] : 0,
'thumbnailHeight' => $file->getThumbnail('') ? \getimagesize($file->getThumbnail('')->getPath() . $file->getThumbnail('')->getSourceFilename())[1] : 0,

// TODO: This is awful.
'tinyThumbnailType' => $file->getThumbnail('tiny') ? $file->mimeType : '',
'tinyThumbnailWidth' => $file->getThumbnail('tiny') ? \getimagesize($file->getThumbnail('tiny')->getPath() . $file->getThumbnail('tiny')->getSourceFilename())[0] : 0,
'tinyThumbnailHeight' => $file->getThumbnail('tiny') ? \getimagesize($file->getThumbnail('tiny')->getPath() . $file->getThumbnail('tiny')->getSourceFilename())[1] : 0,

default => parent::__get($name),
};
}

public static function findByFileID(int $fileID): ?Attachment
Expand Down
14 changes: 14 additions & 0 deletions wcfsetup/install/files/lib/data/file/File.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use wcf\action\FileDownloadAction;
use wcf\data\DatabaseObject;
use wcf\data\file\thumbnail\FileThumbnail;
use wcf\system\file\processor\FileProcessor;
use wcf\system\file\processor\IFileProcessor;
use wcf\system\request\LinkHandler;
Expand All @@ -23,6 +24,9 @@
*/
class File extends DatabaseObject
{
/** @var array<string, FileThumbnail> */
private array $thumbnails = [];

public function getPath(): string
{
$folderA = \substr($this->fileHash, 0, 2);
Expand Down Expand Up @@ -77,4 +81,14 @@ public function canDelete(): bool

return $processor->canDelete($this);
}

public function addThumbnail(FileThumbnail $thumbnail): void
{
$this->thumbnails[$thumbnail->identifier] = $thumbnail;
}

public function getThumbnail(string $identifier): ?FileThumbnail
{
return $this->thumbnails[$identifier] ?? null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,6 @@ private function showImageAsThumbnail(Attachment $attachment, string $alignment,
FontAwesomeIcon::fromValues('magnifying-glass')->toHtml(24),
);

$linkParameters = [
'object' => $attachment,
];
if ($attachment->hasThumbnail()) {
$linkParameters['thumbnail'] = 1;
}

$class = match ($alignment) {
"left" => "messageFloatObjectLeft",
"right" => "messageFloatObjectRight",
Expand All @@ -156,9 +149,11 @@ private function showImageAsThumbnail(Attachment $attachment, string $alignment,
$imageClasses .= ' ' . $class;
}

$src = $attachment->hasThumbnail() ? $attachment->getThumbnailLink('thumbnail') : $attachment->getLink();

$imageElement = \sprintf(
'<img src="%s" class="%s" width="%d" height="%d" alt="" loading="lazy">',
StringUtil::encodeHTML(LinkHandler::getInstance()->getLink('Attachment', $linkParameters)),
StringUtil::encodeHTML($src),
$imageClasses,
$attachment->hasThumbnail() ? $attachment->thumbnailWidth : $attachment->width,
$attachment->hasThumbnail() ? $attachment->thumbnailHeight : $attachment->height,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace wcf\system\message\embedded\object;

use wcf\data\attachment\Attachment;
use wcf\data\attachment\AttachmentList;
use wcf\data\file\FileList;
use wcf\data\file\thumbnail\FileThumbnailList;
use wcf\data\object\type\ObjectTypeCache;
use wcf\system\html\input\HtmlInputProcessor;

Expand Down Expand Up @@ -71,6 +74,46 @@ public function loadObjects(array $objectIDs)
}
}

return $attachmentList->getObjects();
$attachments = $attachmentList->getObjects();

$this->loadFiles($attachments);

return $attachments;
}

/**
* @param Attachment[] $attachments
*/
private function loadFiles(array $attachments): void
{
$fileIDs = [];
foreach ($attachments as $attachment) {
if ($attachment->fileID) {
$fileIDs[] = $attachment->fileID;
}
}

if ($fileIDs === []) {
return;
}

$fileList = new FileList();
$fileList->getConditionBuilder()->add("fileID IN (?)", [$fileIDs]);
$fileList->readObjects();
$files = $fileList->getObjects();

$thumbnailList = new FileThumbnailList();
$thumbnailList->getConditionBuilder()->add("fileID IN (?)", [$fileIDs]);
$thumbnailList->readObjects();
foreach ($thumbnailList as $thumbnail) {
$files[$thumbnail->fileID]->addThumbnail($thumbnail);
}

foreach ($attachments as $attachment) {
$file = $files[$attachment->fileID] ?? null;
if ($file !== null) {
$attachment->setFile($file);
}
}
}
}

0 comments on commit 040aad8

Please sign in to comment.