Skip to content

Commit 21d3468

Browse files
authored
Merge pull request #6381 from WoltLab/6.1-attachment-load-file
Only load the file to the attachments if it is actually needed
2 parents 948bdbf + 99bbaa9 commit 21d3468

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

wcfsetup/install/files/lib/data/attachment/Attachment.class.php

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace wcf\data\attachment;
44

55
use wcf\data\DatabaseObject;
6-
use wcf\data\ILinkableObject;
7-
use wcf\data\IThumbnailFile;
86
use wcf\data\file\File;
97
use wcf\data\file\thumbnail\FileThumbnailList;
8+
use wcf\data\ILinkableObject;
9+
use wcf\data\IThumbnailFile;
1010
use wcf\data\object\type\ObjectTypeCache;
1111
use wcf\system\request\IRouteController;
1212
use wcf\system\request\LinkHandler;
@@ -390,46 +390,62 @@ public function getFile(): ?File
390390

391391
public function setFile(File $file): void
392392
{
393-
if ($this->file->fileID === $file->fileID) {
393+
if ($this->fileID === $file->fileID) {
394394
$this->file = $file;
395395
}
396396
}
397397

398398
#[\Override]
399399
public function __get($name)
400400
{
401-
$file = $this->getFile();
402-
if ($file === null) {
403-
return parent::__get($name);
404-
}
405-
406401
if ($name === 'downloads' || $name === 'lastDownloadTime') {
407402
// Static files are no longer served through PHP but the web server
408403
// instead, therefore we can no longer report any meaningful numbers.
409404
//
410405
// For existing files the stored value is suppressed because it is
411406
// not going to be increased ever, possibly creating a false
412407
// impression when the historic stored value is being reported.
413-
if ($file->isStaticFile()) {
408+
if ($this->getFile()?->isStaticFile()) {
414409
return 0;
415410
}
416411
}
417412

418-
return match ($name) {
419-
'filename' => $file->filename,
420-
'filesize' => $file->fileSize,
421-
'fileType' => $file->mimeType,
422-
'isImage' => $file->isImage() ? 1 : 0,
423-
'height' => $file->height ?: 0,
424-
'width' => $file->width ?: 0,
425-
'thumbnailType' => $file->getThumbnail('')?->getMimeType() ?: '',
426-
'thumbnailWidth' => $file->getThumbnail('')?->width ?: 0,
427-
'thumbnailHeight' => $file->getThumbnail('')?->height ?: 0,
428-
'tinyThumbnailType' => $file->getThumbnail('tiny')?->getMimeType() ?: '',
429-
'tinyThumbnailWidth' => $file->getThumbnail('tiny')?->width ?: 0,
430-
'tinyThumbnailHeight' => $file->getThumbnail('tiny')?->height ?: 0,
431-
default => parent::__get($name),
432-
};
413+
if (\in_array($name, [
414+
'filename',
415+
'filesize',
416+
'fileType',
417+
'isImage',
418+
'height',
419+
'width',
420+
'thumbnailType',
421+
'thumbnailWidth',
422+
'thumbnailHeight',
423+
'tinyThumbnailType',
424+
'tinyThumbnailWidth',
425+
'tinyThumbnailHeight',
426+
])) {
427+
$file = $this->getFile();
428+
if ($file === null) {
429+
return parent::__get($name);
430+
}
431+
432+
return match ($name) {
433+
'filename' => $file->filename,
434+
'filesize' => $file->fileSize,
435+
'fileType' => $file->mimeType,
436+
'isImage' => $file->isImage() ? 1 : 0,
437+
'height' => $file->height ?: 0,
438+
'width' => $file->width ?: 0,
439+
'thumbnailType' => $file->getThumbnail('')?->getMimeType() ?: '',
440+
'thumbnailWidth' => $file->getThumbnail('')?->width ?: 0,
441+
'thumbnailHeight' => $file->getThumbnail('')?->height ?: 0,
442+
'tinyThumbnailType' => $file->getThumbnail('tiny')?->getMimeType() ?: '',
443+
'tinyThumbnailWidth' => $file->getThumbnail('tiny')?->width ?: 0,
444+
'tinyThumbnailHeight' => $file->getThumbnail('tiny')?->height ?: 0,
445+
};
446+
}
447+
448+
return parent::__get($name);
433449
}
434450

435451
public function toHtmlElement(): ?string

0 commit comments

Comments
 (0)