|
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 OC\Files\Filesystem; |
|
25
|
|
|
use OC\Files\View; |
|
26
|
|
|
use OCA\Files_Trashbin\Helper; |
|
27
|
|
|
use OCA\Files_Trashbin\Storage; |
|
28
|
|
|
use OCA\Files_Trashbin\Trashbin; |
|
29
|
|
|
use OCP\Files\FileInfo; |
|
30
|
|
|
use OCP\Files\IRootFolder; |
|
31
|
|
|
use OCP\Files\NotFoundException; |
|
32
|
|
|
use OCP\Files\Storage\IStorage; |
|
33
|
|
|
use OCP\IUser; |
|
34
|
|
|
|
|
35
|
|
|
class LegacyTrashBackend implements ITrashBackend { |
|
36
|
|
|
/** @var array */ |
|
37
|
|
|
private $deletedFiles = []; |
|
38
|
|
|
|
|
39
|
|
|
/** @var IRootFolder */ |
|
40
|
|
|
private $rootFolder; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct(IRootFolder $rootFolder) { |
|
43
|
|
|
$this->rootFolder = $rootFolder; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array $items |
|
48
|
|
|
* @param IUser $user |
|
49
|
|
|
* @param ITrashItem $parent |
|
50
|
|
|
* @return ITrashItem[] |
|
51
|
|
|
*/ |
|
52
|
|
|
private function mapTrashItems(array $items, IUser $user, ITrashItem $parent = null): array { |
|
53
|
|
|
$parentTrashPath = ($parent instanceof ITrashItem) ? $parent->getTrashPath() : ''; |
|
54
|
|
|
$isRoot = $parent === null; |
|
55
|
|
|
return array_map(function (FileInfo $file) use ($parent, $parentTrashPath, $isRoot, $user) { |
|
56
|
|
|
return new TrashItem( |
|
57
|
|
|
$this, |
|
58
|
|
|
$isRoot ? $file['extraData'] : $parent->getOriginalLocation() . '/' . $file->getName(), |
|
|
|
|
|
|
59
|
|
|
$file->getMTime(), |
|
60
|
|
|
$parentTrashPath . '/' . $file->getName() . ($isRoot ? '.d' . $file->getMtime() : ''), |
|
61
|
|
|
$file, |
|
62
|
|
|
$user |
|
63
|
|
|
); |
|
64
|
|
|
}, $items); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function listTrashRoot(IUser $user): array { |
|
68
|
|
|
$entries = Helper::getTrashFiles('/', $user->getUID()); |
|
69
|
|
|
return $this->mapTrashItems($entries, $user); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function listTrashFolder(ITrashItem $folder): array { |
|
73
|
|
|
$user = $folder->getUser(); |
|
74
|
|
|
$entries = Helper::getTrashFiles($folder->getTrashPath(), $user->getUID()); |
|
75
|
|
|
return $this->mapTrashItems($entries, $user ,$folder); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function restoreItem(ITrashItem $item) { |
|
79
|
|
|
Trashbin::restore($item->getTrashPath(), $item->getName(), $item->isRootItem() ? $item->getDeletedTime() : null); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function removeItem(ITrashItem $item) { |
|
83
|
|
|
$user = $item->getUser(); |
|
84
|
|
|
if ($item->isRootItem()) { |
|
85
|
|
|
$path = substr($item->getTrashPath(), 0, -strlen('.d' . $item->getDeletedTime())); |
|
86
|
|
|
Trashbin::delete($path, $user->getUID(), $item->getDeletedTime()); |
|
87
|
|
|
} else { |
|
88
|
|
|
Trashbin::delete($item->getTrashPath(), $user->getUID(), null); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function moveToTrash(IStorage $storage, string $internalPath): bool { |
|
94
|
|
|
if (!$storage instanceof Storage) { |
|
95
|
|
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
$normalized = Filesystem::normalizePath($storage->getMountPoint() . '/' . $internalPath, true, false, true); |
|
98
|
|
|
$view = Filesystem::getView(); |
|
99
|
|
|
if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) { |
|
100
|
|
|
$this->deletedFiles[$normalized] = $normalized; |
|
101
|
|
|
if ($filesPath = $view->getRelativePath($normalized)) { |
|
102
|
|
|
$filesPath = trim($filesPath, '/'); |
|
103
|
|
|
$result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath); |
|
104
|
|
|
} else { |
|
105
|
|
|
$result = false; |
|
106
|
|
|
} |
|
107
|
|
|
unset($this->deletedFiles[$normalized]); |
|
108
|
|
|
} else { |
|
109
|
|
|
$result = false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $result; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getTrashNodeById(IUser $user, int $fileId) { |
|
116
|
|
|
try { |
|
117
|
|
|
$userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
|
118
|
|
|
$trash = $userFolder->getParent()->get('files_trashbin/files'); |
|
119
|
|
|
$trashFiles = $trash->getById($fileId); |
|
120
|
|
|
if (!$trashFiles) { |
|
121
|
|
|
return null; |
|
122
|
|
|
} |
|
123
|
|
|
return $trashFiles ? array_pop($trashFiles) : null; |
|
124
|
|
|
} catch (NotFoundException $e) { |
|
125
|
|
|
return null; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: