Skip to content

Commit 09940bc

Browse files
committed
List trashbin in DAV
First steps for #1332 * Add a new DAV collection * List all files in the trashbin for this user * Deleting files from trashbin * Get files from trashbin (just read) Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
1 parent 9c64a20 commit 09940bc

File tree

10 files changed

+577
-1
lines changed

10 files changed

+577
-1
lines changed

apps/files_trashbin/appinfo/info.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ This application enables users to restore files that were deleted from the syste
99
To prevent a user from running out of disk space, the Deleted files app will not utilize more than 50% of the currently available free quota for deleted files. If the deleted files exceed this limit, the app deletes the oldest files until it gets below this limit. More information is available in the Deleted Files documentation.
1010

1111
</description>
12-
<version>1.4.0</version>
12+
<version>1.4.1</version>
1313
<licence>agpl</licence>
1414
<author>Bjoern Schiessle</author>
1515
<namespace>Files_Trashbin</namespace>
1616
<default_enable/>
1717
<types>
1818
<filesystem/>
19+
<dav/>
1920
</types>
2021
<documentation>
2122
<user>user-trashbin</user>
@@ -34,4 +35,10 @@ To prevent a user from running out of disk space, the Deleted files app will not
3435
<command>OCA\Files_Trashbin\Command\CleanUp</command>
3536
<command>OCA\Files_Trashbin\Command\ExpireTrash</command>
3637
</commands>
38+
39+
<sabre>
40+
<collections>
41+
<collection>OCA\Files_Trashbin\Sabre\RootCollection</collection>
42+
</collections>
43+
</sabre>
3744
</info>

apps/files_trashbin/composer/composer/autoload_classmap.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
'OCA\\Files_Trashbin\\Expiration' => $baseDir . '/../lib/Expiration.php',
1919
'OCA\\Files_Trashbin\\Helper' => $baseDir . '/../lib/Helper.php',
2020
'OCA\\Files_Trashbin\\Hooks' => $baseDir . '/../lib/Hooks.php',
21+
'OCA\\Files_Trashbin\\Sabre\\RootCollection' => $baseDir . '/../lib/Sabre/RootCollection.php',
22+
'OCA\\Files_Trashbin\\Sabre\\TrashFile' => $baseDir . '/../lib/Sabre/TrashFile.php',
23+
'OCA\\Files_Trashbin\\Sabre\\TrashFolder' => $baseDir . '/../lib/Sabre/TrashFolder.php',
24+
'OCA\\Files_Trashbin\\Sabre\\TrashFolderFile' => $baseDir . '/../lib/Sabre/TrashFolderFile.php',
25+
'OCA\\Files_Trashbin\\Sabre\\TrashFolderFolder' => $baseDir . '/../lib/Sabre/TrashFolderFolder.php',
26+
'OCA\\Files_Trashbin\\Sabre\\TrashHome' => $baseDir . '/../lib/Sabre/TrashHome.php',
2127
'OCA\\Files_Trashbin\\Storage' => $baseDir . '/../lib/Storage.php',
2228
'OCA\\Files_Trashbin\\Trashbin' => $baseDir . '/../lib/Trashbin.php',
2329
);

apps/files_trashbin/composer/composer/autoload_static.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class ComposerStaticInitFiles_Trashbin
3333
'OCA\\Files_Trashbin\\Expiration' => __DIR__ . '/..' . '/../lib/Expiration.php',
3434
'OCA\\Files_Trashbin\\Helper' => __DIR__ . '/..' . '/../lib/Helper.php',
3535
'OCA\\Files_Trashbin\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php',
36+
'OCA\\Files_Trashbin\\Sabre\\RootCollection' => __DIR__ . '/..' . '/../lib/Sabre/RootCollection.php',
37+
'OCA\\Files_Trashbin\\Sabre\\TrashFile' => __DIR__ . '/..' . '/../lib/Sabre/TrashFile.php',
38+
'OCA\\Files_Trashbin\\Sabre\\TrashFolder' => __DIR__ . '/..' . '/../lib/Sabre/TrashFolder.php',
39+
'OCA\\Files_Trashbin\\Sabre\\TrashFolderFile' => __DIR__ . '/..' . '/../lib/Sabre/TrashFolderFile.php',
40+
'OCA\\Files_Trashbin\\Sabre\\TrashFolderFolder' => __DIR__ . '/..' . '/../lib/Sabre/TrashFolderFolder.php',
41+
'OCA\\Files_Trashbin\\Sabre\\TrashHome' => __DIR__ . '/..' . '/../lib/Sabre/TrashHome.php',
3642
'OCA\\Files_Trashbin\\Storage' => __DIR__ . '/..' . '/../lib/Storage.php',
3743
'OCA\\Files_Trashbin\\Trashbin' => __DIR__ . '/..' . '/../lib/Trashbin.php',
3844
);

apps/files_trashbin/lib/AppInfo/Application.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace OCA\Files_Trashbin\AppInfo;
2525

26+
use OCA\DAV\Connector\Sabre\Principal;
2627
use OCP\AppFramework\App;
2728
use OCA\Files_Trashbin\Expiration;
2829
use OCP\AppFramework\Utility\ITimeFactory;
@@ -47,5 +48,17 @@ public function __construct (array $urlParams = []) {
4748
$c->query(ITimeFactory::class)
4849
);
4950
});
51+
52+
/*
53+
* Register $principalBackend for the DAV collection
54+
*/
55+
$container->registerService('principalBackend', function () {
56+
return new Principal(
57+
\OC::$server->getUserManager(),
58+
\OC::$server->getGroupManager(),
59+
\OC::$server->getShareManager(),
60+
\OC::$server->getUserSession()
61+
);
62+
});
5063
}
5164
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
4+
*
5+
* @author Roeland Jago Douma <roeland@famdouma.nl>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
namespace OCA\Files_Trashbin\Sabre;
24+
25+
use Sabre\DAV\INode;
26+
use Sabre\DAVACL\AbstractPrincipalCollection;
27+
use Sabre\DAVACL\PrincipalBackend;
28+
29+
class RootCollection extends AbstractPrincipalCollection {
30+
31+
public function __construct(PrincipalBackend\BackendInterface $principalBackend) {
32+
parent::__construct($principalBackend, 'principals/users');
33+
}
34+
35+
/**
36+
* This method returns a node for a principal.
37+
*
38+
* The passed array contains principal information, and is guaranteed to
39+
* at least contain a uri item. Other properties may or may not be
40+
* supplied by the authentication backend.
41+
*
42+
* @param array $principalInfo
43+
* @return INode
44+
*/
45+
public function getChildForPrincipal(array $principalInfo) {
46+
list(,$name) = \Sabre\Uri\split($principalInfo['uri']);
47+
$user = \OC::$server->getUserSession()->getUser();
48+
if (is_null($user) || $name !== $user->getUID()) {
49+
throw new \Sabre\DAV\Exception\Forbidden();
50+
}
51+
return new TrashHome($principalInfo);
52+
}
53+
54+
public function getName() {
55+
return 'trashbin';
56+
}
57+
58+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
4+
*
5+
* @author Roeland Jago Douma <roeland@famdouma.nl>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
namespace OCA\Files_Trashbin\Sabre;
24+
25+
use OCP\Files\FileInfo;
26+
use Sabre\DAV\Exception\Forbidden;
27+
use Sabre\DAV\IFile;
28+
29+
class TrashFile implements IFile {
30+
/** @var string */
31+
private $userId;
32+
33+
/** @var FileInfo */
34+
private $data;
35+
36+
public function __construct(string $userId, FileInfo $data) {
37+
$this->userId = $userId;
38+
$this->data = $data;
39+
}
40+
41+
public function put($data) {
42+
throw new Forbidden();
43+
}
44+
45+
public function get() {
46+
return $this->data->getStorage()->fopen($this->data->getInternalPath().'.d'.$this->getLastModified(), 'rb');
47+
}
48+
49+
public function getContentType() {
50+
return $this->data->getMimetype();
51+
}
52+
53+
public function getETag() {
54+
return $this->data->getEtag();
55+
}
56+
57+
public function getSize() {
58+
return $this->data->getSize();
59+
}
60+
61+
public function delete() {
62+
\OCA\Files_Trashbin\Trashbin::delete($this->data->getName(), $this->userId, $this->getLastModified());
63+
}
64+
65+
public function getName() {
66+
return $this->data->getName() . '.d' . $this->getLastModified();
67+
}
68+
69+
public function setName($name) {
70+
throw new Forbidden();
71+
}
72+
73+
public function getLastModified() {
74+
return $this->data->getMtime();
75+
}
76+
77+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
4+
*
5+
* @author Roeland Jago Douma <roeland@famdouma.nl>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
namespace OCA\Files_Trashbin\Sabre;
24+
25+
use OCP\Files\FileInfo;
26+
use Sabre\DAV\Exception\Forbidden;
27+
use Sabre\DAV\ICollection;
28+
29+
class TrashFolder implements ICollection {
30+
/** @var string */
31+
private $userId;
32+
33+
/** @var FileInfo */
34+
private $data;
35+
36+
public function __construct(string $root, string $userId, FileInfo $data) {
37+
$this->userId = $userId;
38+
$this->data = $data;
39+
}
40+
41+
public function createFile($name, $data = null) {
42+
throw new Forbidden();
43+
}
44+
45+
public function createDirectory($name) {
46+
throw new Forbidden();
47+
}
48+
49+
public function getChild($name) {
50+
$entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->getName(), $this->userId);
51+
52+
foreach ($entries as $entry) {
53+
if ($entry->getName() === $name) {
54+
if ($entry->getMimetype() === 'httpd/unix-directory') {
55+
return new TrashFolderFolder($this->getName(), $this->userId, $entry);
56+
}
57+
return new TrashFolderFile($this->getName(), $this->userId, $entry);
58+
}
59+
}
60+
}
61+
62+
public function getChildren() {
63+
$entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->getName(), $this->userId);
64+
65+
$children = array_map(function (FileInfo $entry) {
66+
if ($entry->getMimetype() === 'httpd/unix-directory') {
67+
return new TrashFolderFolder($this->getName(), $this->userId, $entry);
68+
}
69+
return new TrashFolderFile($this->getName(), $this->userId, $entry);
70+
}, $entries);
71+
72+
return $children;
73+
}
74+
75+
public function childExists($name) {
76+
$entries = \OCA\Files_Trashbin\Helper::getTrashFiles($this->getName(), $this->userId);
77+
78+
foreach ($entries as $entry) {
79+
if ($entry->getName() === $name) {
80+
return true;
81+
}
82+
}
83+
84+
return false;
85+
}
86+
87+
public function delete() {
88+
\OCA\Files_Trashbin\Trashbin::delete($this->data->getName(), $this->userId, $this->getLastModified());
89+
}
90+
91+
public function getName() {
92+
return $this->data->getName() . '.d' . $this->getLastModified();
93+
}
94+
95+
public function setName($name) {
96+
throw new Forbidden();
97+
}
98+
99+
public function getLastModified() {
100+
return $this->data->getMtime();
101+
}
102+
}

0 commit comments

Comments
 (0)