Skip to content

Commit 7b062d7

Browse files
committed
Do 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>
1 parent e24ca4c commit 7b062d7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lib/Listeners/LoadPublicViewerListener.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,21 @@
2929
use OCA\Files_PDFViewer\AppInfo\Application;
3030
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
3131
use OCP\AppFramework\Http\TemplateResponse;
32+
use OCP\AppFramework\QueryException;
3233
use OCP\EventDispatcher\Event;
3334
use OCP\EventDispatcher\IEventListener;
35+
use OCP\IServerContainer;
3436
use OCP\Util;
3537

3638
class LoadPublicViewerListener implements IEventListener {
39+
40+
/** @var IServerContainer */
41+
private $serverContainer;
42+
43+
public function __construct(IServerContainer $serverContainer) {
44+
$this->serverContainer = $serverContainer;
45+
}
46+
3747
public function handle(Event $event): void {
3848
if (!$event instanceof BeforeTemplateRenderedEvent) {
3949
return;
@@ -45,6 +55,27 @@ public function handle(Event $event): void {
4555
return;
4656
}
4757

58+
// Do not load the viewer if there is a download limit
59+
if ($this->getDownloadLimit($event->getShare()->getToken()) >= 0) {
60+
return;
61+
}
62+
4863
Util::addScript(Application::APP_ID, 'files_pdfviewer-public');
4964
}
65+
66+
private function getDownloadLimit(string $shareToken): int {
67+
try {
68+
$limitMapper = $this->serverContainer->get('\OCA\Files_DownloadLimit\Db\LimitMapper');
69+
} catch (QueryException $e) {
70+
return -1;
71+
}
72+
73+
try {
74+
$shareLimit = $limitMapper->get($shareToken);
75+
} catch (\Exception $e) {
76+
return -1;
77+
}
78+
79+
return $shareLimit->getLimit();
80+
}
5081
}

0 commit comments

Comments
 (0)