Skip to content

Commit 4adac44

Browse files
committed
fix select statement
fix select statement Make trashbin api modules Apps can register trashbin backends for specific storages, allowing them to modify trashbin behavior for storages The old trashbin implementation has been wrapped in a "legacy" backend, for future work this can be replaced with a new backend that better handles shares while still keeping the legacy backend around to keep existing trash from being accessible Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 2634ceb commit 4adac44

24 files changed

Lines changed: 952 additions & 435 deletions

apps/files_trashbin/appinfo/info.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@ To prevent a user from running out of disk space, the Deleted files app will not
4444
<plugin>OCA\Files_Trashbin\Sabre\PropfindPlugin</plugin>
4545
</plugins>
4646
</sabre>
47+
48+
<trash>
49+
<backend for="OCP\Files\Storage\IStorage">OCA\Files_Trashbin\Trash\LegacyTrashBackend</backend>
50+
</trash>
4751
</info>

apps/files_trashbin/composer/composer/autoload_classmap.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
'OCA\\Files_Trashbin\\Helper' => $baseDir . '/../lib/Helper.php',
2020
'OCA\\Files_Trashbin\\Hooks' => $baseDir . '/../lib/Hooks.php',
2121
'OCA\\Files_Trashbin\\Sabre\\AbstractTrash' => $baseDir . '/../lib/Sabre/AbstractTrash.php',
22+
'OCA\\Files_Trashbin\\Sabre\\AbstractTrashFile' => $baseDir . '/../lib/Sabre/AbstractTrashFile.php',
23+
'OCA\\Files_Trashbin\\Sabre\\AbstractTrashFolder' => $baseDir . '/../lib/Sabre/AbstractTrashFolder.php',
2224
'OCA\\Files_Trashbin\\Sabre\\ITrash' => $baseDir . '/../lib/Sabre/ITrash.php',
2325
'OCA\\Files_Trashbin\\Sabre\\PropfindPlugin' => $baseDir . '/../lib/Sabre/PropfindPlugin.php',
2426
'OCA\\Files_Trashbin\\Sabre\\RestoreFolder' => $baseDir . '/../lib/Sabre/RestoreFolder.php',
@@ -30,5 +32,12 @@
3032
'OCA\\Files_Trashbin\\Sabre\\TrashHome' => $baseDir . '/../lib/Sabre/TrashHome.php',
3133
'OCA\\Files_Trashbin\\Sabre\\TrashRoot' => $baseDir . '/../lib/Sabre/TrashRoot.php',
3234
'OCA\\Files_Trashbin\\Storage' => $baseDir . '/../lib/Storage.php',
35+
'OCA\\Files_Trashbin\\Trash\\BackendNotFoundException' => $baseDir . '/../lib/Trash/BackendNotFoundException.php',
36+
'OCA\\Files_Trashbin\\Trash\\ITrashBackend' => $baseDir . '/../lib/Trash/ITrashBackend.php',
37+
'OCA\\Files_Trashbin\\Trash\\ITrashItem' => $baseDir . '/../lib/Trash/ITrashItem.php',
38+
'OCA\\Files_Trashbin\\Trash\\ITrashManager' => $baseDir . '/../lib/Trash/ITrashManager.php',
39+
'OCA\\Files_Trashbin\\Trash\\LegacyTrashBackend' => $baseDir . '/../lib/Trash/LegacyTrashBackend.php',
40+
'OCA\\Files_Trashbin\\Trash\\TrashItem' => $baseDir . '/../lib/Trash/TrashItem.php',
41+
'OCA\\Files_Trashbin\\Trash\\TrashManager' => $baseDir . '/../lib/Trash/TrashManager.php',
3342
'OCA\\Files_Trashbin\\Trashbin' => $baseDir . '/../lib/Trashbin.php',
3443
);

apps/files_trashbin/composer/composer/autoload_static.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class ComposerStaticInitFiles_Trashbin
3434
'OCA\\Files_Trashbin\\Helper' => __DIR__ . '/..' . '/../lib/Helper.php',
3535
'OCA\\Files_Trashbin\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php',
3636
'OCA\\Files_Trashbin\\Sabre\\AbstractTrash' => __DIR__ . '/..' . '/../lib/Sabre/AbstractTrash.php',
37+
'OCA\\Files_Trashbin\\Sabre\\AbstractTrashFile' => __DIR__ . '/..' . '/../lib/Sabre/AbstractTrashFile.php',
38+
'OCA\\Files_Trashbin\\Sabre\\AbstractTrashFolder' => __DIR__ . '/..' . '/../lib/Sabre/AbstractTrashFolder.php',
3739
'OCA\\Files_Trashbin\\Sabre\\ITrash' => __DIR__ . '/..' . '/../lib/Sabre/ITrash.php',
3840
'OCA\\Files_Trashbin\\Sabre\\PropfindPlugin' => __DIR__ . '/..' . '/../lib/Sabre/PropfindPlugin.php',
3941
'OCA\\Files_Trashbin\\Sabre\\RestoreFolder' => __DIR__ . '/..' . '/../lib/Sabre/RestoreFolder.php',
@@ -45,6 +47,13 @@ class ComposerStaticInitFiles_Trashbin
4547
'OCA\\Files_Trashbin\\Sabre\\TrashHome' => __DIR__ . '/..' . '/../lib/Sabre/TrashHome.php',
4648
'OCA\\Files_Trashbin\\Sabre\\TrashRoot' => __DIR__ . '/..' . '/../lib/Sabre/TrashRoot.php',
4749
'OCA\\Files_Trashbin\\Storage' => __DIR__ . '/..' . '/../lib/Storage.php',
50+
'OCA\\Files_Trashbin\\Trash\\BackendNotFoundException' => __DIR__ . '/..' . '/../lib/Trash/BackendNotFoundException.php',
51+
'OCA\\Files_Trashbin\\Trash\\ITrashBackend' => __DIR__ . '/..' . '/../lib/Trash/ITrashBackend.php',
52+
'OCA\\Files_Trashbin\\Trash\\ITrashItem' => __DIR__ . '/..' . '/../lib/Trash/ITrashItem.php',
53+
'OCA\\Files_Trashbin\\Trash\\ITrashManager' => __DIR__ . '/..' . '/../lib/Trash/ITrashManager.php',
54+
'OCA\\Files_Trashbin\\Trash\\LegacyTrashBackend' => __DIR__ . '/..' . '/../lib/Trash/LegacyTrashBackend.php',
55+
'OCA\\Files_Trashbin\\Trash\\TrashItem' => __DIR__ . '/..' . '/../lib/Trash/TrashItem.php',
56+
'OCA\\Files_Trashbin\\Trash\\TrashManager' => __DIR__ . '/..' . '/../lib/Trash/TrashManager.php',
4857
'OCA\\Files_Trashbin\\Trashbin' => __DIR__ . '/..' . '/../lib/Trashbin.php',
4958
);
5059

apps/files_trashbin/lib/AppInfo/Application.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
namespace OCA\Files_Trashbin\AppInfo;
2525

2626
use OCA\DAV\Connector\Sabre\Principal;
27+
use OCA\Files_Trashbin\Trash\ITrashManager;
28+
use OCA\Files_Trashbin\Trash\TrashManager;
2729
use OCP\AppFramework\App;
2830
use OCA\Files_Trashbin\Expiration;
31+
use OCP\AppFramework\IAppContainer;
2932
use OCP\AppFramework\Utility\ITimeFactory;
3033
use OCA\Files_Trashbin\Capabilities;
3134

@@ -61,5 +64,36 @@ public function __construct (array $urlParams = []) {
6164
\OC::$server->getConfig()
6265
);
6366
});
67+
68+
$container->registerService(ITrashManager::class, function(IAppContainer $c) {
69+
return new TrashManager();
70+
});
71+
72+
$this->registerTrashBackends();
73+
}
74+
75+
public function registerTrashBackends() {
76+
$server = $this->getContainer()->getServer();
77+
$logger = $server->getLogger();
78+
$appManager = $server->getAppManager();
79+
/** @var ITrashManager $trashManager */
80+
$trashManager = $this->getContainer()->getServer()->query(ITrashManager::class);
81+
foreach($appManager->getInstalledApps() as $app) {
82+
$appInfo = $appManager->getAppInfo($app);
83+
if (isset($appInfo['trash'])) {
84+
$backends = $appInfo['trash'];
85+
foreach($backends as $backend) {
86+
$class = $backend['@value'];
87+
$for = $backend['@attributes']['for'];
88+
89+
try {
90+
$backendObject = $server->query($class);
91+
$trashManager->registerBackend($for, $backendObject);
92+
} catch (\Exception $e) {
93+
$logger->logException($e);
94+
}
95+
}
96+
}
97+
}
6498
}
6599
}

apps/files_trashbin/lib/Sabre/AbstractTrash.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
45
*
@@ -21,13 +22,24 @@
2122

2223
namespace OCA\Files_Trashbin\Sabre;
2324

25+
use OCA\Files_Trashbin\Trash\ITrashItem;
26+
use OCA\Files_Trashbin\Trash\ITrashManager;
2427
use OCP\Files\FileInfo;
28+
use OCP\IUser;
2529

2630
abstract class AbstractTrash implements ITrash {
27-
/** @var FileInfo */
31+
/** @var ITrashItem */
2832
protected $data;
2933

30-
public function __construct(FileInfo $data) {
34+
/** @var ITrashManager */
35+
protected $trashManager;
36+
37+
/** @var IUser */
38+
protected $user;
39+
40+
public function __construct(ITrashManager $trashManager, IUser $user, ITrashItem $data) {
41+
$this->trashManager = $trashManager;
42+
$this->user = $user;
3143
$this->data = $data;
3244
}
3345

@@ -36,7 +48,7 @@ public function getFilename(): string {
3648
}
3749

3850
public function getDeletionTime(): int {
39-
return $this->data->getMtime();
51+
return $this->data->getDeletedTime();
4052
}
4153

4254
public function getFileId(): int {
@@ -66,4 +78,17 @@ public function getETag(): string {
6678
public function getName(): string {
6779
return $this->data->getName();
6880
}
81+
82+
public function getOriginalLocation(): string {
83+
return $this->data->getOriginalLocation($this->user);
84+
}
85+
86+
public function delete() {
87+
$this->trashManager->removeItem($this->user, $this->data);
88+
}
89+
90+
public function restore(): bool {
91+
$this->trashManager->restoreItem($this->data);
92+
return true;
93+
}
6994
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
namespace OCA\Files_Trashbin\Sabre;
24+
25+
use Sabre\DAV\Exception\Forbidden;
26+
use Sabre\DAV\IFile;
27+
28+
abstract class AbstractTrashFile extends AbstractTrash implements IFile , ITrash{
29+
public function put($data) {
30+
throw new Forbidden();
31+
}
32+
33+
public function setName($name) {
34+
throw new Forbidden();
35+
}
36+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
namespace OCA\Files_Trashbin\Sabre;
24+
25+
use OCA\Files_Trashbin\Trash\ITrashItem;
26+
use OCP\Files\FileInfo;
27+
use Sabre\DAV\Exception\Forbidden;
28+
use Sabre\DAV\Exception\NotFound;
29+
use Sabre\DAV\ICollection;
30+
31+
abstract class AbstractTrashFolder extends AbstractTrash implements ICollection, ITrash {
32+
public function getChildren(): array {
33+
$entries = $this->trashManager->listTrashFolder($this->user, $this->data);
34+
35+
$children = array_map(function (ITrashItem $entry) {
36+
if ($entry->getType() === FileInfo::TYPE_FOLDER) {
37+
return new TrashFolderFolder($this->trashManager, $this->user, $entry);
38+
}
39+
return new TrashFolderFile($this->trashManager, $this->user, $entry);
40+
}, $entries);
41+
42+
return $children;
43+
}
44+
45+
public function getChild($name): ITrash {
46+
$entries = $this->getChildren();
47+
48+
foreach ($entries as $entry) {
49+
if ($entry->getName() === $name) {
50+
return $entry;
51+
}
52+
}
53+
54+
throw new NotFound();
55+
}
56+
57+
public function childExists($name): bool {
58+
try {
59+
$this->getChild($name);
60+
return true;
61+
} catch (NotFound $e) {
62+
return false;
63+
}
64+
}
65+
66+
public function setName($name) {
67+
throw new Forbidden();
68+
}
69+
70+
public function createFile($name, $data = null) {
71+
throw new Forbidden();
72+
}
73+
74+
public function createDirectory($name) {
75+
throw new Forbidden();
76+
}
77+
}

apps/files_trashbin/lib/Sabre/RootCollection.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,27 @@
2121
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
*
2323
*/
24+
2425
namespace OCA\Files_Trashbin\Sabre;
2526

27+
use OCA\Files_Trashbin\Trash\ITrashManager;
2628
use OCP\IConfig;
2729
use Sabre\DAV\INode;
2830
use Sabre\DAVACL\AbstractPrincipalCollection;
2931
use Sabre\DAVACL\PrincipalBackend;
3032

3133
class RootCollection extends AbstractPrincipalCollection {
34+
/** @var ITrashManager */
35+
private $trashManager;
3236

33-
public function __construct(PrincipalBackend\BackendInterface $principalBackend, IConfig $config) {
37+
public function __construct(
38+
ITrashManager $trashManager,
39+
PrincipalBackend\BackendInterface $principalBackend,
40+
IConfig $config
41+
) {
3442
parent::__construct($principalBackend, 'principals/users');
3543

44+
$this->trashManager = $trashManager;
3645
$this->disableListing = !$config->getSystemValue('debug', false);
3746
}
3847

@@ -47,12 +56,12 @@ public function __construct(PrincipalBackend\BackendInterface $principalBackend,
4756
* @return INode
4857
*/
4958
public function getChildForPrincipal(array $principalInfo): TrashHome {
50-
list(,$name) = \Sabre\Uri\split($principalInfo['uri']);
59+
list(, $name) = \Sabre\Uri\split($principalInfo['uri']);
5160
$user = \OC::$server->getUserSession()->getUser();
5261
if (is_null($user) || $name !== $user->getUID()) {
5362
throw new \Sabre\DAV\Exception\Forbidden();
5463
}
55-
return new TrashHome($principalInfo);
64+
return new TrashHome($principalInfo, $this->trashManager, $user);
5665
}
5766

5867
public function getName(): string {

apps/files_trashbin/lib/Sabre/TrashFile.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,15 @@
2121
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
*
2323
*/
24-
namespace OCA\Files_Trashbin\Sabre;
25-
26-
use OCP\Files\FileInfo;
27-
use Sabre\DAV\Exception\Forbidden;
28-
use Sabre\DAV\IFile;
29-
30-
class TrashFile extends AbstractTrash implements IFile, ITrash {
31-
/** @var string */
32-
private $userId;
33-
34-
public function __construct(string $userId, FileInfo $data) {
35-
$this->userId = $userId;
36-
parent::__construct($data);
37-
}
3824

39-
public function put($data) {
40-
throw new Forbidden();
41-
}
25+
namespace OCA\Files_Trashbin\Sabre;
4226

27+
class TrashFile extends AbstractTrashFile {
4328
public function get() {
44-
return $this->data->getStorage()->fopen($this->data->getInternalPath().'.d'.$this->getLastModified(), 'rb');
45-
}
46-
47-
public function delete() {
48-
\OCA\Files_Trashbin\Trashbin::delete($this->data->getName(), $this->userId, $this->getLastModified());
29+
return $this->data->getStorage()->fopen($this->data->getInternalPath() . '.d' . $this->getLastModified(), 'rb');
4930
}
5031

5132
public function getName(): string {
5233
return $this->data->getName() . '.d' . $this->getLastModified();
5334
}
54-
55-
public function setName($name) {
56-
throw new Forbidden();
57-
}
58-
59-
public function restore(): bool {
60-
return \OCA\Files_Trashbin\Trashbin::restore($this->getName(), $this->data->getName(), $this->getLastModified());
61-
}
62-
63-
public function getOriginalLocation(): string {
64-
return $this->data['extraData'];
65-
}
6635
}

0 commit comments

Comments
 (0)