Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/rector-strict.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
$nextcloudDir . '/apps/settings/lib/Service/AuthorizedGroupService.php',
$nextcloudDir . '/lib/private/Files/Storage/Storage.php',
$nextcloudDir . '/lib/private/Files/Storage/Wrapper/Wrapper.php',
$nextcloudDir . '/lib/private/Files/Storage/StorageFactory.php',
])
->withPreparedSets(
deadCode: true,
Expand Down
9 changes: 4 additions & 5 deletions lib/private/Files/Mount/MountPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCP\Files\Storage\IStorage;
use OCP\Files\Storage\IStorageFactory;
use OCP\Server;
use Override;
use Psr\Log\LoggerInterface;

class MountPoint implements IMountPoint {
Expand Down Expand Up @@ -212,13 +213,11 @@ private function formatPath($path) {
return $path;
}

/**
* @param callable $wrapper
*/
public function wrapStorage($wrapper) {
#[Override]
public function wrapStorage($wrapper): void {
$storage = $this->getStorage();
// storage can be null if it couldn't be initialized
if ($storage != null) {
if ($storage !== null) {
$this->storage = $wrapper($this->mountPoint, $storage, $this);
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/private/Files/Node/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ public function getFirstNodeByIdInPath(int $id, string $path): ?INode {
}
}
}

$node = current($this->getByIdInPath($id, $path));
if (!$node) {
return null;
Expand Down
30 changes: 14 additions & 16 deletions lib/private/Files/Storage/StorageFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
Expand All @@ -12,14 +14,14 @@
use OCP\Files\Storage\IStorage;
use OCP\Files\Storage\IStorageFactory;
use OCP\Server;
use Override;
use Psr\Log\LoggerInterface;

class StorageFactory implements IStorageFactory {
/**
* @var array[] [$name=>['priority'=>$priority, 'wrapper'=>$callable] $storageWrappers
*/
private $storageWrappers = [];
/** @var array<non-empty-string, array{priority: int, wrapper: callable(string, IStorage, IMountPoint):IStorage}> */
private array $storageWrappers = [];

#[Override]
public function addStorageWrapper(string $wrapperName, callable $callback, int $priority = 50, array $existingMounts = []): bool {
if (isset($this->storageWrappers[$wrapperName])) {
return false;
Expand All @@ -44,31 +46,27 @@ public function removeStorageWrapper(string $wrapperName): void {
unset($this->storageWrappers[$wrapperName]);
}

/**
* Create an instance of a storage and apply the registered storage wrappers
*/
#[Override]
public function getInstance(IMountPoint $mountPoint, string $class, array $arguments): IStorage {
if (!is_a($class, IConstructableStorage::class, true)) {
Server::get(LoggerInterface::class)->warning('Building a storage not implementing IConstructableStorage is deprecated since 31.0.0', ['class' => $class]);
}

return $this->wrap($mountPoint, new $class($arguments));
}

public function wrap(IMountPoint $mountPoint, IStorage $storage): IStorage {
$wrappers = array_values($this->storageWrappers);
usort($wrappers, function ($a, $b) {
return $b['priority'] - $a['priority'];
});
/** @var callable[] $wrappers */
$wrappers = array_map(function ($wrapper) {
return $wrapper['wrapper'];
}, $wrappers);
$wrappers = $this->storageWrappers;
usort($wrappers, static fn (array $a, array $b): int => $b['priority'] - $a['priority']);

$wrappers = array_map(static fn (array $wrapper): callable => $wrapper['wrapper'], $wrappers);
foreach ($wrappers as $wrapper) {
$storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint);
if (!($storage instanceof IStorage)) {
if (!$storage instanceof IStorage) {
throw new \Exception('Invalid result from storage wrapper');
}
}

return $storage;
}
}
2 changes: 1 addition & 1 deletion lib/private/Preview/PreviewMigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function migrateFileId(int $fileId, bool $flatPath): array {
/** @var SimpleFile $previewFile */
$preview = Preview::fromPath($path, $this->mimeTypeDetector);
if ($preview === false) {
$this->logger->error('Unable to import old preview at path.');
$this->logger->error('Unable to import old preview at path: ' . $path);
continue;
}
$preview->generateId();
Expand Down
5 changes: 4 additions & 1 deletion lib/public/Files/Mount/IMountPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
*/
namespace OCP\Files\Mount;

use OCP\Files\Storage\IStorage;

/**
* A storage mounted to folder on the filesystem
*
* @since 8.0.0
*/
interface IMountPoint {
Expand Down Expand Up @@ -64,7 +67,7 @@ public function getInternalPath($path);
/**
* Apply a storage wrapper to the mounted storage
*
* @param callable $wrapper
* @param callable(string, IStorage, IMountPoint):IStorage $wrapper
* @since 8.0.0
*/
public function wrapStorage($wrapper);
Expand Down
19 changes: 13 additions & 6 deletions lib/public/Files/Storage/IStorageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,34 @@
*/
namespace OCP\Files\Storage;

use OCP\AppFramework\Attribute\Consumable;
use OCP\Files\Mount\IMountPoint;

/**
* Creates storage instances and manages and applies storage wrappers
* Creates storage instances and manages and applies storage wrappers.
*
* @since 8.0.0
*/
#[Consumable(since: '8.0.0')]
interface IStorageFactory {
/**
* allow modifier storage behaviour by adding wrappers around storages
* Allow modifier storage behaviour by adding wrappers around storages.
*
* $callback should be a function of type (string $mountPoint, Storage $storage) => Storage
* @param non-empty-string $wrapperName
* @param callable(string, IStorage, IMountPoint):IStorage $callback Callback should be a function of type (string $mountPoint, Storage $storage, IMountPoint) => Storage
* @param list<IMountPoint> $existingMounts
*
* @return bool true if the wrapper was added, false if there was already a wrapper with this
* name registered
* @since 8.0.0
*/
public function addStorageWrapper(string $wrapperName, callable $callback);
public function addStorageWrapper(string $wrapperName, callable $callback, int $priority = 50, array $existingMounts = []): bool;

/**
* @return IStorage
* Create an instance of a storage and apply the registered storage wrappers.
*
* @param class-string<IStorage> $class
* @since 8.0.0
*/
public function getInstance(IMountPoint $mountPoint, string $class, array $arguments);
public function getInstance(IMountPoint $mountPoint, string $class, array $arguments): IStorage;
}
1 change: 1 addition & 0 deletions psalm-strict.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<file name="apps/settings/lib/Service/AuthorizedGroupService.php"/>
<file name="lib/private/Files/Storage/Storage.php"/>
<file name="lib/private/Files/Storage/Wrapper/Wrapper.php"/>
<file name="lib/public/Files/Storage/IStorageFactory.php"/>
<ignoreFiles>
<directory name="apps/**/composer"/>
<directory name="apps/**/tests"/>
Expand Down
Loading