diff --git a/lib/Controller/AttachmentController.php b/lib/Controller/AttachmentController.php index a4fca259cc3..050533f1dbe 100644 --- a/lib/Controller/AttachmentController.php +++ b/lib/Controller/AttachmentController.php @@ -216,13 +216,18 @@ public function getImageFile(string $imageFileName, string $shareToken = '', $userId = $this->getUserId(); $imageFile = $this->attachmentService->getImageFile($documentId, $imageFileName, $userId, $preferRawImage === 1); } - return $imageFile !== null - ? new DataDownloadResponse( + + if ($imageFile !== null) { + $response = new DataDownloadResponse( $imageFile->getContent(), $imageFile->getName(), $this->getSecureMimeType($imageFile->getMimeType()) - ) - : new DataResponse('', Http::STATUS_NOT_FOUND); + ); + $response->cacheFor(3600 * 24, false, true); + return $response; + } + + return new DataResponse('', Http::STATUS_NOT_FOUND); } catch (Exception $e) { $this->logger->error('getImageFile error', ['exception' => $e]); return new DataResponse('', Http::STATUS_NOT_FOUND); diff --git a/lib/Service/AttachmentService.php b/lib/Service/AttachmentService.php index 44f246fd8dc..d6ac822b863 100755 --- a/lib/Service/AttachmentService.php +++ b/lib/Service/AttachmentService.php @@ -23,6 +23,7 @@ use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\Files\SimpleFS\ISimpleFile; +use OCP\FilesMetadata\IFilesMetadataManager; use OCP\IPreview; use OCP\IURLGenerator; use OCP\Lock\LockedException; @@ -39,6 +40,7 @@ public function __construct( private IMimeTypeDetector $mimeTypeDetector, private IURLGenerator $urlGenerator, private IFilenameValidator $filenameValidator, + private IFilesMetadataManager $filesMetadataManager, ) { } @@ -219,21 +221,34 @@ public function getAttachmentList(int $documentId, ?string $userId = null, ?Sess $attachments = []; $userFolder = $userId !== null ? $this->rootFolder->getUserFolder($userId) : null; + + $fileNodes = []; + $fileIds = []; foreach ($attachmentDir->getDirectoryListing() as $node) { - if (!($node instanceof File)) { - // Ignore anything but files - continue; + if ($node instanceof File) { + // we only want Files + $fileNodes[] = $node; + $fileIds[] = $node->getId(); } + } + + // this is done outside the loop for efficiency + $metadataMap = $this->filesMetadataManager->getMetadataForFiles($fileIds); + + foreach ($fileNodes as $node) { $isImage = in_array($node->getMimetype(), AttachmentController::IMAGE_MIME_TYPES, true); $name = $node->getName(); + $fileId = $node->getId(); + $metadata = $metadataMap[$fileId] ?? null; $attachments[] = [ - 'fileId' => $node->getId(), + 'fileId' => $fileId, 'name' => $name, 'size' => Util::humanFileSize($node->getSize()), 'mimetype' => $node->getMimeType(), 'mtime' => $node->getMTime(), 'isImage' => $isImage, 'davPath' => $userFolder?->getRelativePath($node->getPath()), + 'metadata' => $metadata, 'fullUrl' => $isImage ? $this->urlGenerator->linkToRouteAbsolute('text.Attachment.getImageFile') . $urlParamsBase . '&imageFileName=' . rawurlencode($name) . '&preferRawImage=1' : $this->urlGenerator->linkToRouteAbsolute('text.Attachment.getMediaFile') . $urlParamsBase . '&mediaFileName=' . rawurlencode($name), diff --git a/src/nodes/ImageView.vue b/src/nodes/ImageView.vue index cc5ad026b65..7dc864e1622 100644 --- a/src/nodes/ImageView.vue +++ b/src/nodes/ImageView.vue @@ -6,6 +6,7 @@