diff options
-rw-r--r-- | controller/pagecontroller.php | 78 | ||||
-rw-r--r-- | environment/environment.php | 17 | ||||
-rw-r--r-- | service/downloadservice.php | 14 |
3 files changed, 65 insertions, 44 deletions
diff --git a/controller/pagecontroller.php b/controller/pagecontroller.php index a3f3149d..3b8a08e2 100644 --- a/controller/pagecontroller.php +++ b/controller/pagecontroller.php @@ -17,6 +17,7 @@ namespace OCA\GalleryPlus\Controller; use OCP\IURLGenerator; use OCP\IRequest; use OCP\IConfig; +use OCP\Files\File; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; @@ -118,40 +119,19 @@ class PageController extends Controller { * @PublicPage * @NoCSRFRequired * - * Shows the albums and pictures the token gives access to + * Shows the albums and pictures or download the single file the token gives access to * * @param string $token * @param null|string $filename * - * @return TemplateResponse + * @return TemplateResponse|ImageResponse|RedirectResponse */ public function publicIndex($token, $filename) { - if (is_null($filename)) { - $appName = $this->appName; - $displayName = $this->environment->getDisplayName(); - $albumName = $this->environment->getSharedFolderName(); - $server2ServerSharing = $this->appConfig->getAppValue( - 'files_sharing', 'outgoing_server2server_share_enabled', 'yes' - ); - $server2ServerSharing = ($server2ServerSharing === 'yes') ? true : false; - $protected = $this->environment->isShareProtected(); - $protected = ($protected) ? 'true' : 'false'; - - // Parameters sent to the template - $params = [ - 'appName' => $appName, - 'token' => $token, - 'displayName' => $displayName, - 'albumName' => $albumName, - 'server2ServerSharing' => $server2ServerSharing, - 'protected' => $protected, - 'filename' => $albumName - ]; - - // Will render the page using the template found in templates/public.php - return new TemplateResponse($appName, 'public', $params, 'public'); + $node = $this->environment->getSharedNode(); + if ($node->getType() === 'dir') { + return $this->showPublicPage($token); } else { - return $this->downloadFile(); + return $this->downloadFile($node, $filename); } } @@ -195,16 +175,50 @@ class PageController extends Controller { } /** - * @PublicPage - * @NoCSRFRequired + * Shows the albums and pictures the token gives access to + * + * @param $token * + * @return TemplateResponse + */ + private function showPublicPage($token) { + $albumName = $this->environment->getSharedFolderName(); + $server2ServerSharing = $this->appConfig->getAppValue( + 'files_sharing', 'outgoing_server2server_share_enabled', 'yes' + ); + $server2ServerSharing = ($server2ServerSharing === 'yes') ? true : false; + $protected = $this->environment->isShareProtected(); + $protected = ($protected) ? 'true' : 'false'; + // Parameters sent to the template + $params = [ + 'appName' => $this->appName, + 'token' => $token, + 'displayName' => $this->environment->getDisplayName(), + 'albumName' => $albumName, + 'server2ServerSharing' => $server2ServerSharing, + 'protected' => $protected, + 'filename' => $albumName + ]; + + // Will render the page using the template found in templates/public.php + return new TemplateResponse($this->appName, 'public', $params, 'public'); + } + + /** * Downloads the file associated with a token * - * @return \OCA\GalleryPlus\Http\ImageResponse|Http\RedirectResponse + * @param File $file + * @param string|null $filename + * + * @return ImageResponse|RedirectResponse */ - private function downloadFile() { + private function downloadFile($file, $filename) { try { - $download = $this->downloadService->downloadFile(); + $download = $this->downloadService->downloadFile($file); + if (is_null($filename)) { + $filename = $file->getName(); + } + $download['name'] = $filename; return new ImageResponse($download); } catch (ServiceException $exception) { diff --git a/environment/environment.php b/environment/environment.php index d116b60a..35504273 100644 --- a/environment/environment.php +++ b/environment/environment.php @@ -57,6 +57,10 @@ class Environment { */ private $userManager; /** + * @var int + */ + private $sharedNodeId; + /** * @var IServerContainer */ private $serverContainer; @@ -122,8 +126,8 @@ class Environment { $this->userFolder = $this->setupFilesystem($origShareOwner); // This is actually the node ID - $fileSource = $linkItem['file_source']; - $this->fromRootToFolder = $this->buildFromRootToFolder($fileSource); + $this->sharedNodeId = $linkItem['file_source']; + $this->fromRootToFolder = $this->buildFromRootToFolder($this->sharedNodeId); $this->folderName = $linkItem['file_target']; $this->userId = $rootLinkItem['uid_owner']; @@ -187,6 +191,15 @@ class Environment { } /** + * Returns the shared node + * + * @return File|Folder + */ + public function getSharedNode() { + return $this->getResourceFromId($this->sharedNodeId); + } + + /** * Returns the resource identified by the given ID * * @param int $resourceId diff --git a/service/downloadservice.php b/service/downloadservice.php index 88279170..dee7cc19 100644 --- a/service/downloadservice.php +++ b/service/downloadservice.php @@ -34,18 +34,12 @@ class DownloadService extends Service { * * @throws NotFoundServiceException */ - public function downloadFile($file = null, $base64Encode = false) { + public function downloadFile($file, $base64Encode = false) { $download = false; try { - // If no file is given, we try to get it from the token - if (is_null($file)) { - $this->logger->debug("[DownloadService] File to Download: File linked with token"); - $file = $this->environment->getResourceFromPath(''); - } else { - $this->logger->debug( - "[DownloadService] File to Download: {name}", ['name' => $file->getName()] - ); - } + $this->logger->debug( + "[DownloadService] File to Download: {name}", ['name' => $file->getName()] + ); $download = [ 'preview' => $file->getContent(), 'mimetype' => $file->getMimeType() |