From ed6f9c2c8c77874cd258c31117106c3b0d9a3595 Mon Sep 17 00:00:00 2001 From: Gerben Date: Sat, 29 Sep 2018 12:56:51 +0300 Subject: [PATCH] Support serving a file within a shared directory --- appinfo/routes.php | 2 ++ lib/Controller/PubPageController.php | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 045f35f..2d02f93 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -3,6 +3,8 @@ return [ 'routes' => [ ['name' => 'pubPage#getByToken', 'url' => '/s/{token}', 'verb' => 'GET'], + ['name' => 'pubPage#getByTokenAndPath', 'url' => '/s/{token}/{path}', 'verb' => 'GET', + 'requirements' => array('path' => '.+')], ['name' => 'privatePage#getByPath', 'url' => '/files/{path}', 'verb' => 'GET', 'requirements' => array('path' => '.+')], ] diff --git a/lib/Controller/PubPageController.php b/lib/Controller/PubPageController.php index 7284d29..284d5ef 100644 --- a/lib/Controller/PubPageController.php +++ b/lib/Controller/PubPageController.php @@ -1,6 +1,7 @@ manager->getShareByToken($token); $node = $share->getNode(); - // if ($node->getType() === 'dir') { TODO } + if ($node->getType() === 'dir') { + // Is there some reasonable thing to return for a directory? An html index? A tarball? + throw new Exception("Requested share is a directory, not a file."); + } $content = $node->getContent(); $mimetype = $node->getMimeType(); $this->returnRawResponse($content, $mimetype); } + + /** + * @PublicPage + * @NoAdminRequired + * @NoCSRFRequired + */ + public function getByTokenAndPath($token, $path) { + $share = $this->manager->getShareByToken($token); + $dirNode = $share->getNode(); + if ($dirNode->getType() !== 'dir') { + throw new Exception("Received a sub-path for a share that is not a directory"); + } + $fileNode = $dirNode->get($path); + if ($fileNode->getType() === 'dir') { + // Is there some reasonable thing to return for a directory? An html index? A tarball? + throw new Exception("Requested share is a directory, not a file."); + } + $content = $fileNode->getContent(); + $mimetype = $fileNode->getMimeType(); + $this->returnRawResponse($content, $mimetype); + } }