Skip to content

Commit b98fda1

Browse files
committed
fix(share): get file from context for attachments
Signed-off-by: Max <max@nextcloud.com>
1 parent bcbbc4a commit b98fda1

2 files changed

Lines changed: 16 additions & 25 deletions

File tree

lib/Service/AttachmentService.php

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public function uploadAttachment(int $documentId, string $newFileName, $newFileR
309309
* @throws InvalidPathException
310310
* @throws NoUserException
311311
*/
312-
public function uploadAttachmentPublic(?int $documentId, string $newFileName, $newFileResource, string $shareToken): array {
312+
public function uploadAttachmentPublic(int $documentId, string $newFileName, $newFileResource, string $shareToken): array {
313313
try {
314314
$share = $this->shareManager->getShareByToken($shareToken);
315315
} catch (ShareNotFound) {
@@ -541,32 +541,23 @@ private function getTextFile(int $documentId, string $userId): File {
541541
*
542542
* @throws NotFoundException
543543
*/
544-
private function getTextFilePublic(?int $documentId, string $shareToken): File {
545-
// is the file shared with this token?
544+
private function getTextFilePublic(int $documentId, string $shareToken): File {
545+
// TODO: Lazy load the context and enable these additional checks inside the context
546546
try {
547547
$share = $this->shareManager->getShareByToken($shareToken);
548-
if (in_array($share->getShareType(), [IShare::TYPE_LINK, IShare::TYPE_EMAIL])) {
549-
// shared file or folder?
550-
if ($share->getNodeType() === 'file') {
551-
$textFile = $share->getNode();
552-
if ($textFile instanceof File
553-
&& !$this->isDownloadDisabled($textFile)
554-
&& $textFile->getId() === $documentId
555-
) {
556-
return $textFile;
557-
}
558-
} elseif ($documentId !== null && $share->getNodeType() === 'folder') {
559-
$folder = $share->getNode();
560-
if ($folder instanceof Folder) {
561-
$textFile = $folder->getFirstNodeById($documentId);
562-
if ($textFile instanceof File && !$this->isDownloadDisabled($textFile)) {
563-
return $textFile;
564-
}
565-
}
566-
}
567-
}
568548
} catch (ShareNotFound) {
569-
// same as below
549+
throw new NotFoundException();
550+
}
551+
if (!in_array($share->getShareType(), [IShare::TYPE_LINK, IShare::TYPE_EMAIL])) {
552+
throw new NotFoundException();
553+
}
554+
$document = $this->documentMapper->find($documentId);
555+
$type = $document->getContextType();
556+
$id = $document->getContextId();
557+
$context = $this->contextManager->getContext($type, $id, $shareToken);
558+
$file = $context->getFile();
559+
if ($file instanceof File && !$this->isDownloadDisabled($file)) {
560+
return $file;
570561
}
571562
throw new NotFoundException('Text file with id=' . (string)$documentId . ' and shareToken ' . $shareToken . ' was not found.');
572563
}

lib/Service/FileService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function getFileByIdFromShare(int $fileId, string $shareToken): File {
4848
if ($node instanceof Folder) {
4949
$node = $node->getFirstNodeById($fileId);
5050
}
51-
if ($node instanceof File) {
51+
if ($node instanceof File && $node->getId() === $fileId) {
5252
return $node;
5353
}
5454
throw new NotFoundException();

0 commit comments

Comments
 (0)