Passed
Push — master ( b9eef6...a1fcee )
by Blizzz
13:41 queued 12s
created
apps/files_versions/lib/Storage.php 1 patch
Indentation   +850 added lines, -850 removed lines patch added patch discarded remove patch
@@ -69,854 +69,854 @@
 block discarded – undo
69 69
 use Psr\Log\LoggerInterface;
70 70
 
71 71
 class Storage {
72
-	public const DEFAULTENABLED = true;
73
-	public const DEFAULTMAXSIZE = 50; // unit: percentage; 50% of available disk space/quota
74
-	public const VERSIONS_ROOT = 'files_versions/';
75
-
76
-	public const DELETE_TRIGGER_MASTER_REMOVED = 0;
77
-	public const DELETE_TRIGGER_RETENTION_CONSTRAINT = 1;
78
-	public const DELETE_TRIGGER_QUOTA_EXCEEDED = 2;
79
-
80
-	// files for which we can remove the versions after the delete operation was successful
81
-	private static $deletedFiles = [];
82
-
83
-	private static $sourcePathAndUser = [];
84
-
85
-	private static $max_versions_per_interval = [
86
-		//first 10sec, one version every 2sec
87
-		1 => ['intervalEndsAfter' => 10,      'step' => 2],
88
-		//next minute, one version every 10sec
89
-		2 => ['intervalEndsAfter' => 60,      'step' => 10],
90
-		//next hour, one version every minute
91
-		3 => ['intervalEndsAfter' => 3600,    'step' => 60],
92
-		//next 24h, one version every hour
93
-		4 => ['intervalEndsAfter' => 86400,   'step' => 3600],
94
-		//next 30days, one version per day
95
-		5 => ['intervalEndsAfter' => 2592000, 'step' => 86400],
96
-		//until the end one version per week
97
-		6 => ['intervalEndsAfter' => -1,      'step' => 604800],
98
-	];
99
-
100
-	/** @var \OCA\Files_Versions\AppInfo\Application */
101
-	private static $application;
102
-
103
-	/**
104
-	 * get the UID of the owner of the file and the path to the file relative to
105
-	 * owners files folder
106
-	 *
107
-	 * @param string $filename
108
-	 * @return array
109
-	 * @throws \OC\User\NoUserException
110
-	 */
111
-	public static function getUidAndFilename($filename) {
112
-		$uid = Filesystem::getOwner($filename);
113
-		$userManager = \OC::$server->get(IUserManager::class);
114
-		// if the user with the UID doesn't exists, e.g. because the UID points
115
-		// to a remote user with a federated cloud ID we use the current logged-in
116
-		// user. We need a valid local user to create the versions
117
-		if (!$userManager->userExists($uid)) {
118
-			$uid = OC_User::getUser();
119
-		}
120
-		Filesystem::initMountPoints($uid);
121
-		if ($uid !== OC_User::getUser()) {
122
-			$info = Filesystem::getFileInfo($filename);
123
-			$ownerView = new View('/'.$uid.'/files');
124
-			try {
125
-				$filename = $ownerView->getPath($info['fileid']);
126
-				// make sure that the file name doesn't end with a trailing slash
127
-				// can for example happen single files shared across servers
128
-				$filename = rtrim($filename, '/');
129
-			} catch (NotFoundException $e) {
130
-				$filename = null;
131
-			}
132
-		}
133
-		return [$uid, $filename];
134
-	}
135
-
136
-	/**
137
-	 * Remember the owner and the owner path of the source file
138
-	 *
139
-	 * @param string $source source path
140
-	 */
141
-	public static function setSourcePathAndUser($source) {
142
-		[$uid, $path] = self::getUidAndFilename($source);
143
-		self::$sourcePathAndUser[$source] = ['uid' => $uid, 'path' => $path];
144
-	}
145
-
146
-	/**
147
-	 * Gets the owner and the owner path from the source path
148
-	 *
149
-	 * @param string $source source path
150
-	 * @return array with user id and path
151
-	 */
152
-	public static function getSourcePathAndUser($source) {
153
-		if (isset(self::$sourcePathAndUser[$source])) {
154
-			$uid = self::$sourcePathAndUser[$source]['uid'];
155
-			$path = self::$sourcePathAndUser[$source]['path'];
156
-			unset(self::$sourcePathAndUser[$source]);
157
-		} else {
158
-			$uid = $path = false;
159
-		}
160
-		return [$uid, $path];
161
-	}
162
-
163
-	/**
164
-	 * get current size of all versions from a given user
165
-	 *
166
-	 * @param string $user user who owns the versions
167
-	 * @return int versions size
168
-	 */
169
-	private static function getVersionsSize($user) {
170
-		$view = new View('/' . $user);
171
-		$fileInfo = $view->getFileInfo('/files_versions');
172
-		return isset($fileInfo['size']) ? $fileInfo['size'] : 0;
173
-	}
174
-
175
-	/**
176
-	 * store a new version of a file.
177
-	 */
178
-	public static function store($filename) {
179
-
180
-		// if the file gets streamed we need to remove the .part extension
181
-		// to get the right target
182
-		$ext = pathinfo($filename, PATHINFO_EXTENSION);
183
-		if ($ext === 'part') {
184
-			$filename = substr($filename, 0, -5);
185
-		}
186
-
187
-		// we only handle existing files
188
-		if (! Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) {
189
-			return false;
190
-		}
191
-
192
-		// since hook paths are always relative to the "default filesystem view"
193
-		// we always use the owner from there to get the full node
194
-		$uid = Filesystem::getView()->getOwner('');
195
-
196
-		/** @var IRootFolder $rootFolder */
197
-		$rootFolder = \OC::$server->get(IRootFolder::class);
198
-		$userFolder = $rootFolder->getUserFolder($uid);
199
-
200
-		$eventDispatcher = \OC::$server->get(IEventDispatcher::class);
201
-		try {
202
-			$file = $userFolder->get($filename);
203
-		} catch (NotFoundException $e) {
204
-			return false;
205
-		}
206
-
207
-		$mount = $file->getMountPoint();
208
-		if ($mount instanceof SharedMount) {
209
-			$ownerFolder = $rootFolder->getUserFolder($mount->getShare()->getShareOwner());
210
-			$ownerNodes = $ownerFolder->getById($file->getId());
211
-			if (count($ownerNodes)) {
212
-				$file = current($ownerNodes);
213
-				$uid = $mount->getShare()->getShareOwner();
214
-			}
215
-		}
216
-
217
-		/** @var IUserManager $userManager */
218
-		$userManager = \OC::$server->get(IUserManager::class);
219
-		$user = $userManager->get($uid);
220
-
221
-		if (!$user) {
222
-			return false;
223
-		}
224
-
225
-		// no use making versions for empty files
226
-		if ($file->getSize() === 0) {
227
-			return false;
228
-		}
229
-
230
-		$event = new CreateVersionEvent($file);
231
-		$eventDispatcher->dispatch('OCA\Files_Versions::createVersion', $event);
232
-		if ($event->shouldCreateVersion() === false) {
233
-			return false;
234
-		}
235
-
236
-		/** @var IVersionManager $versionManager */
237
-		$versionManager = \OC::$server->get(IVersionManager::class);
238
-
239
-		$versionManager->createVersion($user, $file);
240
-	}
241
-
242
-
243
-	/**
244
-	 * mark file as deleted so that we can remove the versions if the file is gone
245
-	 * @param string $path
246
-	 */
247
-	public static function markDeletedFile($path) {
248
-		[$uid, $filename] = self::getUidAndFilename($path);
249
-		self::$deletedFiles[$path] = [
250
-			'uid' => $uid,
251
-			'filename' => $filename];
252
-	}
253
-
254
-	/**
255
-	 * delete the version from the storage and cache
256
-	 *
257
-	 * @param View $view
258
-	 * @param string $path
259
-	 */
260
-	protected static function deleteVersion($view, $path) {
261
-		$view->unlink($path);
262
-		/**
263
-		 * @var \OC\Files\Storage\Storage $storage
264
-		 * @var string $internalPath
265
-		 */
266
-		[$storage, $internalPath] = $view->resolvePath($path);
267
-		$cache = $storage->getCache($internalPath);
268
-		$cache->remove($internalPath);
269
-	}
270
-
271
-	/**
272
-	 * Delete versions of a file
273
-	 */
274
-	public static function delete($path) {
275
-		$deletedFile = self::$deletedFiles[$path];
276
-		$uid = $deletedFile['uid'];
277
-		$filename = $deletedFile['filename'];
278
-
279
-		if (!Filesystem::file_exists($path)) {
280
-			$view = new View('/' . $uid . '/files_versions');
281
-
282
-			$versions = self::getVersions($uid, $filename);
283
-			if (!empty($versions)) {
284
-				foreach ($versions as $v) {
285
-					\OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]);
286
-					self::deleteVersion($view, $filename . '.v' . $v['version']);
287
-					\OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]);
288
-				}
289
-			}
290
-		}
291
-		unset(self::$deletedFiles[$path]);
292
-	}
293
-
294
-	/**
295
-	 * Rename or copy versions of a file of the given paths
296
-	 *
297
-	 * @param string $sourcePath source path of the file to move, relative to
298
-	 * the currently logged in user's "files" folder
299
-	 * @param string $targetPath target path of the file to move, relative to
300
-	 * the currently logged in user's "files" folder
301
-	 * @param string $operation can be 'copy' or 'rename'
302
-	 */
303
-	public static function renameOrCopy($sourcePath, $targetPath, $operation) {
304
-		[$sourceOwner, $sourcePath] = self::getSourcePathAndUser($sourcePath);
305
-
306
-		// it was a upload of a existing file if no old path exists
307
-		// in this case the pre-hook already called the store method and we can
308
-		// stop here
309
-		if ($sourcePath === false) {
310
-			return true;
311
-		}
312
-
313
-		[$targetOwner, $targetPath] = self::getUidAndFilename($targetPath);
314
-
315
-		$sourcePath = ltrim($sourcePath, '/');
316
-		$targetPath = ltrim($targetPath, '/');
317
-
318
-		$rootView = new View('');
319
-
320
-		// did we move a directory ?
321
-		if ($rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
322
-			// does the directory exists for versions too ?
323
-			if ($rootView->is_dir('/' . $sourceOwner . '/files_versions/' . $sourcePath)) {
324
-				// create missing dirs if necessary
325
-				self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
326
-
327
-				// move the directory containing the versions
328
-				$rootView->$operation(
329
-					'/' . $sourceOwner . '/files_versions/' . $sourcePath,
330
-					'/' . $targetOwner . '/files_versions/' . $targetPath
331
-				);
332
-			}
333
-		} elseif ($versions = Storage::getVersions($sourceOwner, '/' . $sourcePath)) {
334
-			// create missing dirs if necessary
335
-			self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
336
-
337
-			foreach ($versions as $v) {
338
-				// move each version one by one to the target directory
339
-				$rootView->$operation(
340
-					'/' . $sourceOwner . '/files_versions/' . $sourcePath.'.v' . $v['version'],
341
-					'/' . $targetOwner . '/files_versions/' . $targetPath.'.v'.$v['version']
342
-				);
343
-			}
344
-		}
345
-
346
-		// if we moved versions directly for a file, schedule expiration check for that file
347
-		if (!$rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
348
-			self::scheduleExpire($targetOwner, $targetPath);
349
-		}
350
-	}
351
-
352
-	/**
353
-	 * Rollback to an old version of a file.
354
-	 *
355
-	 * @param string $file file name
356
-	 * @param int $revision revision timestamp
357
-	 * @return bool
358
-	 */
359
-	public static function rollback(string $file, int $revision, IUser $user) {
360
-
361
-		// add expected leading slash
362
-		$filename = '/' . ltrim($file, '/');
363
-
364
-		// Fetch the userfolder to trigger view hooks
365
-		$root = \OC::$server->get(IRootFolder::class);
366
-		$userFolder = $root->getUserFolder($user->getUID());
367
-
368
-		$users_view = new View('/'.$user->getUID());
369
-		$files_view = new View('/'. $user->getUID().'/files');
370
-
371
-		$versionCreated = false;
372
-
373
-		$fileInfo = $files_view->getFileInfo($file);
374
-
375
-		// check if user has the permissions to revert a version
376
-		if (!$fileInfo->isUpdateable()) {
377
-			return false;
378
-		}
379
-
380
-		//first create a new version
381
-		$version = 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename);
382
-		if (!$users_view->file_exists($version)) {
383
-			$users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename));
384
-			$versionCreated = true;
385
-		}
386
-
387
-		$fileToRestore = 'files_versions' . $filename . '.v' . $revision;
388
-
389
-		// Restore encrypted version of the old file for the newly restored file
390
-		// This has to happen manually here since the file is manually copied below
391
-		$oldVersion = $users_view->getFileInfo($fileToRestore)->getEncryptedVersion();
392
-		$oldFileInfo = $users_view->getFileInfo($fileToRestore);
393
-		$cache = $fileInfo->getStorage()->getCache();
394
-		$cache->update(
395
-			$fileInfo->getId(), [
396
-				'encrypted' => $oldVersion,
397
-				'encryptedVersion' => $oldVersion,
398
-				'size' => $oldFileInfo->getSize()
399
-			]
400
-		);
401
-
402
-		// rollback
403
-		if (self::copyFileContents($users_view, $fileToRestore, 'files' . $filename)) {
404
-			$files_view->touch($file, $revision);
405
-			Storage::scheduleExpire($user->getUID(), $file);
406
-
407
-			$node = $userFolder->get($file);
408
-
409
-			// TODO: move away from those legacy hooks!
410
-			\OC_Hook::emit('\OCP\Versions', 'rollback', [
411
-				'path' => $filename,
412
-				'revision' => $revision,
413
-				'node' => $node,
414
-			]);
415
-			return true;
416
-		} elseif ($versionCreated) {
417
-			self::deleteVersion($users_view, $version);
418
-		}
419
-
420
-		return false;
421
-	}
422
-
423
-	/**
424
-	 * Stream copy file contents from $path1 to $path2
425
-	 *
426
-	 * @param View $view view to use for copying
427
-	 * @param string $path1 source file to copy
428
-	 * @param string $path2 target file
429
-	 *
430
-	 * @return bool true for success, false otherwise
431
-	 */
432
-	private static function copyFileContents($view, $path1, $path2) {
433
-		/** @var \OC\Files\Storage\Storage $storage1 */
434
-		[$storage1, $internalPath1] = $view->resolvePath($path1);
435
-		/** @var \OC\Files\Storage\Storage $storage2 */
436
-		[$storage2, $internalPath2] = $view->resolvePath($path2);
437
-
438
-		$view->lockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
439
-		$view->lockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
440
-
441
-		// TODO add a proper way of overwriting a file while maintaining file ids
442
-		if ($storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage') || $storage2->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')) {
443
-			$source = $storage1->fopen($internalPath1, 'r');
444
-			$target = $storage2->fopen($internalPath2, 'w');
445
-			[, $result] = \OC_Helper::streamCopy($source, $target);
446
-			fclose($source);
447
-			fclose($target);
448
-
449
-			if ($result !== false) {
450
-				$storage1->unlink($internalPath1);
451
-			}
452
-		} else {
453
-			$result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
454
-		}
455
-
456
-		$view->unlockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
457
-		$view->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
458
-
459
-		return ($result !== false);
460
-	}
461
-
462
-	/**
463
-	 * get a list of all available versions of a file in descending chronological order
464
-	 * @param string $uid user id from the owner of the file
465
-	 * @param string $filename file to find versions of, relative to the user files dir
466
-	 * @param string $userFullPath
467
-	 * @return array versions newest version first
468
-	 */
469
-	public static function getVersions($uid, $filename, $userFullPath = '') {
470
-		$versions = [];
471
-		if (empty($filename)) {
472
-			return $versions;
473
-		}
474
-		// fetch for old versions
475
-		$view = new View('/' . $uid . '/');
476
-
477
-		$pathinfo = pathinfo($filename);
478
-		$versionedFile = $pathinfo['basename'];
479
-
480
-		$dir = Filesystem::normalizePath(self::VERSIONS_ROOT . '/' . $pathinfo['dirname']);
481
-
482
-		$dirContent = false;
483
-		if ($view->is_dir($dir)) {
484
-			$dirContent = $view->opendir($dir);
485
-		}
486
-
487
-		if ($dirContent === false) {
488
-			return $versions;
489
-		}
490
-
491
-		if (is_resource($dirContent)) {
492
-			while (($entryName = readdir($dirContent)) !== false) {
493
-				if (!Filesystem::isIgnoredDir($entryName)) {
494
-					$pathparts = pathinfo($entryName);
495
-					$filename = $pathparts['filename'];
496
-					if ($filename === $versionedFile) {
497
-						$pathparts = pathinfo($entryName);
498
-						$timestamp = substr($pathparts['extension'], 1);
499
-						$filename = $pathparts['filename'];
500
-						$key = $timestamp . '#' . $filename;
501
-						$versions[$key]['version'] = $timestamp;
502
-						$versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($timestamp);
503
-						if (empty($userFullPath)) {
504
-							$versions[$key]['preview'] = '';
505
-						} else {
506
-							/** @var IURLGenerator $urlGenerator */
507
-							$urlGenerator = \OC::$server->get(IURLGenerator::class);
508
-							$versions[$key]['preview'] = $urlGenerator->linkToRoute('files_version.Preview.getPreview',
509
-								['file' => $userFullPath, 'version' => $timestamp]);
510
-						}
511
-						$versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename);
512
-						$versions[$key]['name'] = $versionedFile;
513
-						$versions[$key]['size'] = $view->filesize($dir . '/' . $entryName);
514
-						$versions[$key]['mimetype'] = \OC::$server->get(IMimeTypeDetector::class)->detectPath($versionedFile);
515
-					}
516
-				}
517
-			}
518
-			closedir($dirContent);
519
-		}
520
-
521
-		// sort with newest version first
522
-		krsort($versions);
523
-
524
-		return $versions;
525
-	}
526
-
527
-	/**
528
-	 * Expire versions that older than max version retention time
529
-	 *
530
-	 * @param string $uid
531
-	 */
532
-	public static function expireOlderThanMaxForUser($uid) {
533
-		/** @var IRootFolder $root */
534
-		$root = \OC::$server->get(IRootFolder::class);
535
-		try {
536
-			/** @var Folder $versionsRoot */
537
-			$versionsRoot = $root->get('/' . $uid . '/files_versions');
538
-		} catch (NotFoundException $e) {
539
-			return;
540
-		}
541
-
542
-		$expiration = self::getExpiration();
543
-		$threshold = $expiration->getMaxAgeAsTimestamp();
544
-		if (!$threshold) {
545
-			return;
546
-		}
547
-
548
-		$allVersions = $versionsRoot->search(new SearchQuery(
549
-			new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [
550
-				new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', FileInfo::MIMETYPE_FOLDER),
551
-			]),
552
-			0,
553
-			0,
554
-			[]
555
-		));
556
-
557
-		/** @var Node[] $versions */
558
-		$versions = array_filter($allVersions, function (Node $info) use ($threshold) {
559
-			$versionsBegin = strrpos($info->getName(), '.v');
560
-			if ($versionsBegin === false) {
561
-				return false;
562
-			}
563
-			$version = (int)substr($info->getName(), $versionsBegin + 2);
564
-			return $version < $threshold;
565
-		});
566
-
567
-		foreach ($versions as $version) {
568
-			$internalPath = $version->getInternalPath();
569
-			\OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $internalPath, 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT]);
570
-			$version->delete();
571
-			\OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $internalPath, 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT]);
572
-		}
573
-	}
574
-
575
-	/**
576
-	 * translate a timestamp into a string like "5 days ago"
577
-	 *
578
-	 * @param int $timestamp
579
-	 * @return string for example "5 days ago"
580
-	 */
581
-	private static function getHumanReadableTimestamp($timestamp) {
582
-		$diff = time() - $timestamp;
583
-
584
-		if ($diff < 60) { // first minute
585
-			return  $diff . " seconds ago";
586
-		} elseif ($diff < 3600) { //first hour
587
-			return round($diff / 60) . " minutes ago";
588
-		} elseif ($diff < 86400) { // first day
589
-			return round($diff / 3600) . " hours ago";
590
-		} elseif ($diff < 604800) { //first week
591
-			return round($diff / 86400) . " days ago";
592
-		} elseif ($diff < 2419200) { //first month
593
-			return round($diff / 604800) . " weeks ago";
594
-		} elseif ($diff < 29030400) { // first year
595
-			return round($diff / 2419200) . " months ago";
596
-		} else {
597
-			return round($diff / 29030400) . " years ago";
598
-		}
599
-	}
600
-
601
-	/**
602
-	 * returns all stored file versions from a given user
603
-	 * @param string $uid id of the user
604
-	 * @return array with contains two arrays 'all' which contains all versions sorted by age and 'by_file' which contains all versions sorted by filename
605
-	 */
606
-	private static function getAllVersions($uid) {
607
-		$view = new View('/' . $uid . '/');
608
-		$dirs = [self::VERSIONS_ROOT];
609
-		$versions = [];
610
-
611
-		while (!empty($dirs)) {
612
-			$dir = array_pop($dirs);
613
-			$files = $view->getDirectoryContent($dir);
614
-
615
-			foreach ($files as $file) {
616
-				$fileData = $file->getData();
617
-				$filePath = $dir . '/' . $fileData['name'];
618
-				if ($file['type'] === 'dir') {
619
-					$dirs[] = $filePath;
620
-				} else {
621
-					$versionsBegin = strrpos($filePath, '.v');
622
-					$relPathStart = strlen(self::VERSIONS_ROOT);
623
-					$version = substr($filePath, $versionsBegin + 2);
624
-					$relpath = substr($filePath, $relPathStart, $versionsBegin - $relPathStart);
625
-					$key = $version . '#' . $relpath;
626
-					$versions[$key] = ['path' => $relpath, 'timestamp' => $version];
627
-				}
628
-			}
629
-		}
630
-
631
-		// newest version first
632
-		krsort($versions);
633
-
634
-		$result = [
635
-			'all' => [],
636
-			'by_file' => [],
637
-		];
638
-
639
-		foreach ($versions as $key => $value) {
640
-			$size = $view->filesize(self::VERSIONS_ROOT.'/'.$value['path'].'.v'.$value['timestamp']);
641
-			$filename = $value['path'];
642
-
643
-			$result['all'][$key]['version'] = $value['timestamp'];
644
-			$result['all'][$key]['path'] = $filename;
645
-			$result['all'][$key]['size'] = $size;
646
-
647
-			$result['by_file'][$filename][$key]['version'] = $value['timestamp'];
648
-			$result['by_file'][$filename][$key]['path'] = $filename;
649
-			$result['by_file'][$filename][$key]['size'] = $size;
650
-		}
651
-
652
-		return $result;
653
-	}
654
-
655
-	/**
656
-	 * get list of files we want to expire
657
-	 * @param array $versions list of versions
658
-	 * @param integer $time
659
-	 * @param bool $quotaExceeded is versions storage limit reached
660
-	 * @return array containing the list of to deleted versions and the size of them
661
-	 */
662
-	protected static function getExpireList($time, $versions, $quotaExceeded = false) {
663
-		$expiration = self::getExpiration();
664
-
665
-		if ($expiration->shouldAutoExpire()) {
666
-			[$toDelete, $size] = self::getAutoExpireList($time, $versions);
667
-		} else {
668
-			$size = 0;
669
-			$toDelete = [];  // versions we want to delete
670
-		}
671
-
672
-		foreach ($versions as $key => $version) {
673
-			if ($expiration->isExpired($version['version'], $quotaExceeded) && !isset($toDelete[$key])) {
674
-				$size += $version['size'];
675
-				$toDelete[$key] = $version['path'] . '.v' . $version['version'];
676
-			}
677
-		}
678
-
679
-		return [$toDelete, $size];
680
-	}
681
-
682
-	/**
683
-	 * get list of files we want to expire
684
-	 * @param array $versions list of versions
685
-	 * @param integer $time
686
-	 * @return array containing the list of to deleted versions and the size of them
687
-	 */
688
-	protected static function getAutoExpireList($time, $versions) {
689
-		$size = 0;
690
-		$toDelete = [];  // versions we want to delete
691
-
692
-		$interval = 1;
693
-		$step = Storage::$max_versions_per_interval[$interval]['step'];
694
-		if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
695
-			$nextInterval = -1;
696
-		} else {
697
-			$nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
698
-		}
699
-
700
-		$firstVersion = reset($versions);
701
-
702
-		if ($firstVersion === false) {
703
-			return [$toDelete, $size];
704
-		}
705
-
706
-		$firstKey = key($versions);
707
-		$prevTimestamp = $firstVersion['version'];
708
-		$nextVersion = $firstVersion['version'] - $step;
709
-		unset($versions[$firstKey]);
710
-
711
-		foreach ($versions as $key => $version) {
712
-			$newInterval = true;
713
-			while ($newInterval) {
714
-				if ($nextInterval === -1 || $prevTimestamp > $nextInterval) {
715
-					if ($version['version'] > $nextVersion) {
716
-						//distance between two version too small, mark to delete
717
-						$toDelete[$key] = $version['path'] . '.v' . $version['version'];
718
-						$size += $version['size'];
719
-						\OC::$server->get(LoggerInterface::class)->info('Mark to expire '. $version['path'] .' next version should be ' . $nextVersion . " or smaller. (prevTimestamp: " . $prevTimestamp . "; step: " . $step, ['app' => 'files_versions']);
720
-					} else {
721
-						$nextVersion = $version['version'] - $step;
722
-						$prevTimestamp = $version['version'];
723
-					}
724
-					$newInterval = false; // version checked so we can move to the next one
725
-				} else { // time to move on to the next interval
726
-					$interval++;
727
-					$step = Storage::$max_versions_per_interval[$interval]['step'];
728
-					$nextVersion = $prevTimestamp - $step;
729
-					if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
730
-						$nextInterval = -1;
731
-					} else {
732
-						$nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
733
-					}
734
-					$newInterval = true; // we changed the interval -> check same version with new interval
735
-				}
736
-			}
737
-		}
738
-
739
-		return [$toDelete, $size];
740
-	}
741
-
742
-	/**
743
-	 * Schedule versions expiration for the given file
744
-	 *
745
-	 * @param string $uid owner of the file
746
-	 * @param string $fileName file/folder for which to schedule expiration
747
-	 */
748
-	public static function scheduleExpire($uid, $fileName) {
749
-		// let the admin disable auto expire
750
-		$expiration = self::getExpiration();
751
-		if ($expiration->isEnabled()) {
752
-			$command = new Expire($uid, $fileName);
753
-			/** @var IBus $bus */
754
-			$bus = \OC::$server->get(IBus::class);
755
-			$bus->push($command);
756
-		}
757
-	}
758
-
759
-	/**
760
-	 * Expire versions which exceed the quota.
761
-	 *
762
-	 * This will setup the filesystem for the given user but will not
763
-	 * tear it down afterwards.
764
-	 *
765
-	 * @param string $filename path to file to expire
766
-	 * @param string $uid user for which to expire the version
767
-	 * @return bool|int|null
768
-	 */
769
-	public static function expire($filename, $uid) {
770
-		$expiration = self::getExpiration();
771
-
772
-		/** @var LoggerInterface $logger */
773
-		$logger = \OC::$server->get(LoggerInterface::class);
774
-
775
-		if ($expiration->isEnabled()) {
776
-			// get available disk space for user
777
-			$user = \OC::$server->get(IUserManager::class)->get($uid);
778
-			if (is_null($user)) {
779
-				$logger->error('Backends provided no user object for ' . $uid, ['app' => 'files_versions']);
780
-				throw new \OC\User\NoUserException('Backends provided no user object for ' . $uid);
781
-			}
782
-
783
-			\OC_Util::setupFS($uid);
784
-
785
-			try {
786
-				if (!Filesystem::file_exists($filename)) {
787
-					return false;
788
-				}
789
-			} catch (StorageNotAvailableException $e) {
790
-				// if we can't check that the file hasn't been deleted we can only assume that it hasn't
791
-				// note that this `StorageNotAvailableException` is about the file the versions originate from,
792
-				// not the storage that the versions are stored on
793
-			}
794
-
795
-			if (empty($filename)) {
796
-				// file maybe renamed or deleted
797
-				return false;
798
-			}
799
-			$versionsFileview = new View('/'.$uid.'/files_versions');
800
-
801
-			$softQuota = true;
802
-			$quota = $user->getQuota();
803
-			if ($quota === null || $quota === 'none') {
804
-				$quota = Filesystem::free_space('/');
805
-				$softQuota = false;
806
-			} else {
807
-				$quota = \OCP\Util::computerFileSize($quota);
808
-			}
809
-
810
-			// make sure that we have the current size of the version history
811
-			$versionsSize = self::getVersionsSize($uid);
812
-
813
-			// calculate available space for version history
814
-			// subtract size of files and current versions size from quota
815
-			if ($quota >= 0) {
816
-				if ($softQuota) {
817
-					$root = \OC::$server->get(IRootFolder::class);
818
-					$userFolder = $root->getUserFolder($uid);
819
-					if (is_null($userFolder)) {
820
-						$availableSpace = 0;
821
-					} else {
822
-						$free = $quota - $userFolder->getSize(false); // remaining free space for user
823
-						if ($free > 0) {
824
-							$availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $versionsSize; // how much space can be used for versions
825
-						} else {
826
-							$availableSpace = $free - $versionsSize;
827
-						}
828
-					}
829
-				} else {
830
-					$availableSpace = $quota;
831
-				}
832
-			} else {
833
-				$availableSpace = PHP_INT_MAX;
834
-			}
835
-
836
-			$allVersions = Storage::getVersions($uid, $filename);
837
-
838
-			$time = time();
839
-			[$toDelete, $sizeOfDeletedVersions] = self::getExpireList($time, $allVersions, $availableSpace <= 0);
840
-
841
-			$availableSpace = $availableSpace + $sizeOfDeletedVersions;
842
-			$versionsSize = $versionsSize - $sizeOfDeletedVersions;
843
-
844
-			// if still not enough free space we rearrange the versions from all files
845
-			if ($availableSpace <= 0) {
846
-				$result = self::getAllVersions($uid);
847
-				$allVersions = $result['all'];
848
-
849
-				foreach ($result['by_file'] as $versions) {
850
-					[$toDeleteNew, $size] = self::getExpireList($time, $versions, $availableSpace <= 0);
851
-					$toDelete = array_merge($toDelete, $toDeleteNew);
852
-					$sizeOfDeletedVersions += $size;
853
-				}
854
-				$availableSpace = $availableSpace + $sizeOfDeletedVersions;
855
-				$versionsSize = $versionsSize - $sizeOfDeletedVersions;
856
-			}
857
-
858
-			foreach ($toDelete as $key => $path) {
859
-				\OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]);
860
-				self::deleteVersion($versionsFileview, $path);
861
-				\OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]);
862
-				unset($allVersions[$key]); // update array with the versions we keep
863
-				$logger->info('Expire: ' . $path, ['app' => 'files_versions']);
864
-			}
865
-
866
-			// Check if enough space is available after versions are rearranged.
867
-			// If not we delete the oldest versions until we meet the size limit for versions,
868
-			// but always keep the two latest versions
869
-			$numOfVersions = count($allVersions) - 2 ;
870
-			$i = 0;
871
-			// sort oldest first and make sure that we start at the first element
872
-			ksort($allVersions);
873
-			reset($allVersions);
874
-			while ($availableSpace < 0 && $i < $numOfVersions) {
875
-				$version = current($allVersions);
876
-				\OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]);
877
-				self::deleteVersion($versionsFileview, $version['path'] . '.v' . $version['version']);
878
-				\OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]);
879
-				$logger->info('running out of space! Delete oldest version: ' . $version['path'].'.v'.$version['version'], ['app' => 'files_versions']);
880
-				$versionsSize -= $version['size'];
881
-				$availableSpace += $version['size'];
882
-				next($allVersions);
883
-				$i++;
884
-			}
885
-
886
-			return $versionsSize; // finally return the new size of the version history
887
-		}
888
-
889
-		return false;
890
-	}
891
-
892
-	/**
893
-	 * Create recursively missing directories inside of files_versions
894
-	 * that match the given path to a file.
895
-	 *
896
-	 * @param string $filename $path to a file, relative to the user's
897
-	 * "files" folder
898
-	 * @param View $view view on data/user/
899
-	 */
900
-	public static function createMissingDirectories($filename, $view) {
901
-		$dirname = Filesystem::normalizePath(dirname($filename));
902
-		$dirParts = explode('/', $dirname);
903
-		$dir = "/files_versions";
904
-		foreach ($dirParts as $part) {
905
-			$dir = $dir . '/' . $part;
906
-			if (!$view->file_exists($dir)) {
907
-				$view->mkdir($dir);
908
-			}
909
-		}
910
-	}
911
-
912
-	/**
913
-	 * Static workaround
914
-	 * @return Expiration
915
-	 */
916
-	protected static function getExpiration() {
917
-		if (self::$application === null) {
918
-			self::$application = \OC::$server->get(Application::class);
919
-		}
920
-		return self::$application->getContainer()->get(Expiration::class);
921
-	}
72
+    public const DEFAULTENABLED = true;
73
+    public const DEFAULTMAXSIZE = 50; // unit: percentage; 50% of available disk space/quota
74
+    public const VERSIONS_ROOT = 'files_versions/';
75
+
76
+    public const DELETE_TRIGGER_MASTER_REMOVED = 0;
77
+    public const DELETE_TRIGGER_RETENTION_CONSTRAINT = 1;
78
+    public const DELETE_TRIGGER_QUOTA_EXCEEDED = 2;
79
+
80
+    // files for which we can remove the versions after the delete operation was successful
81
+    private static $deletedFiles = [];
82
+
83
+    private static $sourcePathAndUser = [];
84
+
85
+    private static $max_versions_per_interval = [
86
+        //first 10sec, one version every 2sec
87
+        1 => ['intervalEndsAfter' => 10,      'step' => 2],
88
+        //next minute, one version every 10sec
89
+        2 => ['intervalEndsAfter' => 60,      'step' => 10],
90
+        //next hour, one version every minute
91
+        3 => ['intervalEndsAfter' => 3600,    'step' => 60],
92
+        //next 24h, one version every hour
93
+        4 => ['intervalEndsAfter' => 86400,   'step' => 3600],
94
+        //next 30days, one version per day
95
+        5 => ['intervalEndsAfter' => 2592000, 'step' => 86400],
96
+        //until the end one version per week
97
+        6 => ['intervalEndsAfter' => -1,      'step' => 604800],
98
+    ];
99
+
100
+    /** @var \OCA\Files_Versions\AppInfo\Application */
101
+    private static $application;
102
+
103
+    /**
104
+     * get the UID of the owner of the file and the path to the file relative to
105
+     * owners files folder
106
+     *
107
+     * @param string $filename
108
+     * @return array
109
+     * @throws \OC\User\NoUserException
110
+     */
111
+    public static function getUidAndFilename($filename) {
112
+        $uid = Filesystem::getOwner($filename);
113
+        $userManager = \OC::$server->get(IUserManager::class);
114
+        // if the user with the UID doesn't exists, e.g. because the UID points
115
+        // to a remote user with a federated cloud ID we use the current logged-in
116
+        // user. We need a valid local user to create the versions
117
+        if (!$userManager->userExists($uid)) {
118
+            $uid = OC_User::getUser();
119
+        }
120
+        Filesystem::initMountPoints($uid);
121
+        if ($uid !== OC_User::getUser()) {
122
+            $info = Filesystem::getFileInfo($filename);
123
+            $ownerView = new View('/'.$uid.'/files');
124
+            try {
125
+                $filename = $ownerView->getPath($info['fileid']);
126
+                // make sure that the file name doesn't end with a trailing slash
127
+                // can for example happen single files shared across servers
128
+                $filename = rtrim($filename, '/');
129
+            } catch (NotFoundException $e) {
130
+                $filename = null;
131
+            }
132
+        }
133
+        return [$uid, $filename];
134
+    }
135
+
136
+    /**
137
+     * Remember the owner and the owner path of the source file
138
+     *
139
+     * @param string $source source path
140
+     */
141
+    public static function setSourcePathAndUser($source) {
142
+        [$uid, $path] = self::getUidAndFilename($source);
143
+        self::$sourcePathAndUser[$source] = ['uid' => $uid, 'path' => $path];
144
+    }
145
+
146
+    /**
147
+     * Gets the owner and the owner path from the source path
148
+     *
149
+     * @param string $source source path
150
+     * @return array with user id and path
151
+     */
152
+    public static function getSourcePathAndUser($source) {
153
+        if (isset(self::$sourcePathAndUser[$source])) {
154
+            $uid = self::$sourcePathAndUser[$source]['uid'];
155
+            $path = self::$sourcePathAndUser[$source]['path'];
156
+            unset(self::$sourcePathAndUser[$source]);
157
+        } else {
158
+            $uid = $path = false;
159
+        }
160
+        return [$uid, $path];
161
+    }
162
+
163
+    /**
164
+     * get current size of all versions from a given user
165
+     *
166
+     * @param string $user user who owns the versions
167
+     * @return int versions size
168
+     */
169
+    private static function getVersionsSize($user) {
170
+        $view = new View('/' . $user);
171
+        $fileInfo = $view->getFileInfo('/files_versions');
172
+        return isset($fileInfo['size']) ? $fileInfo['size'] : 0;
173
+    }
174
+
175
+    /**
176
+     * store a new version of a file.
177
+     */
178
+    public static function store($filename) {
179
+
180
+        // if the file gets streamed we need to remove the .part extension
181
+        // to get the right target
182
+        $ext = pathinfo($filename, PATHINFO_EXTENSION);
183
+        if ($ext === 'part') {
184
+            $filename = substr($filename, 0, -5);
185
+        }
186
+
187
+        // we only handle existing files
188
+        if (! Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) {
189
+            return false;
190
+        }
191
+
192
+        // since hook paths are always relative to the "default filesystem view"
193
+        // we always use the owner from there to get the full node
194
+        $uid = Filesystem::getView()->getOwner('');
195
+
196
+        /** @var IRootFolder $rootFolder */
197
+        $rootFolder = \OC::$server->get(IRootFolder::class);
198
+        $userFolder = $rootFolder->getUserFolder($uid);
199
+
200
+        $eventDispatcher = \OC::$server->get(IEventDispatcher::class);
201
+        try {
202
+            $file = $userFolder->get($filename);
203
+        } catch (NotFoundException $e) {
204
+            return false;
205
+        }
206
+
207
+        $mount = $file->getMountPoint();
208
+        if ($mount instanceof SharedMount) {
209
+            $ownerFolder = $rootFolder->getUserFolder($mount->getShare()->getShareOwner());
210
+            $ownerNodes = $ownerFolder->getById($file->getId());
211
+            if (count($ownerNodes)) {
212
+                $file = current($ownerNodes);
213
+                $uid = $mount->getShare()->getShareOwner();
214
+            }
215
+        }
216
+
217
+        /** @var IUserManager $userManager */
218
+        $userManager = \OC::$server->get(IUserManager::class);
219
+        $user = $userManager->get($uid);
220
+
221
+        if (!$user) {
222
+            return false;
223
+        }
224
+
225
+        // no use making versions for empty files
226
+        if ($file->getSize() === 0) {
227
+            return false;
228
+        }
229
+
230
+        $event = new CreateVersionEvent($file);
231
+        $eventDispatcher->dispatch('OCA\Files_Versions::createVersion', $event);
232
+        if ($event->shouldCreateVersion() === false) {
233
+            return false;
234
+        }
235
+
236
+        /** @var IVersionManager $versionManager */
237
+        $versionManager = \OC::$server->get(IVersionManager::class);
238
+
239
+        $versionManager->createVersion($user, $file);
240
+    }
241
+
242
+
243
+    /**
244
+     * mark file as deleted so that we can remove the versions if the file is gone
245
+     * @param string $path
246
+     */
247
+    public static function markDeletedFile($path) {
248
+        [$uid, $filename] = self::getUidAndFilename($path);
249
+        self::$deletedFiles[$path] = [
250
+            'uid' => $uid,
251
+            'filename' => $filename];
252
+    }
253
+
254
+    /**
255
+     * delete the version from the storage and cache
256
+     *
257
+     * @param View $view
258
+     * @param string $path
259
+     */
260
+    protected static function deleteVersion($view, $path) {
261
+        $view->unlink($path);
262
+        /**
263
+         * @var \OC\Files\Storage\Storage $storage
264
+         * @var string $internalPath
265
+         */
266
+        [$storage, $internalPath] = $view->resolvePath($path);
267
+        $cache = $storage->getCache($internalPath);
268
+        $cache->remove($internalPath);
269
+    }
270
+
271
+    /**
272
+     * Delete versions of a file
273
+     */
274
+    public static function delete($path) {
275
+        $deletedFile = self::$deletedFiles[$path];
276
+        $uid = $deletedFile['uid'];
277
+        $filename = $deletedFile['filename'];
278
+
279
+        if (!Filesystem::file_exists($path)) {
280
+            $view = new View('/' . $uid . '/files_versions');
281
+
282
+            $versions = self::getVersions($uid, $filename);
283
+            if (!empty($versions)) {
284
+                foreach ($versions as $v) {
285
+                    \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]);
286
+                    self::deleteVersion($view, $filename . '.v' . $v['version']);
287
+                    \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED]);
288
+                }
289
+            }
290
+        }
291
+        unset(self::$deletedFiles[$path]);
292
+    }
293
+
294
+    /**
295
+     * Rename or copy versions of a file of the given paths
296
+     *
297
+     * @param string $sourcePath source path of the file to move, relative to
298
+     * the currently logged in user's "files" folder
299
+     * @param string $targetPath target path of the file to move, relative to
300
+     * the currently logged in user's "files" folder
301
+     * @param string $operation can be 'copy' or 'rename'
302
+     */
303
+    public static function renameOrCopy($sourcePath, $targetPath, $operation) {
304
+        [$sourceOwner, $sourcePath] = self::getSourcePathAndUser($sourcePath);
305
+
306
+        // it was a upload of a existing file if no old path exists
307
+        // in this case the pre-hook already called the store method and we can
308
+        // stop here
309
+        if ($sourcePath === false) {
310
+            return true;
311
+        }
312
+
313
+        [$targetOwner, $targetPath] = self::getUidAndFilename($targetPath);
314
+
315
+        $sourcePath = ltrim($sourcePath, '/');
316
+        $targetPath = ltrim($targetPath, '/');
317
+
318
+        $rootView = new View('');
319
+
320
+        // did we move a directory ?
321
+        if ($rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
322
+            // does the directory exists for versions too ?
323
+            if ($rootView->is_dir('/' . $sourceOwner . '/files_versions/' . $sourcePath)) {
324
+                // create missing dirs if necessary
325
+                self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
326
+
327
+                // move the directory containing the versions
328
+                $rootView->$operation(
329
+                    '/' . $sourceOwner . '/files_versions/' . $sourcePath,
330
+                    '/' . $targetOwner . '/files_versions/' . $targetPath
331
+                );
332
+            }
333
+        } elseif ($versions = Storage::getVersions($sourceOwner, '/' . $sourcePath)) {
334
+            // create missing dirs if necessary
335
+            self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
336
+
337
+            foreach ($versions as $v) {
338
+                // move each version one by one to the target directory
339
+                $rootView->$operation(
340
+                    '/' . $sourceOwner . '/files_versions/' . $sourcePath.'.v' . $v['version'],
341
+                    '/' . $targetOwner . '/files_versions/' . $targetPath.'.v'.$v['version']
342
+                );
343
+            }
344
+        }
345
+
346
+        // if we moved versions directly for a file, schedule expiration check for that file
347
+        if (!$rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
348
+            self::scheduleExpire($targetOwner, $targetPath);
349
+        }
350
+    }
351
+
352
+    /**
353
+     * Rollback to an old version of a file.
354
+     *
355
+     * @param string $file file name
356
+     * @param int $revision revision timestamp
357
+     * @return bool
358
+     */
359
+    public static function rollback(string $file, int $revision, IUser $user) {
360
+
361
+        // add expected leading slash
362
+        $filename = '/' . ltrim($file, '/');
363
+
364
+        // Fetch the userfolder to trigger view hooks
365
+        $root = \OC::$server->get(IRootFolder::class);
366
+        $userFolder = $root->getUserFolder($user->getUID());
367
+
368
+        $users_view = new View('/'.$user->getUID());
369
+        $files_view = new View('/'. $user->getUID().'/files');
370
+
371
+        $versionCreated = false;
372
+
373
+        $fileInfo = $files_view->getFileInfo($file);
374
+
375
+        // check if user has the permissions to revert a version
376
+        if (!$fileInfo->isUpdateable()) {
377
+            return false;
378
+        }
379
+
380
+        //first create a new version
381
+        $version = 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename);
382
+        if (!$users_view->file_exists($version)) {
383
+            $users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename));
384
+            $versionCreated = true;
385
+        }
386
+
387
+        $fileToRestore = 'files_versions' . $filename . '.v' . $revision;
388
+
389
+        // Restore encrypted version of the old file for the newly restored file
390
+        // This has to happen manually here since the file is manually copied below
391
+        $oldVersion = $users_view->getFileInfo($fileToRestore)->getEncryptedVersion();
392
+        $oldFileInfo = $users_view->getFileInfo($fileToRestore);
393
+        $cache = $fileInfo->getStorage()->getCache();
394
+        $cache->update(
395
+            $fileInfo->getId(), [
396
+                'encrypted' => $oldVersion,
397
+                'encryptedVersion' => $oldVersion,
398
+                'size' => $oldFileInfo->getSize()
399
+            ]
400
+        );
401
+
402
+        // rollback
403
+        if (self::copyFileContents($users_view, $fileToRestore, 'files' . $filename)) {
404
+            $files_view->touch($file, $revision);
405
+            Storage::scheduleExpire($user->getUID(), $file);
406
+
407
+            $node = $userFolder->get($file);
408
+
409
+            // TODO: move away from those legacy hooks!
410
+            \OC_Hook::emit('\OCP\Versions', 'rollback', [
411
+                'path' => $filename,
412
+                'revision' => $revision,
413
+                'node' => $node,
414
+            ]);
415
+            return true;
416
+        } elseif ($versionCreated) {
417
+            self::deleteVersion($users_view, $version);
418
+        }
419
+
420
+        return false;
421
+    }
422
+
423
+    /**
424
+     * Stream copy file contents from $path1 to $path2
425
+     *
426
+     * @param View $view view to use for copying
427
+     * @param string $path1 source file to copy
428
+     * @param string $path2 target file
429
+     *
430
+     * @return bool true for success, false otherwise
431
+     */
432
+    private static function copyFileContents($view, $path1, $path2) {
433
+        /** @var \OC\Files\Storage\Storage $storage1 */
434
+        [$storage1, $internalPath1] = $view->resolvePath($path1);
435
+        /** @var \OC\Files\Storage\Storage $storage2 */
436
+        [$storage2, $internalPath2] = $view->resolvePath($path2);
437
+
438
+        $view->lockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
439
+        $view->lockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
440
+
441
+        // TODO add a proper way of overwriting a file while maintaining file ids
442
+        if ($storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage') || $storage2->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')) {
443
+            $source = $storage1->fopen($internalPath1, 'r');
444
+            $target = $storage2->fopen($internalPath2, 'w');
445
+            [, $result] = \OC_Helper::streamCopy($source, $target);
446
+            fclose($source);
447
+            fclose($target);
448
+
449
+            if ($result !== false) {
450
+                $storage1->unlink($internalPath1);
451
+            }
452
+        } else {
453
+            $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
454
+        }
455
+
456
+        $view->unlockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
457
+        $view->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
458
+
459
+        return ($result !== false);
460
+    }
461
+
462
+    /**
463
+     * get a list of all available versions of a file in descending chronological order
464
+     * @param string $uid user id from the owner of the file
465
+     * @param string $filename file to find versions of, relative to the user files dir
466
+     * @param string $userFullPath
467
+     * @return array versions newest version first
468
+     */
469
+    public static function getVersions($uid, $filename, $userFullPath = '') {
470
+        $versions = [];
471
+        if (empty($filename)) {
472
+            return $versions;
473
+        }
474
+        // fetch for old versions
475
+        $view = new View('/' . $uid . '/');
476
+
477
+        $pathinfo = pathinfo($filename);
478
+        $versionedFile = $pathinfo['basename'];
479
+
480
+        $dir = Filesystem::normalizePath(self::VERSIONS_ROOT . '/' . $pathinfo['dirname']);
481
+
482
+        $dirContent = false;
483
+        if ($view->is_dir($dir)) {
484
+            $dirContent = $view->opendir($dir);
485
+        }
486
+
487
+        if ($dirContent === false) {
488
+            return $versions;
489
+        }
490
+
491
+        if (is_resource($dirContent)) {
492
+            while (($entryName = readdir($dirContent)) !== false) {
493
+                if (!Filesystem::isIgnoredDir($entryName)) {
494
+                    $pathparts = pathinfo($entryName);
495
+                    $filename = $pathparts['filename'];
496
+                    if ($filename === $versionedFile) {
497
+                        $pathparts = pathinfo($entryName);
498
+                        $timestamp = substr($pathparts['extension'], 1);
499
+                        $filename = $pathparts['filename'];
500
+                        $key = $timestamp . '#' . $filename;
501
+                        $versions[$key]['version'] = $timestamp;
502
+                        $versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($timestamp);
503
+                        if (empty($userFullPath)) {
504
+                            $versions[$key]['preview'] = '';
505
+                        } else {
506
+                            /** @var IURLGenerator $urlGenerator */
507
+                            $urlGenerator = \OC::$server->get(IURLGenerator::class);
508
+                            $versions[$key]['preview'] = $urlGenerator->linkToRoute('files_version.Preview.getPreview',
509
+                                ['file' => $userFullPath, 'version' => $timestamp]);
510
+                        }
511
+                        $versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename);
512
+                        $versions[$key]['name'] = $versionedFile;
513
+                        $versions[$key]['size'] = $view->filesize($dir . '/' . $entryName);
514
+                        $versions[$key]['mimetype'] = \OC::$server->get(IMimeTypeDetector::class)->detectPath($versionedFile);
515
+                    }
516
+                }
517
+            }
518
+            closedir($dirContent);
519
+        }
520
+
521
+        // sort with newest version first
522
+        krsort($versions);
523
+
524
+        return $versions;
525
+    }
526
+
527
+    /**
528
+     * Expire versions that older than max version retention time
529
+     *
530
+     * @param string $uid
531
+     */
532
+    public static function expireOlderThanMaxForUser($uid) {
533
+        /** @var IRootFolder $root */
534
+        $root = \OC::$server->get(IRootFolder::class);
535
+        try {
536
+            /** @var Folder $versionsRoot */
537
+            $versionsRoot = $root->get('/' . $uid . '/files_versions');
538
+        } catch (NotFoundException $e) {
539
+            return;
540
+        }
541
+
542
+        $expiration = self::getExpiration();
543
+        $threshold = $expiration->getMaxAgeAsTimestamp();
544
+        if (!$threshold) {
545
+            return;
546
+        }
547
+
548
+        $allVersions = $versionsRoot->search(new SearchQuery(
549
+            new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [
550
+                new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', FileInfo::MIMETYPE_FOLDER),
551
+            ]),
552
+            0,
553
+            0,
554
+            []
555
+        ));
556
+
557
+        /** @var Node[] $versions */
558
+        $versions = array_filter($allVersions, function (Node $info) use ($threshold) {
559
+            $versionsBegin = strrpos($info->getName(), '.v');
560
+            if ($versionsBegin === false) {
561
+                return false;
562
+            }
563
+            $version = (int)substr($info->getName(), $versionsBegin + 2);
564
+            return $version < $threshold;
565
+        });
566
+
567
+        foreach ($versions as $version) {
568
+            $internalPath = $version->getInternalPath();
569
+            \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $internalPath, 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT]);
570
+            $version->delete();
571
+            \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $internalPath, 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT]);
572
+        }
573
+    }
574
+
575
+    /**
576
+     * translate a timestamp into a string like "5 days ago"
577
+     *
578
+     * @param int $timestamp
579
+     * @return string for example "5 days ago"
580
+     */
581
+    private static function getHumanReadableTimestamp($timestamp) {
582
+        $diff = time() - $timestamp;
583
+
584
+        if ($diff < 60) { // first minute
585
+            return  $diff . " seconds ago";
586
+        } elseif ($diff < 3600) { //first hour
587
+            return round($diff / 60) . " minutes ago";
588
+        } elseif ($diff < 86400) { // first day
589
+            return round($diff / 3600) . " hours ago";
590
+        } elseif ($diff < 604800) { //first week
591
+            return round($diff / 86400) . " days ago";
592
+        } elseif ($diff < 2419200) { //first month
593
+            return round($diff / 604800) . " weeks ago";
594
+        } elseif ($diff < 29030400) { // first year
595
+            return round($diff / 2419200) . " months ago";
596
+        } else {
597
+            return round($diff / 29030400) . " years ago";
598
+        }
599
+    }
600
+
601
+    /**
602
+     * returns all stored file versions from a given user
603
+     * @param string $uid id of the user
604
+     * @return array with contains two arrays 'all' which contains all versions sorted by age and 'by_file' which contains all versions sorted by filename
605
+     */
606
+    private static function getAllVersions($uid) {
607
+        $view = new View('/' . $uid . '/');
608
+        $dirs = [self::VERSIONS_ROOT];
609
+        $versions = [];
610
+
611
+        while (!empty($dirs)) {
612
+            $dir = array_pop($dirs);
613
+            $files = $view->getDirectoryContent($dir);
614
+
615
+            foreach ($files as $file) {
616
+                $fileData = $file->getData();
617
+                $filePath = $dir . '/' . $fileData['name'];
618
+                if ($file['type'] === 'dir') {
619
+                    $dirs[] = $filePath;
620
+                } else {
621
+                    $versionsBegin = strrpos($filePath, '.v');
622
+                    $relPathStart = strlen(self::VERSIONS_ROOT);
623
+                    $version = substr($filePath, $versionsBegin + 2);
624
+                    $relpath = substr($filePath, $relPathStart, $versionsBegin - $relPathStart);
625
+                    $key = $version . '#' . $relpath;
626
+                    $versions[$key] = ['path' => $relpath, 'timestamp' => $version];
627
+                }
628
+            }
629
+        }
630
+
631
+        // newest version first
632
+        krsort($versions);
633
+
634
+        $result = [
635
+            'all' => [],
636
+            'by_file' => [],
637
+        ];
638
+
639
+        foreach ($versions as $key => $value) {
640
+            $size = $view->filesize(self::VERSIONS_ROOT.'/'.$value['path'].'.v'.$value['timestamp']);
641
+            $filename = $value['path'];
642
+
643
+            $result['all'][$key]['version'] = $value['timestamp'];
644
+            $result['all'][$key]['path'] = $filename;
645
+            $result['all'][$key]['size'] = $size;
646
+
647
+            $result['by_file'][$filename][$key]['version'] = $value['timestamp'];
648
+            $result['by_file'][$filename][$key]['path'] = $filename;
649
+            $result['by_file'][$filename][$key]['size'] = $size;
650
+        }
651
+
652
+        return $result;
653
+    }
654
+
655
+    /**
656
+     * get list of files we want to expire
657
+     * @param array $versions list of versions
658
+     * @param integer $time
659
+     * @param bool $quotaExceeded is versions storage limit reached
660
+     * @return array containing the list of to deleted versions and the size of them
661
+     */
662
+    protected static function getExpireList($time, $versions, $quotaExceeded = false) {
663
+        $expiration = self::getExpiration();
664
+
665
+        if ($expiration->shouldAutoExpire()) {
666
+            [$toDelete, $size] = self::getAutoExpireList($time, $versions);
667
+        } else {
668
+            $size = 0;
669
+            $toDelete = [];  // versions we want to delete
670
+        }
671
+
672
+        foreach ($versions as $key => $version) {
673
+            if ($expiration->isExpired($version['version'], $quotaExceeded) && !isset($toDelete[$key])) {
674
+                $size += $version['size'];
675
+                $toDelete[$key] = $version['path'] . '.v' . $version['version'];
676
+            }
677
+        }
678
+
679
+        return [$toDelete, $size];
680
+    }
681
+
682
+    /**
683
+     * get list of files we want to expire
684
+     * @param array $versions list of versions
685
+     * @param integer $time
686
+     * @return array containing the list of to deleted versions and the size of them
687
+     */
688
+    protected static function getAutoExpireList($time, $versions) {
689
+        $size = 0;
690
+        $toDelete = [];  // versions we want to delete
691
+
692
+        $interval = 1;
693
+        $step = Storage::$max_versions_per_interval[$interval]['step'];
694
+        if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
695
+            $nextInterval = -1;
696
+        } else {
697
+            $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
698
+        }
699
+
700
+        $firstVersion = reset($versions);
701
+
702
+        if ($firstVersion === false) {
703
+            return [$toDelete, $size];
704
+        }
705
+
706
+        $firstKey = key($versions);
707
+        $prevTimestamp = $firstVersion['version'];
708
+        $nextVersion = $firstVersion['version'] - $step;
709
+        unset($versions[$firstKey]);
710
+
711
+        foreach ($versions as $key => $version) {
712
+            $newInterval = true;
713
+            while ($newInterval) {
714
+                if ($nextInterval === -1 || $prevTimestamp > $nextInterval) {
715
+                    if ($version['version'] > $nextVersion) {
716
+                        //distance between two version too small, mark to delete
717
+                        $toDelete[$key] = $version['path'] . '.v' . $version['version'];
718
+                        $size += $version['size'];
719
+                        \OC::$server->get(LoggerInterface::class)->info('Mark to expire '. $version['path'] .' next version should be ' . $nextVersion . " or smaller. (prevTimestamp: " . $prevTimestamp . "; step: " . $step, ['app' => 'files_versions']);
720
+                    } else {
721
+                        $nextVersion = $version['version'] - $step;
722
+                        $prevTimestamp = $version['version'];
723
+                    }
724
+                    $newInterval = false; // version checked so we can move to the next one
725
+                } else { // time to move on to the next interval
726
+                    $interval++;
727
+                    $step = Storage::$max_versions_per_interval[$interval]['step'];
728
+                    $nextVersion = $prevTimestamp - $step;
729
+                    if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
730
+                        $nextInterval = -1;
731
+                    } else {
732
+                        $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
733
+                    }
734
+                    $newInterval = true; // we changed the interval -> check same version with new interval
735
+                }
736
+            }
737
+        }
738
+
739
+        return [$toDelete, $size];
740
+    }
741
+
742
+    /**
743
+     * Schedule versions expiration for the given file
744
+     *
745
+     * @param string $uid owner of the file
746
+     * @param string $fileName file/folder for which to schedule expiration
747
+     */
748
+    public static function scheduleExpire($uid, $fileName) {
749
+        // let the admin disable auto expire
750
+        $expiration = self::getExpiration();
751
+        if ($expiration->isEnabled()) {
752
+            $command = new Expire($uid, $fileName);
753
+            /** @var IBus $bus */
754
+            $bus = \OC::$server->get(IBus::class);
755
+            $bus->push($command);
756
+        }
757
+    }
758
+
759
+    /**
760
+     * Expire versions which exceed the quota.
761
+     *
762
+     * This will setup the filesystem for the given user but will not
763
+     * tear it down afterwards.
764
+     *
765
+     * @param string $filename path to file to expire
766
+     * @param string $uid user for which to expire the version
767
+     * @return bool|int|null
768
+     */
769
+    public static function expire($filename, $uid) {
770
+        $expiration = self::getExpiration();
771
+
772
+        /** @var LoggerInterface $logger */
773
+        $logger = \OC::$server->get(LoggerInterface::class);
774
+
775
+        if ($expiration->isEnabled()) {
776
+            // get available disk space for user
777
+            $user = \OC::$server->get(IUserManager::class)->get($uid);
778
+            if (is_null($user)) {
779
+                $logger->error('Backends provided no user object for ' . $uid, ['app' => 'files_versions']);
780
+                throw new \OC\User\NoUserException('Backends provided no user object for ' . $uid);
781
+            }
782
+
783
+            \OC_Util::setupFS($uid);
784
+
785
+            try {
786
+                if (!Filesystem::file_exists($filename)) {
787
+                    return false;
788
+                }
789
+            } catch (StorageNotAvailableException $e) {
790
+                // if we can't check that the file hasn't been deleted we can only assume that it hasn't
791
+                // note that this `StorageNotAvailableException` is about the file the versions originate from,
792
+                // not the storage that the versions are stored on
793
+            }
794
+
795
+            if (empty($filename)) {
796
+                // file maybe renamed or deleted
797
+                return false;
798
+            }
799
+            $versionsFileview = new View('/'.$uid.'/files_versions');
800
+
801
+            $softQuota = true;
802
+            $quota = $user->getQuota();
803
+            if ($quota === null || $quota === 'none') {
804
+                $quota = Filesystem::free_space('/');
805
+                $softQuota = false;
806
+            } else {
807
+                $quota = \OCP\Util::computerFileSize($quota);
808
+            }
809
+
810
+            // make sure that we have the current size of the version history
811
+            $versionsSize = self::getVersionsSize($uid);
812
+
813
+            // calculate available space for version history
814
+            // subtract size of files and current versions size from quota
815
+            if ($quota >= 0) {
816
+                if ($softQuota) {
817
+                    $root = \OC::$server->get(IRootFolder::class);
818
+                    $userFolder = $root->getUserFolder($uid);
819
+                    if (is_null($userFolder)) {
820
+                        $availableSpace = 0;
821
+                    } else {
822
+                        $free = $quota - $userFolder->getSize(false); // remaining free space for user
823
+                        if ($free > 0) {
824
+                            $availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $versionsSize; // how much space can be used for versions
825
+                        } else {
826
+                            $availableSpace = $free - $versionsSize;
827
+                        }
828
+                    }
829
+                } else {
830
+                    $availableSpace = $quota;
831
+                }
832
+            } else {
833
+                $availableSpace = PHP_INT_MAX;
834
+            }
835
+
836
+            $allVersions = Storage::getVersions($uid, $filename);
837
+
838
+            $time = time();
839
+            [$toDelete, $sizeOfDeletedVersions] = self::getExpireList($time, $allVersions, $availableSpace <= 0);
840
+
841
+            $availableSpace = $availableSpace + $sizeOfDeletedVersions;
842
+            $versionsSize = $versionsSize - $sizeOfDeletedVersions;
843
+
844
+            // if still not enough free space we rearrange the versions from all files
845
+            if ($availableSpace <= 0) {
846
+                $result = self::getAllVersions($uid);
847
+                $allVersions = $result['all'];
848
+
849
+                foreach ($result['by_file'] as $versions) {
850
+                    [$toDeleteNew, $size] = self::getExpireList($time, $versions, $availableSpace <= 0);
851
+                    $toDelete = array_merge($toDelete, $toDeleteNew);
852
+                    $sizeOfDeletedVersions += $size;
853
+                }
854
+                $availableSpace = $availableSpace + $sizeOfDeletedVersions;
855
+                $versionsSize = $versionsSize - $sizeOfDeletedVersions;
856
+            }
857
+
858
+            foreach ($toDelete as $key => $path) {
859
+                \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]);
860
+                self::deleteVersion($versionsFileview, $path);
861
+                \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]);
862
+                unset($allVersions[$key]); // update array with the versions we keep
863
+                $logger->info('Expire: ' . $path, ['app' => 'files_versions']);
864
+            }
865
+
866
+            // Check if enough space is available after versions are rearranged.
867
+            // If not we delete the oldest versions until we meet the size limit for versions,
868
+            // but always keep the two latest versions
869
+            $numOfVersions = count($allVersions) - 2 ;
870
+            $i = 0;
871
+            // sort oldest first and make sure that we start at the first element
872
+            ksort($allVersions);
873
+            reset($allVersions);
874
+            while ($availableSpace < 0 && $i < $numOfVersions) {
875
+                $version = current($allVersions);
876
+                \OC_Hook::emit('\OCP\Versions', 'preDelete', ['path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]);
877
+                self::deleteVersion($versionsFileview, $version['path'] . '.v' . $version['version']);
878
+                \OC_Hook::emit('\OCP\Versions', 'delete', ['path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED]);
879
+                $logger->info('running out of space! Delete oldest version: ' . $version['path'].'.v'.$version['version'], ['app' => 'files_versions']);
880
+                $versionsSize -= $version['size'];
881
+                $availableSpace += $version['size'];
882
+                next($allVersions);
883
+                $i++;
884
+            }
885
+
886
+            return $versionsSize; // finally return the new size of the version history
887
+        }
888
+
889
+        return false;
890
+    }
891
+
892
+    /**
893
+     * Create recursively missing directories inside of files_versions
894
+     * that match the given path to a file.
895
+     *
896
+     * @param string $filename $path to a file, relative to the user's
897
+     * "files" folder
898
+     * @param View $view view on data/user/
899
+     */
900
+    public static function createMissingDirectories($filename, $view) {
901
+        $dirname = Filesystem::normalizePath(dirname($filename));
902
+        $dirParts = explode('/', $dirname);
903
+        $dir = "/files_versions";
904
+        foreach ($dirParts as $part) {
905
+            $dir = $dir . '/' . $part;
906
+            if (!$view->file_exists($dir)) {
907
+                $view->mkdir($dir);
908
+            }
909
+        }
910
+    }
911
+
912
+    /**
913
+     * Static workaround
914
+     * @return Expiration
915
+     */
916
+    protected static function getExpiration() {
917
+        if (self::$application === null) {
918
+            self::$application = \OC::$server->get(Application::class);
919
+        }
920
+        return self::$application->getContainer()->get(Expiration::class);
921
+    }
922 922
 }
Please login to merge, or discard this patch.