Skip to content

Commit d6cf529

Browse files
committed
feat: Make it possible to add a storage wrapper using public APIs
Before that every app is using an internal call to Filesystem::addStorageWrapper. The new API enforce that this is called at the right moment. Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent ecbb5d8 commit d6cf529

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

lib/private/Files/SetupManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,10 @@ private function oneTimeUserSetup(IUser $user) {
319319
$event = new BeforeFileSystemSetupEvent($user);
320320
$this->eventDispatcher->dispatchTyped($event);
321321

322+
foreach ($event->getStorageWrappers() as $name => $wrapper) {
323+
Filesystem::addStorageWrapper($name, $wrapper['wrapper'], $wrapper['priority']);
324+
}
325+
322326
Filesystem::logWarningWhenAddingStorageWrapper($prevLogging);
323327

324328
$userDir = '/' . $user->getUID() . '/files';

lib/public/Files/Events/BeforeFileSystemSetupEvent.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,23 @@
1010
namespace OCP\Files\Events;
1111

1212
use OCP\EventDispatcher\Event;
13+
use OCP\Files\Storage\IStorage;
1314
use OCP\IUser;
1415

1516
/**
16-
* Event triggered before the file system is setup
17+
* Event triggered before the file system is set up.
1718
*
1819
* @since 31.0.0
1920
*/
2021
class BeforeFileSystemSetupEvent extends Event {
22+
/** @var array<string, array{wrapper: callable(string $mountPoint, IStorage $storage): IStorage, priority: int}> $storageWrappers */
23+
private array $storageWrappers = [];
24+
2125
/**
2226
* @since 31.0.0
2327
*/
2428
public function __construct(
25-
private IUser $user,
29+
private readonly IUser $user,
2630
) {
2731
parent::__construct();
2832
}
@@ -33,4 +37,25 @@ public function __construct(
3337
public function getUser(): IUser {
3438
return $this->user;
3539
}
40+
41+
/**
42+
* Add a storage wrapper to the file system. This allows apps to inject storage wrappers
43+
* for every mount.
44+
*
45+
* @param callable(string $mountPoint, IStorage $storage): IStorage $wrapper
46+
* @since 35.0.0
47+
*/
48+
public function addStorageWrapper(string $name, callable $wrapper, int $priority = 50): void {
49+
$this->storageWrappers[$name] = ['wrapper' => $wrapper, 'priority' => $priority];
50+
}
51+
52+
/**
53+
* Get the storage wrappers.
54+
*
55+
* @since 35.0.0
56+
* @return array<string, array{wrapper: callable(string $mountPoint, IStorage $storage): IStorage, priority: int}>
57+
*/
58+
public function getStorageWrappers(): array {
59+
return $this->storageWrappers;
60+
}
3661
}

0 commit comments

Comments
 (0)