*/
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();
}
/**
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
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;
*/
class File extends DatabaseObject
{
+ /** @var array<string, FileThumbnail> */
+ private array $thumbnails = [];
+
public function getPath(): string
{
$folderA = \substr($this->fileHash, 0, 2);
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;
+ }
}
FontAwesomeIcon::fromValues('magnifying-glass')->toHtml(24),
);
- $linkParameters = [
- 'object' => $attachment,
- ];
- if ($attachment->hasThumbnail()) {
- $linkParameters['thumbnail'] = 1;
- }
-
$class = match ($alignment) {
"left" => "messageFloatObjectLeft",
"right" => "messageFloatObjectRight",
$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,
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;
}
}
- 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);
+ }
+ }
}
}