Skip to content

Commit

Permalink
Merge pull request #4481 from nextcloud/fix/download-attachment
Browse files Browse the repository at this point in the history
fix: Fetch attachment share permissions
  • Loading branch information
blizzz authored Jul 11, 2023
2 parents 519637d + 6019212 commit 3b1951c
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions lib/Service/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace OCA\Text\Service;

use OC\User\NoUserException;
use OCA\Files_Sharing\SharedStorage;
use OCA\Text\Controller\AttachmentController;
use OCP\Constants;
use OCP\Files\File;
Expand Down Expand Up @@ -155,7 +156,7 @@ public function getMediaFilePublic(int $documentId, string $mediaFileName, strin
private function getMediaFullFile(string $mediaFileName, File $textFile): ?File {
$attachmentFolder = $this->getAttachmentDirectoryForFile($textFile, true);
$mediaFile = $attachmentFolder->get($mediaFileName);
if ($mediaFile instanceof File) {
if ($mediaFile instanceof File && !$this->isDownloadDisabled($mediaFile)) {
return $mediaFile;
}
return null;
Expand Down Expand Up @@ -192,7 +193,7 @@ public function getMediaFilePreviewPublic(int $documentId, string $mediaFileName
private function getMediaFilePreviewFile(string $mediaFileName, File $textFile): ?array {
$attachmentFolder = $this->getAttachmentDirectoryForFile($textFile, true);
$mediaFile = $attachmentFolder->get($mediaFileName);
if ($mediaFile instanceof File) {
if ($mediaFile instanceof File && !$this->isDownloadDisabled($mediaFile)) {
if ($this->previewManager->isMimeSupported($mediaFile->getMimeType())) {
try {
return [
Expand Down Expand Up @@ -453,13 +454,27 @@ private function getFileFromPath(string $filePath, string $userId): ?File {
$userFolder = $this->rootFolder->getUserFolder($userId);
if ($userFolder->nodeExists($filePath)) {
$file = $userFolder->get($filePath);
if ($file instanceof File) {
if ($file instanceof File && !$this->isDownloadDisabled($file)) {
return $file;
}
}
return null;
}

private function isDownloadDisabled(File $file): bool {
$storage = $file->getStorage();
if ($storage->instanceOfStorage(SharedStorage::class)) {
/** @var SharedStorage $storage */
$share = $storage->getShare();
$attributes = $share->getAttributes();
if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
return true;
}
}

return false;
}

/**
* Get a user file from file ID
*
Expand All @@ -472,9 +487,10 @@ private function getFileFromPath(string $filePath, string $userId): ?File {
*/
private function getTextFile(int $documentId, string $userId): File {
$userFolder = $this->rootFolder->getUserFolder($userId);
$textFile = $userFolder->getById($documentId);
if (count($textFile) > 0 && $textFile[0] instanceof File) {
return $textFile[0];
$files = $userFolder->getById($documentId);
$file = array_shift($files);
if ($file instanceof File && !$this->isDownloadDisabled($file)) {
return $file;
}
throw new NotFoundException('Text file with id=' . $documentId . ' was not found in storage of ' . $userId);
}
Expand All @@ -495,15 +511,16 @@ private function getTextFilePublic(?int $documentId, string $shareToken): File {
// shared file or folder?
if ($share->getNodeType() === 'file') {
$textFile = $share->getNode();
if ($textFile instanceof File) {
if ($textFile instanceof File && !$this->isDownloadDisabled($textFile)) {
return $textFile;
}
} elseif ($documentId !== null && $share->getNodeType() === 'folder') {
$folder = $share->getNode();
if ($folder instanceof Folder) {
$textFile = $folder->getById($documentId);
if (count($textFile) > 0 && $textFile[0] instanceof File) {
return $textFile[0];
$textFile = array_shift($textFile);
if ($textFile instanceof File && !$this->isDownloadDisabled($textFile)) {
return $textFile;
}
}
}
Expand Down

0 comments on commit 3b1951c

Please sign in to comment.