Skip to content

Commit

Permalink
feat: Make ISharedStorage public API and reuse where possible
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Aug 11, 2024
1 parent 66950c0 commit 16498af
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 28 deletions.
27 changes: 14 additions & 13 deletions apps/dav/lib/Connector/Sabre/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\ISharedStorage;
use OCP\Files\StorageNotAvailableException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
Expand Down Expand Up @@ -262,8 +263,8 @@ public function getSharePermissions($user) {
$storage = null;
}

if ($storage && $storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
/** @var \OCA\Files_Sharing\SharedStorage $storage */
if ($storage && $storage->instanceOfStorage(ISharedStorage::class)) {
/** @var ISharedStorage $storage */
$permissions = (int)$storage->getShare()->getPermissions();
} else {
$permissions = $this->info->getPermissions();
Expand Down Expand Up @@ -306,8 +307,8 @@ public function getShareAttributes(): array {
}

$attributes = [];
if (method_exists($storage, 'getShare')) {
/** @var \OCA\Files_Sharing\SharedStorage $storage */
if ($storage->instanceOfStorage(ISharedStorage::class)) {
/** @var ISharedStorage $storage */
$attributes = $storage->getShare()->getAttributes();
if ($attributes === null) {
return [];
Expand All @@ -326,17 +327,17 @@ public function getNoteFromShare(?string $user): string {
return '';
}

if (!method_exists($storage, 'getShare')) {
return '';
$note = '';
if ($storage->instanceOfStorage(ISharedStorage::class)) {
/** @var ISharedStorage $storage */
$share = $storage->getShare();
$note = $share->getNote();
if ($user === $share->getShareOwner()) {
// Note is only for recipient not the owner
return '';
}
}
/** @var \OCA\Files_Sharing\SharedStorage $storage */

$share = $storage->getShare();
$note = $share->getNote();
if ($user === $share->getShareOwner()) {
// Note is only for recipient not the owner
return '';
}
return $note;
}

Expand Down
5 changes: 3 additions & 2 deletions apps/dav/lib/DAV/ViewOnlyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCA\Files_Versions\Sabre\VersionFile;
use OCP\Files\Folder;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\ISharedStorage;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
Expand Down Expand Up @@ -81,11 +82,11 @@ public function checkViewOnly(RequestInterface $request): bool {

$storage = $node->getStorage();

if (!$storage->instanceOfStorage(\OCA\Files_Sharing\SharedStorage::class)) {
if (!$storage->instanceOfStorage(ISharedStorage::class)) {
return true;
}
// Extract extra permissions
/** @var \OCA\Files_Sharing\SharedStorage $storage */
/** @var ISharedStorage $storage */
$share = $storage->getShare();

$attributes = $share->getAttributes();
Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/Storage/PublicShareWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace OCA\DAV\Storage;

use OC\Files\Storage\Wrapper\Wrapper;
use OCP\Files\Storage\ISharedStorage;
use OCP\Share\IShare;

class PublicShareWrapper extends Wrapper {

Check notice

Code scanning / Psalm

DeprecatedInterface Note

OCP\Files\Storage is marked deprecated
Expand All @@ -29,7 +30,7 @@ public function __construct($arguments) {
public function getShare(): IShare {
$storage = parent::getWrapperStorage();
if (method_exists($storage, 'getShare')) {
/** @var \OCA\Files_Sharing\SharedStorage $storage */
/** @var ISharedStorage $storage */
return $storage->getShare();
}

Expand Down
14 changes: 10 additions & 4 deletions apps/dav/tests/unit/Connector/Sabre/NodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use OC\Files\FileInfo;
use OC\Files\Mount\MountPoint;
use OC\Files\Node\Folder;
use OC\Files\View;
use OC\Share20\ShareAttributes;
use OCA\Files_Sharing\SharedMount;
Expand All @@ -21,6 +22,7 @@
use OCP\ICache;
use OCP\Share\IManager;
use OCP\Share\IShare;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Class NodeTest
Expand Down Expand Up @@ -201,14 +203,16 @@ public function testShareAttributes(): void {

$share->expects($this->once())->method('getAttributes')->willReturn($attributes);

$info = $this->getMockBuilder(FileInfo::class)
/** @var Folder&MockObject $info */
$info = $this->getMockBuilder(Folder::class)
->disableOriginalConstructor()
->setMethods(['getStorage', 'getType'])
->onlyMethods(['getStorage', 'getType'])
->getMock();

$info->method('getStorage')->willReturn($storage);
$info->method('getType')->willReturn(FileInfo::TYPE_FOLDER);

/** @var View&MockObject $view */
$view = $this->getMockBuilder(View::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -225,14 +229,16 @@ public function testShareAttributesNonShare(): void {

$shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();

$info = $this->getMockBuilder(FileInfo::class)
/** @var Folder&MockObject */
$info = $this->getMockBuilder(Folder::class)
->disableOriginalConstructor()
->setMethods(['getStorage', 'getType'])
->onlyMethods(['getStorage', 'getType'])
->getMock();

$info->method('getStorage')->willReturn($storage);
$info->method('getType')->willReturn(FileInfo::TYPE_FOLDER);

/** @var View&MockObject */
$view = $this->getMockBuilder(View::class)
->disableOriginalConstructor()
->getMock();
Expand Down
5 changes: 3 additions & 2 deletions apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCA\Files_Versions\Versions\IVersion;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\Storage\ISharedStorage;
use OCP\Files\Storage\IStorage;
use OCP\IUser;
use OCP\Share\IAttributes;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function testCanGetNonShared(): void {

$storage = $this->createMock(IStorage::class);
$file->method('getStorage')->willReturn($storage);
$storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false);
$storage->method('instanceOfStorage')->with(ISharedStorage::class)->willReturn(false);

$this->assertTrue($this->plugin->checkViewOnly($this->request));
}
Expand Down Expand Up @@ -140,7 +141,7 @@ public function testCanGet(bool $isVersion, ?bool $attrEnabled, bool $expectCanD
$nodeInfo->expects($this->once())
->method('getStorage')
->willReturn($storage);
$storage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(true);
$storage->method('instanceOfStorage')->with(ISharedStorage::class)->willReturn(true);
$storage->method('getShare')->willReturn($share);

$extAttr = $this->createMock(IAttributes::class);
Expand Down
3 changes: 3 additions & 0 deletions apps/files_sharing/lib/ISharedStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@

use OCP\Files\Storage\IStorage;

/**
* @deprecated 30.0.0 use `\OCP\Files\Storage\ISharedStorage` instead
*/
interface ISharedStorage extends IStorage {
}
5 changes: 3 additions & 2 deletions apps/files_sharing/lib/SharedStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
use OC\Files\Storage\Wrapper\Wrapper;
use OC\User\NoUserException;
use OCA\Files_External\Config\ConfigAdapter;
use OCA\Files_Sharing\ISharedStorage as LegacyISharedStorage;
use OCP\Constants;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\Folder;
use OCP\Files\IHomeStorage;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IDisableEncryptionStorage;
use OCP\Files\Storage\ISharedStorage;
use OCP\Files\Storage\IStorage;
use OCP\Lock\ILockingProvider;
use OCP\Share\IShare;
Expand All @@ -35,7 +36,7 @@
/**
* Convert target path to source path and pass the function call to the correct storage provider
*/
class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage, IDisableEncryptionStorage {
class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements LegacyISharedStorage, ISharedStorage, IDisableEncryptionStorage {

Check notice

Code scanning / Psalm

DeprecatedInterface Note

OCA\Files_Sharing\ISharedStorage is marked deprecated

Check notice

Code scanning / Psalm

DeprecatedInterface Note

OCP\Files\Storage is marked deprecated
/** @var \OCP\Share\IShare */
private $superShare;

Expand Down
7 changes: 3 additions & 4 deletions apps/files_versions/lib/Versions/LegacyVersionsBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use Exception;
use OC\Files\View;
use OCA\DAV\Connector\Sabre\Exception\Forbidden;
use OCA\Files_Sharing\ISharedStorage;
use OCA\Files_Sharing\SharedStorage;
use OCA\Files_Versions\Db\VersionEntity;
use OCA\Files_Versions\Db\VersionsMapper;
use OCA\Files_Versions\Storage;
Expand All @@ -24,6 +22,7 @@
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\ISharedStorage;
use OCP\Files\Storage\IStorage;
use OCP\IUser;
use OCP\IUserManager;
Expand All @@ -48,7 +47,7 @@ public function useBackendForStorage(IStorage $storage): bool {
public function getVersionsForFile(IUser $user, FileInfo $file): array {
$storage = $file->getStorage();

if ($storage->instanceOfStorage(SharedStorage::class)) {
if ($storage->instanceOfStorage(ISharedStorage::class)) {
$owner = $storage->getOwner('');
$user = $this->userManager->get($owner);

Expand Down Expand Up @@ -192,7 +191,7 @@ public function getVersionFile(IUser $user, FileInfo $sourceFile, $revision): Fi

// Shared files have their versions in the owners root folder so we need to obtain them from there
if ($storage->instanceOfStorage(ISharedStorage::class) && $owner) {
/** @var SharedStorage $storage */
/** @var ISharedStorage $storage */
$userFolder = $this->rootFolder->getUserFolder($owner->getUID());
$user = $owner;
$ownerPathInStorage = $sourceFile->getInternalPath();
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@
'OCP\\Files\\Storage\\ILockingStorage' => $baseDir . '/lib/public/Files/Storage/ILockingStorage.php',
'OCP\\Files\\Storage\\INotifyStorage' => $baseDir . '/lib/public/Files/Storage/INotifyStorage.php',
'OCP\\Files\\Storage\\IReliableEtagStorage' => $baseDir . '/lib/public/Files/Storage/IReliableEtagStorage.php',
'OCP\\Files\\Storage\\ISharedStorage' => $baseDir . '/lib/public/Files/Storage/ISharedStorage.php',
'OCP\\Files\\Storage\\IStorage' => $baseDir . '/lib/public/Files/Storage/IStorage.php',
'OCP\\Files\\Storage\\IStorageFactory' => $baseDir . '/lib/public/Files/Storage/IStorageFactory.php',
'OCP\\Files\\Storage\\IWriteStreamStorage' => $baseDir . '/lib/public/Files/Storage/IWriteStreamStorage.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Files\\Storage\\ILockingStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/ILockingStorage.php',
'OCP\\Files\\Storage\\INotifyStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/INotifyStorage.php',
'OCP\\Files\\Storage\\IReliableEtagStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/IReliableEtagStorage.php',
'OCP\\Files\\Storage\\ISharedStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/ISharedStorage.php',
'OCP\\Files\\Storage\\IStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/IStorage.php',
'OCP\\Files\\Storage\\IStorageFactory' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/IStorageFactory.php',
'OCP\\Files\\Storage\\IWriteStreamStorage' => __DIR__ . '/../../..' . '/lib/public/Files/Storage/IWriteStreamStorage.php',
Expand Down
26 changes: 26 additions & 0 deletions lib/public/Files/Storage/ISharedStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\Files\Storage;

use OCP\Share\IShare;

/**
* Interface for a storage that is based on a file share
*
* @since 30.0.0
*/
interface ISharedStorage extends IStorage {
/**
* The the associated share
*
* @return IShare
* @since 30.0.0
*/
public function getShare(): IShare;
}

0 comments on commit 16498af

Please sign in to comment.