diff --git a/apps/files_sharing/lib/Controller/PublicPreviewController.php b/apps/files_sharing/lib/Controller/PublicPreviewController.php index 49e48993f5cc6..56d8d94534f15 100644 --- a/apps/files_sharing/lib/Controller/PublicPreviewController.php +++ b/apps/files_sharing/lib/Controller/PublicPreviewController.php @@ -101,4 +101,51 @@ public function getPreview( return new DataResponse([], Http::STATUS_BAD_REQUEST); } } + + /** + * @PublicPage + * @NoCSRFRequired + * @NoSameSiteCookieRequired + * + * @param $token + * @return DataResponse|FileDisplayResponse + */ + public function directLink($token) { + // No token no image + if ($token === '') { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + + // No share no image + try { + $share = $this->shareManager->getShareByToken($token); + } catch (ShareNotFound $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + + // No permissions no image + if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) { + return new DataResponse([], Http::STATUS_FORBIDDEN); + } + + // Password protected shares have no direct link! + if ($share->getPassword() !== null) { + return new DataResponse([], Http::STATUS_FORBIDDEN); + } + + try { + $node = $share->getNode(); + if ($node instanceof Folder) { + // Direct link only works for single files + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + + $f = $this->previewManager->getPreview($node, -1, -1, false); + return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); + } catch (NotFoundException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } catch (\InvalidArgumentException $e) { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + } } diff --git a/apps/files_sharing/lib/Controller/ShareController.php b/apps/files_sharing/lib/Controller/ShareController.php index 14fc8d6338109..a7cf1a78971b0 100644 --- a/apps/files_sharing/lib/Controller/ShareController.php +++ b/apps/files_sharing/lib/Controller/ShareController.php @@ -372,9 +372,14 @@ public function showShare($token, $path = '') { $shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024); $shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024); $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null); + $shareTmpl['previewURL'] = $shareTmpl['downloadURL']; if ($shareTmpl['previewSupported']) { $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview', ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 't' => $shareTmpl['dirToken']]); + // We just have direct previews for image files + if ($share->getNode()->getMimePart() === 'image') { + $shareTmpl['previewURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]); + } } else { $shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png')); } diff --git a/apps/files_sharing/templates/public.php b/apps/files_sharing/templates/public.php index a593e596dfb53..8bbb53fa4e078 100644 --- a/apps/files_sharing/templates/public.php +++ b/apps/files_sharing/templates/public.php @@ -92,7 +92,7 @@