2727namespace OCA \Text \Service ;
2828
2929use OC \User \NoUserException ;
30+ use OCA \Files_Sharing \SharedStorage ;
3031use OCA \Text \Controller \AttachmentController ;
3132use OCP \Constants ;
3233use OCP \Files \File ;
@@ -155,7 +156,7 @@ public function getMediaFilePublic(int $documentId, string $mediaFileName, strin
155156 private function getMediaFullFile (string $ mediaFileName , File $ textFile ): ?File {
156157 $ attachmentFolder = $ this ->getAttachmentDirectoryForFile ($ textFile , true );
157158 $ mediaFile = $ attachmentFolder ->get ($ mediaFileName );
158- if ($ mediaFile instanceof File) {
159+ if ($ mediaFile instanceof File && ! $ this -> isDownloadDisabled ( $ mediaFile ) ) {
159160 return $ mediaFile ;
160161 }
161162 return null ;
@@ -192,7 +193,7 @@ public function getMediaFilePreviewPublic(int $documentId, string $mediaFileName
192193 private function getMediaFilePreviewFile (string $ mediaFileName , File $ textFile ): ?array {
193194 $ attachmentFolder = $ this ->getAttachmentDirectoryForFile ($ textFile , true );
194195 $ mediaFile = $ attachmentFolder ->get ($ mediaFileName );
195- if ($ mediaFile instanceof File) {
196+ if ($ mediaFile instanceof File && ! $ this -> isDownloadDisabled ( $ mediaFile ) ) {
196197 if ($ this ->previewManager ->isMimeSupported ($ mediaFile ->getMimeType ())) {
197198 try {
198199 return [
@@ -453,13 +454,27 @@ private function getFileFromPath(string $filePath, string $userId): ?File {
453454 $ userFolder = $ this ->rootFolder ->getUserFolder ($ userId );
454455 if ($ userFolder ->nodeExists ($ filePath )) {
455456 $ file = $ userFolder ->get ($ filePath );
456- if ($ file instanceof File) {
457+ if ($ file instanceof File && ! $ this -> isDownloadDisabled ( $ file ) ) {
457458 return $ file ;
458459 }
459460 }
460461 return null ;
461462 }
462463
464+ private function isDownloadDisabled (File $ file ): bool {
465+ $ storage = $ file ->getStorage ();
466+ if ($ storage ->instanceOfStorage (SharedStorage::class)) {
467+ /** @var SharedStorage $storage */
468+ $ share = $ storage ->getShare ();
469+ $ attributes = $ share ->getAttributes ();
470+ if ($ attributes !== null && $ attributes ->getAttribute ('permissions ' , 'download ' ) === false ) {
471+ return true ;
472+ }
473+ }
474+
475+ return false ;
476+ }
477+
463478 /**
464479 * Get a user file from file ID
465480 *
@@ -472,9 +487,10 @@ private function getFileFromPath(string $filePath, string $userId): ?File {
472487 */
473488 private function getTextFile (int $ documentId , string $ userId ): File {
474489 $ userFolder = $ this ->rootFolder ->getUserFolder ($ userId );
475- $ textFile = $ userFolder ->getById ($ documentId );
476- if (count ($ textFile ) > 0 && $ textFile [0 ] instanceof File) {
477- return $ textFile [0 ];
490+ $ files = $ userFolder ->getById ($ documentId );
491+ $ file = array_shift ($ files );
492+ if ($ file instanceof File && !$ this ->isDownloadDisabled ($ file )) {
493+ return $ file ;
478494 }
479495 throw new NotFoundException ('Text file with id= ' . $ documentId . ' was not found in storage of ' . $ userId );
480496 }
@@ -495,15 +511,16 @@ private function getTextFilePublic(?int $documentId, string $shareToken): File {
495511 // shared file or folder?
496512 if ($ share ->getNodeType () === 'file ' ) {
497513 $ textFile = $ share ->getNode ();
498- if ($ textFile instanceof File) {
514+ if ($ textFile instanceof File && ! $ this -> isDownloadDisabled ( $ textFile ) ) {
499515 return $ textFile ;
500516 }
501517 } elseif ($ documentId !== null && $ share ->getNodeType () === 'folder ' ) {
502518 $ folder = $ share ->getNode ();
503519 if ($ folder instanceof Folder) {
504520 $ textFile = $ folder ->getById ($ documentId );
505- if (count ($ textFile ) > 0 && $ textFile [0 ] instanceof File) {
506- return $ textFile [0 ];
521+ $ textFile = array_shift ($ textFile );
522+ if ($ textFile instanceof File && !$ this ->isDownloadDisabled ($ textFile )) {
523+ return $ textFile ;
507524 }
508525 }
509526 }
0 commit comments