Passed
Push — master ( c4d71c...15ef35 )
by Morris
13:37 queued 11s
created

LegacyVersionsBackend::getVersionFolder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
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_Versions\Versions;
23
24
use OC\Files\View;
25
use OCA\Files_Versions\Storage;
26
use OCP\Files\File;
27
use OCP\Files\FileInfo;
28
use OCP\Files\Folder;
29
use OCP\Files\IRootFolder;
30
use OCP\Files\NotFoundException;
31
use OCP\IUser;
32
33
class LegacyVersionsBackend implements IVersionBackend {
34
	/** @var IRootFolder */
35
	private $rootFolder;
36
37
	public function __construct(IRootFolder $rootFolder) {
38
		$this->rootFolder = $rootFolder;
39
	}
40
41
	public function getVersionsForFile(IUser $user, FileInfo $file): array {
42
		$userFolder = $this->rootFolder->getUserFolder($user->getUID());
43
		$versions = Storage::getVersions($user->getUID(), $userFolder->getRelativePath($file->getPath()));
44
45
		return array_map(function (array $data) use ($file, $user) {
46
			return new Version(
47
				(int)$data['version'],
48
				(int)$data['version'],
49
				$data['name'],
50
				(int)$data['size'],
51
				$data['mimetype'],
52
				$data['path'],
53
				$file,
54
				$this,
55
				$user
56
			);
57
		}, $versions);
58
	}
59
60
	public function createVersion(IUser $user, FileInfo $file) {
61
		$userFolder = $this->rootFolder->getUserFolder($user->getUID());
62
		$relativePath = $userFolder->getRelativePath($file->getPath());
63
		$userView = new View('/' . $user->getUID());
64
		// create all parent folders
65
		Storage::createMissingDirectories($relativePath, $userView);
66
67
		Storage::scheduleExpire($user->getUID(), $relativePath);
68
69
		// store a new version of a file
70
		$userView->copy('files/' . $relativePath, 'files_versions/' . $relativePath . '.v' . $file->getMtime());
71
		// ensure the file is scanned
72
		$userView->getFileInfo('files_versions/' . $relativePath . '.v' . $file->getMtime());
73
	}
74
75
	public function rollback(IVersion $version) {
76
		return Storage::rollback($version->getVersionPath(), $version->getRevisionId());
77
	}
78
79
	private function getVersionFolder(IUser $user): Folder {
80
		$userRoot = $this->rootFolder->getUserFolder($user->getUID())
81
			->getParent();
82
		try {
83
			/** @var Folder $folder */
84
			$folder = $userRoot->get('files_versions');
85
			return $folder;
86
		} catch (NotFoundException $e) {
87
			return $userRoot->newFolder('files_versions');
88
		}
89
	}
90
91
	public function read(IVersion $version) {
92
		$versions = $this->getVersionFolder($version->getUser());
93
		/** @var File $file */
94
		$file = $versions->get($version->getVersionPath() . '.v' . $version->getRevisionId());
95
		return $file->fopen('r');
96
	}
97
98
	public function getVersionFile(IUser $user, FileInfo $sourceFile, int $revision): File {
99
		$userFolder = $this->rootFolder->getUserFolder($user->getUID());
100
		$versionFolder = $this->getVersionFolder($user);
101
		/** @var File $file */
102
		$file = $versionFolder->get($userFolder->getRelativePath($sourceFile->getPath()) . '.v' . $revision);
103
		return $file;
104
	}
105
}
106