Skip to content

Commit

Permalink
Do not load PDF viewer if public share has a download limit
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
danxuliu committed Sep 6, 2022
1 parent e24ca4c commit 7b062d7
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/Listeners/LoadPublicViewerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@
use OCA\Files_PDFViewer\AppInfo\Application;
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCP\AppFramework\Http\TemplateResponse;
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;
Expand All @@ -45,6 +55,27 @@ public function handle(Event $event): void {
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();
}
}

0 comments on commit 7b062d7

Please sign in to comment.