-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathCacheListener.php
42 lines (35 loc) Β· 1.17 KB
/
CacheListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\GroupFolders;
use OCA\GroupFolders\Mount\GroupFolderStorage;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\CacheEntryInsertedEvent;
use OCP\Files\Cache\CacheEntryUpdatedEvent;
use OCP\Files\Cache\ICacheEvent;
use RuntimeException;
class CacheListener {
public function __construct(
private readonly IEventDispatcher $eventDispatcher,
) {
}
public function listen(): void {
$this->eventDispatcher->addListener(CacheEntryInsertedEvent::class, $this->onCacheEvent(...), 99999);
$this->eventDispatcher->addListener(CacheEntryUpdatedEvent::class, $this->onCacheEvent(...), 99999);
}
public function onCacheEvent(ICacheEvent $event): void {
if (!$event->getStorage()->instanceOfStorage(GroupFolderStorage::class)) {
return;
}
$jailedPath = preg_replace('/^__groupfolders\/\d+\//', '', $event->getPath());
if ($jailedPath === null) {
throw new RuntimeException('Failed to build jailed path');
}
if ($jailedPath !== $event->getPath()) {
$event->setPath($jailedPath);
}
}
}