Skip to content

Commit 77f4c0c

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 <[email protected]>
1 parent 7ac1d65 commit 77f4c0c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lib/Listeners/LoadPublicViewerListener.php

+31
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,21 @@
2828

2929
use OCA\Files_PDFViewer\AppInfo\Application;
3030
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
31+
use OCP\AppFramework\QueryException;
3132
use OCP\EventDispatcher\Event;
3233
use OCP\EventDispatcher\IEventListener;
34+
use OCP\IServerContainer;
3335
use OCP\Util;
3436

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

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

0 commit comments

Comments
 (0)