diff options
author | Daniel Calviño Sánchez <danxuliu@gmail.com> | 2022-09-06 04:00:27 +0300 |
---|---|---|
committer | Daniel Calviño Sánchez <danxuliu@gmail.com> | 2022-09-07 03:57:45 +0300 |
commit | 77f4c0cd043d70c3baba6bca04e5bdcdee2c1047 (patch) | |
tree | 4c97642def33f43e5a2e24e64c8ec03e1c8f3b57 | |
parent | 7ac1d65bcd971f80257191c99615648d5b10c0ca (diff) |
Do not load PDF viewer if public share has a download limitdo-not-load-pdf-viewer-if-public-share-has-a-download-limit
The PDF viewer needs to download the file in order to render it in the
client. Due to this, if a public share has a download limit, loading the
PDF viewer automatically reduces the number of available downloads by
one, even if the user does not download the file manually. To prevent
that now the PDF viewer is not loaded in public shares if the share has
a download limit.
Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
-rw-r--r-- | lib/Listeners/LoadPublicViewerListener.php | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/Listeners/LoadPublicViewerListener.php b/lib/Listeners/LoadPublicViewerListener.php index e7996b8..8dbc959 100644 --- a/lib/Listeners/LoadPublicViewerListener.php +++ b/lib/Listeners/LoadPublicViewerListener.php @@ -28,11 +28,21 @@ namespace OCA\Files_PDFViewer\Listeners; use OCA\Files_PDFViewer\AppInfo\Application; use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent; +use OCP\AppFramework\QueryException; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; +use OCP\IServerContainer; use OCP\Util; class LoadPublicViewerListener implements IEventListener { + + /** @var IServerContainer */ + private $serverContainer; + + public function __construct(IServerContainer $serverContainer) { + $this->serverContainer = $serverContainer; + } + public function handle(Event $event): void { if (!$event instanceof BeforeTemplateRenderedEvent) { return; @@ -44,6 +54,27 @@ class LoadPublicViewerListener implements IEventListener { return; } + // Do not load the viewer if there is a download limit + if ($this->getDownloadLimit($event->getShare()->getToken()) >= 0) { + return; + } + Util::addScript(Application::APP_ID, 'files_pdfviewer-public'); } + + private function getDownloadLimit(string $shareToken): int { + try { + $limitMapper = $this->serverContainer->get('\OCA\Files_DownloadLimit\Db\LimitMapper'); + } catch (QueryException $e) { + return -1; + } + + try { + $shareLimit = $limitMapper->get($shareToken); + } catch (\Exception $e) { + return -1; + } + + return $shareLimit->getLimit(); + } } |