-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathVersionsBackend.php
419 lines (360 loc) Β· 15.5 KB
/
VersionsBackend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\GroupFolders\Versions;
use Exception;
use OCA\DAV\Connector\Sabre\Exception\Forbidden;
use OCA\Files_Versions\Versions\IDeletableVersionBackend;
use OCA\Files_Versions\Versions\IMetadataVersion;
use OCA\Files_Versions\Versions\IMetadataVersionBackend;
use OCA\Files_Versions\Versions\INeedSyncVersionBackend;
use OCA\Files_Versions\Versions\IVersion;
use OCA\Files_Versions\Versions\IVersionBackend;
use OCA\Files_Versions\Versions\IVersionsImporterBackend;
use OCA\GroupFolders\Mount\GroupMountPoint;
use OCA\GroupFolders\Mount\MountProvider;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Constants;
use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\Files\Folder;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorage;
use OCP\IUser;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
class VersionsBackend implements IVersionBackend, IMetadataVersionBackend, IDeletableVersionBackend, INeedSyncVersionBackend, IVersionsImporterBackend {
public function __construct(
private IRootFolder $rootFolder,
private Folder $appFolder,
private MountProvider $mountProvider,
private ITimeFactory $timeFactory,
private LoggerInterface $logger,
private GroupVersionsMapper $groupVersionsMapper,
private IMimeTypeLoader $mimeTypeLoader,
private IUserSession $userSession,
) {
}
public function useBackendForStorage(IStorage $storage): bool {
return true;
}
private function getFolderIdForFile(FileInfo $file): int {
$mount = $file->getMountPoint();
if (!($mount instanceof GroupMountPoint)) {
throw new Exception('Trying to get the folder id for a file not in a group folder');
}
return $mount->getFolderId();
}
public function getVersionFolderForFile(FileInfo $file): Folder {
$folderId = $this->getFolderIdForFile($file);
try {
$groupfoldersVersionsFolder = $this->getVersionsFolder($folderId);
/** @var Folder $versionsFolder */
return $groupfoldersVersionsFolder->get((string)$file->getId());
} catch (NotFoundException $e) {
// The folder for the file's versions might not exists if no versions has been create yet.
return $groupfoldersVersionsFolder->newFolder((string)$file->getId());
}
}
public function getVersionsForFile(IUser $user, FileInfo $file): array {
$versionsFolder = $this->getVersionFolderForFile($file);
try {
$versions = $this->getVersionsForFileFromDB($file, $user);
// Early exit if we find any version in the database.
// Else we continue to populate the DB from what's on disk.
if (count($versions) > 0) {
return $versions;
}
// Insert the entry in the DB for the current version.
$versionEntity = new GroupVersionEntity();
$versionEntity->setFileId($file->getId());
$versionEntity->setTimestamp($file->getMTime());
$versionEntity->setSize($file->getSize());
$versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype()));
$versionEntity->setDecodedMetadata([]);
$this->groupVersionsMapper->insert($versionEntity);
// Insert entries in the DB for existing versions.
$versionsOnFS = $versionsFolder->getDirectoryListing();
foreach ($versionsOnFS as $version) {
if ($version instanceof Folder) {
$this->logger->error('Found an unexpected subfolder inside the groupfolder version folder.');
}
$versionEntity = new GroupVersionEntity();
$versionEntity->setFileId($file->getId());
// HACK: before this commit, versions were created with the current timestamp instead of the version's mtime.
// This means that the name of some versions is the exact mtime of the next version. This behavior is now fixed.
// To prevent occasional conflicts between the last version and the current one, we decrement the last version mtime.
$mtime = (int)$version->getName();
if ($mtime === $file->getMTime()) {
$versionEntity->setTimestamp($mtime - 1);
$version->move($version->getParent()->getPath() . '/' . ($mtime - 1));
} else {
$versionEntity->setTimestamp($mtime);
}
$versionEntity->setSize($version->getSize());
// Use the main file mimetype for this initialization as the original mimetype is unknown.
$versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype()));
$versionEntity->setDecodedMetadata([]);
$this->groupVersionsMapper->insert($versionEntity);
}
return $this->getVersionsForFileFromDB($file, $user);
} catch (NotFoundException $e) {
return [];
}
}
/**
* @return IVersion[]
*/
private function getVersionsForFileFromDB(FileInfo $fileInfo, IUser $user): array {
$folderId = $this->getFolderIdForFile($fileInfo);
$mountPoint = $fileInfo->getMountPoint();
$versionsFolder = $this->getVersionFolderForFile($fileInfo);
/** @var Folder */
$groupFolder = $this->rootFolder->get('/__groupfolders/' . $folderId);
$versionEntities = $this->groupVersionsMapper->findAllVersionsForFileId($fileInfo->getId());
$mappedVersions = array_map(
function (GroupVersionEntity $versionEntity) use ($versionsFolder, $mountPoint, $fileInfo, $user, $folderId, $groupFolder) {
if ($fileInfo->getMtime() === $versionEntity->getTimestamp()) {
if ($fileInfo instanceof File) {
$versionFile = $fileInfo;
} else {
$versionFile = $groupFolder->get($fileInfo->getInternalPath());
}
} else {
try {
$versionFile = $versionsFolder->get((string)$versionEntity->getTimestamp());
} catch (NotFoundException $e) {
// The version does not exists on disk anymore, so we can delete its entity in the DB.
// The reality is that the disk version might have been lost during a move operation between storages,
// and its not possible to recover it, so removing the entity makes sense.
$this->groupVersionsMapper->delete($versionEntity);
return null;
}
}
return new GroupVersion(
$versionEntity->getTimestamp(),
$versionEntity->getTimestamp(),
$fileInfo->getName(),
$versionEntity->getSize(),
$this->mimeTypeLoader->getMimetypeById($versionEntity->getMimetype()),
$mountPoint->getInternalPath($fileInfo->getPath()),
$fileInfo,
$this,
$user,
$versionEntity->getDecodedMetadata(),
$versionFile,
$folderId,
);
},
$versionEntities,
);
// Filter out null values.
return array_filter($mappedVersions);
}
/**
* @return void
*/
public function createVersion(IUser $user, FileInfo $file) {
$versionsFolder = $this->getVersionFolderForFile($file);
$versionMount = $versionsFolder->getMountPoint();
$sourceMount = $file->getMountPoint();
$sourceCache = $sourceMount->getStorage()->getCache();
$revision = $file->getMtime();
$versionInternalPath = $versionsFolder->getInternalPath() . '/' . $revision;
$sourceInternalPath = $file->getInternalPath();
$versionMount->getStorage()->copyFromStorage($sourceMount->getStorage(), $sourceInternalPath, $versionInternalPath);
$versionMount->getStorage()->getCache()->copyFromCache($sourceCache, $sourceCache->get($sourceInternalPath), $versionInternalPath);
}
public function rollback(IVersion $version): void {
if (!($version instanceof GroupVersion)) {
throw new \LogicException('Trying to restore a version from a file not in a group folder');
}
if (!$this->currentUserHasPermissions($version->getSourceFile(), \OCP\Constants::PERMISSION_UPDATE)) {
throw new Forbidden('You cannot restore this version because you do not have update permissions on the source file.');
}
$this->createVersion($version->getUser(), $version->getSourceFile());
/** @var GroupMountPoint $targetMount */
$targetMount = $version->getSourceFile()->getMountPoint();
$targetCache = $targetMount->getStorage()->getCache();
$versionMount = $version->getVersionFile()->getMountPoint();
$versionCache = $versionMount->getStorage()->getCache();
$targetInternalPath = $version->getSourceFile()->getInternalPath();
$versionInternalPath = $version->getVersionFile()->getInternalPath();
$targetMount->getStorage()->copyFromStorage($versionMount->getStorage(), $versionInternalPath, $targetInternalPath);
$versionMount->getStorage()->getCache()->copyFromCache($targetCache, $versionCache->get($versionInternalPath), $targetMount->getSourcePath() . '/' . $targetInternalPath);
}
public function read(IVersion $version) {
if ($version instanceof GroupVersion) {
return $version->getVersionFile()->fopen('r');
} else {
return false;
}
}
public function getVersionFile(IUser $user, FileInfo $sourceFile, $revision): File {
$versionsFolder = $this->getVersionFolderForFile($sourceFile);
$file = $versionsFolder->get((string)$revision);
assert($file instanceof File);
return $file;
}
/**
* @param array{id: int, mount_point: string, groups: array<empty, empty>|mixed, quota: mixed, size: int, acl: bool} $folder
* @return (FileInfo|null)[] [$fileId => FileInfo|null]
*/
public function getAllVersionedFiles(array $folder) {
$versionsFolder = $this->getVersionsFolder($folder['id']);
$mount = $this->mountProvider->getMount($folder['id'], '/dummyuser/files/' . $folder['mount_point'], Constants::PERMISSION_ALL, $folder['quota']);
if ($mount === null) {
$this->logger->error('Tried to get all the versioned files from a non existing mountpoint');
return [];
}
try {
$contents = $versionsFolder->getDirectoryListing();
} catch (NotFoundException $e) {
return [];
}
$fileIds = array_map(function (Node $node) use ($mount): int {
return (int)$node->getName();
}, $contents);
$files = array_map(function (int $fileId) use ($mount): ?FileInfo {
$cacheEntry = $mount->getStorage()->getCache()->get($fileId);
if ($cacheEntry) {
return new \OC\Files\FileInfo($mount->getMountPoint() . '/' . $cacheEntry->getPath(), $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount);
} else {
return null;
}
}, $fileIds);
return array_combine($fileIds, $files);
}
public function deleteAllVersionsForFile(int $folderId, int $fileId): void {
$versionsFolder = $this->getVersionsFolder($folderId);
try {
$versionsFolder->get((string)$fileId)->delete();
$this->groupVersionsMapper->deleteAllVersionsForFileId($fileId);
} catch (NotFoundException $e) {
}
}
private function getVersionsFolder(int $folderId): Folder {
try {
return $this->appFolder->get('versions/' . $folderId);
} catch (NotFoundException $e) {
/** @var Folder $trashRoot */
$trashRoot = $this->appFolder->nodeExists('versions') ? $this->appFolder->get('versions') : $this->appFolder->newFolder('versions');
return $trashRoot->newFolder((string)$folderId);
}
}
public function setMetadataValue(Node $node, int $revision, string $key, string $value): void {
if (!$this->currentUserHasPermissions($node, \OCP\Constants::PERMISSION_UPDATE)) {
throw new Forbidden('You cannot update the version\'s metadata because you do not have update permissions on the source file.');
}
$versionEntity = $this->groupVersionsMapper->findVersionForFileId($node->getId(), $revision);
$versionEntity->setMetadataValue($key, $value);
$this->groupVersionsMapper->update($versionEntity);
}
public function deleteVersion(IVersion $version): void {
if (!$this->currentUserHasPermissions($version->getSourceFile(), \OCP\Constants::PERMISSION_DELETE)) {
throw new Forbidden('You cannot delete this version because you do not have delete permissions on the source file.');
}
$sourceFile = $version->getSourceFile();
$mount = $sourceFile->getMountPoint();
if (!($mount instanceof GroupMountPoint)) {
return;
}
$versionsFolder = $this->getVersionsFolder($mount->getFolderId())->get((string)$sourceFile->getId());
/** @var Folder $versionsFolder */
$versionsFolder->get((string)$version->getRevisionId())->delete();
$versionEntity = $this->groupVersionsMapper->findVersionForFileId(
$version->getSourceFile()->getId(),
$version->getTimestamp(),
);
$this->groupVersionsMapper->delete($versionEntity);
}
public function createVersionEntity(File $file): void {
$versionEntity = new GroupVersionEntity();
$versionEntity->setFileId($file->getId());
$versionEntity->setTimestamp($file->getMTime());
$versionEntity->setSize($file->getSize());
$versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype()));
$versionEntity->setDecodedMetadata([]);
$this->groupVersionsMapper->insert($versionEntity);
}
public function updateVersionEntity(File $sourceFile, int $revision, array $properties): void {
$versionEntity = $this->groupVersionsMapper->findVersionForFileId($sourceFile->getId(), $revision);
if (isset($properties['timestamp'])) {
$versionEntity->setTimestamp($properties['timestamp']);
}
if (isset($properties['size'])) {
$versionEntity->setSize($properties['size']);
}
if (isset($properties['mimetype'])) {
$versionEntity->setMimetype($properties['mimetype']);
}
$this->groupVersionsMapper->update($versionEntity);
}
public function deleteVersionsEntity(File $file): void {
$this->groupVersionsMapper->deleteAllVersionsForFileId($file->getId());
}
private function currentUserHasPermissions(FileInfo $sourceFile, int $permissions): bool {
$currentUserId = $this->userSession->getUser()?->getUID();
if ($currentUserId === null) {
throw new NotFoundException("No user logged in");
}
return ($sourceFile->getPermissions() & $permissions) === $permissions;
}
/**
* @inheritdoc
* @psalm-suppress MethodSignatureMismatch - The signature of the method is correct, but psalm somehow can't understand it
*/
public function importVersionsForFile(IUser $user, Node $source, Node $target, array $versions): void {
$mount = $target->getMountPoint();
if (!($mount instanceof GroupMountPoint)) {
return;
}
$versionsFolder = $this->getVersionFolderForFile($target);
foreach ($versions as $version) {
// 1. Move the file to the new location
if ($version->getTimestamp() !== $source->getMTime()) {
$backend = $version->getBackend();
$versionFile = $backend->getVersionFile($user, $source, $version->getRevisionId());
$versionsFolder->newFile($version->getRevisionId(), $versionFile->fopen('r'));
}
// 2. Create the entity in the database
$versionEntity = new GroupVersionEntity();
$versionEntity->setFileId($target->getId());
$versionEntity->setTimestamp($version->getTimestamp());
$versionEntity->setSize($version->getSize());
$versionEntity->setMimetype($this->mimeTypeLoader->getId($version->getMimetype()));
if ($version instanceof IMetadataVersion) {
$versionEntity->setDecodedMetadata($version->getMetadata());
}
$this->groupVersionsMapper->insert($versionEntity);
}
}
/**
* @inheritdoc
*/
public function clearVersionsForFile(IUser $user, Node $source, Node $target): void {
$folderId = $this->getFolderIdForFile($source);
$this->deleteAllVersionsForFile($folderId, $target->getId());
}
}