Skip to content

Commit aa3bb61

Browse files
committed
fixup! fix: keep track of download count
Signed-off-by: Hamza <[email protected]>
1 parent 01e6c04 commit aa3bb61

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

lib/Listener/BeforeNodeReadListener.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
use OCP\Files\Folder;
2020
use OCP\Files\NotFoundException;
2121
use OCP\Files\Storage\ISharedStorage;
22+
use OCP\ICache;
23+
use OCP\ICacheFactory;
24+
use OCP\IRequest;
25+
use OCP\ISession;
2226
use OCP\Share\IManager;
2327
use OCP\Share\IShare;
2428
use Psr\Log\LoggerInterface;
@@ -27,11 +31,16 @@
2731
* @template-implements IEventListener<BeforeNodeReadEvent|BeforeZipCreatedEvent|Event>
2832
*/
2933
class BeforeNodeReadListener implements IEventListener {
34+
private ICache $cache;
3035
public function __construct(
3136
private IManager $manager,
3237
private LimitMapper $mapper,
3338
private LoggerInterface $logger,
39+
private IRequest $request,
40+
private ISession $session,
41+
ICacheFactory $cacheFactory,
3442
) {
43+
$this->cache = $cacheFactory->createDistributed('files_downloadlimit_event');
3544
}
3645

3746
public function handle(Event $event): void {
@@ -67,10 +76,13 @@ public function handleBeforeZipCreatedEvent(BeforeZipCreatedEvent $event): void
6776
/** @var ISharedStorage $storage */
6877
$share = $storage->getShare();
6978

70-
if ($share->getShareType() !== IShare::TYPE_LINK) {
79+
if (!in_array($share->getShareType(), [IShare::TYPE_EMAIL, IShare::TYPE_LINK])) {
7180
return;
7281
}
7382

83+
/* Cache that that folder download activity was published */
84+
$this->cache->set($this->request->getId(), $node->getPath(), 3600);
85+
7486
$this->singleFileDownloaded($share);
7587
}
7688

@@ -93,10 +105,25 @@ public function handleBeforeNodeReadEvent(BeforeNodeReadEvent $event): void {
93105
/** @var ISharedStorage $storage */
94106
$share = $storage->getShare();
95107

96-
if ($share->getShareType() !== IShare::TYPE_LINK) {
108+
if (!in_array($share->getShareType(), [IShare::TYPE_EMAIL, IShare::TYPE_LINK])) {
109+
return;
110+
}
111+
112+
$path = $this->cache->get($this->request->getId());
113+
if (is_string($path) && str_starts_with($node->getPath(), $path)) {
114+
/* An activity was published for a containing folder already */
97115
return;
98116
}
99117

118+
/* Avoid publishing several activities for one video playing */
119+
$cacheKey = $node->getId() . $node->getPath() . $this->session->getId();
120+
if (($this->request->getHeader('range') !== '') && ($this->cache->get($cacheKey) === 'true')) {
121+
/* This is a range request and an activity for the same file was published in the same session */
122+
return;
123+
}
124+
$this->cache->set($cacheKey, 'true', 3600);
125+
126+
100127
$this->singleFileDownloaded($share);
101128
}
102129

0 commit comments

Comments
 (0)