1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @license GNU AGPL version 3 or any later version |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
10
|
|
|
* License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\Files_Trashbin\Trash; |
23
|
|
|
|
24
|
|
|
use OCP\Files\FileInfo; |
25
|
|
|
use OCP\Files\Mount\IMountPoint; |
26
|
|
|
use OCP\Files\Storage\IStorage; |
27
|
|
|
use OCP\IUser; |
28
|
|
|
|
29
|
|
|
class TrashManager implements ITrashManager { |
30
|
|
|
/** @var ITrashBackend[] */ |
31
|
|
|
private $backends = []; |
32
|
|
|
|
33
|
|
|
private $trashPaused = false; |
34
|
|
|
|
35
|
|
|
public function registerBackend(string $storageType, ITrashBackend $backend) { |
36
|
|
|
$this->backends[$storageType] = $backend; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return ITrashBackend[] |
41
|
|
|
*/ |
42
|
|
|
private function getBackends(): array { |
43
|
|
|
return $this->backends; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function listTrashRoot(IUser $user): array { |
47
|
|
|
$items = array_reduce($this->getBackends(), function (array $items, ITrashBackend $backend) use ($user) { |
48
|
|
|
return array_merge($items, $backend->listTrashRoot($user)); |
49
|
|
|
}, []); |
50
|
|
|
usort($items, function (ITrashItem $a, ITrashItem $b) { |
51
|
|
|
return $a->getDeletedTime() - $b->getDeletedTime(); |
52
|
|
|
}); |
53
|
|
|
return $items; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function getBackendForItem(ITrashItem $item) { |
57
|
|
|
return $item->getTrashBackend(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function listTrashFolder(ITrashItem $folder): array { |
61
|
|
|
return $this->getBackendForItem($folder)->listTrashFolder($folder); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function restoreItem(ITrashItem $item) { |
65
|
|
|
return $this->getBackendForItem($item)->restoreItem($item); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function removeItem(ITrashItem $item) { |
69
|
|
|
$this->getBackendForItem($item)->removeItem($item); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param IStorage $storage |
74
|
|
|
* @return ITrashBackend |
75
|
|
|
* @throws BackendNotFoundException |
76
|
|
|
*/ |
77
|
|
|
public function getBackendForStorage(IStorage $storage): ITrashBackend { |
78
|
|
|
$fullType = get_class($storage); |
79
|
|
|
$foundType = array_reduce(array_keys($this->backends), function ($type, $registeredType) use ($storage) { |
80
|
|
|
if ( |
81
|
|
|
$storage->instanceOfStorage($registeredType) && |
82
|
|
|
($type === '' || is_subclass_of($registeredType, $type)) |
|
|
|
|
83
|
|
|
) { |
84
|
|
|
return $registeredType; |
85
|
|
|
} else { |
86
|
|
|
return $type; |
87
|
|
|
} |
88
|
|
|
}, ''); |
89
|
|
|
if ($foundType === '') { |
90
|
|
|
throw new BackendNotFoundException("Trash backend for $fullType not found"); |
91
|
|
|
} else { |
92
|
|
|
return $this->backends[$foundType]; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function moveToTrash(IStorage $storage, string $internalPath): bool { |
97
|
|
|
if ($this->trashPaused) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
try { |
101
|
|
|
$backend = $this->getBackendForStorage($storage); |
102
|
|
|
return $backend->moveToTrash($storage, $internalPath); |
103
|
|
|
} catch (BackendNotFoundException $e) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getTrashNodeById(IUser $user, int $fileId) { |
109
|
|
|
foreach ($this->backends as $backend) { |
110
|
|
|
$item = $backend->getTrashNodeById($user, $fileId); |
111
|
|
|
if ($item !== null) { |
112
|
|
|
return $item; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function pauseTrash() { |
119
|
|
|
$this->trashPaused = true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function resumeTrash() { |
123
|
|
|
$this->trashPaused = false; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|