1
0

LegacyVersionsBackend.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files_Versions\Versions;
  26. use OC\Files\View;
  27. use OCA\DAV\Connector\Sabre\Exception\Forbidden;
  28. use OCA\Files_Sharing\SharedStorage;
  29. use OCA\Files_Versions\Storage;
  30. use OCP\Files\File;
  31. use OCP\Files\FileInfo;
  32. use OCP\Files\Folder;
  33. use OCP\Files\IRootFolder;
  34. use OCP\Files\NotFoundException;
  35. use OCP\Files\Storage\IStorage;
  36. use OCP\IUser;
  37. use OCP\IUserManager;
  38. use OCP\IUserSession;
  39. class LegacyVersionsBackend implements IVersionBackend {
  40. /** @var IRootFolder */
  41. private $rootFolder;
  42. /** @var IUserManager */
  43. private $userManager;
  44. /** @var IUserSession */
  45. private $userSession;
  46. public function __construct(IRootFolder $rootFolder, IUserManager $userManager, IUserSession $userSession) {
  47. $this->rootFolder = $rootFolder;
  48. $this->userManager = $userManager;
  49. $this->userSession = $userSession;
  50. }
  51. public function useBackendForStorage(IStorage $storage): bool {
  52. return true;
  53. }
  54. public function getVersionsForFile(IUser $user, FileInfo $file): array {
  55. $storage = $file->getStorage();
  56. if ($storage->instanceOfStorage(SharedStorage::class)) {
  57. $owner = $storage->getOwner('');
  58. $user = $this->userManager->get($owner);
  59. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  60. $nodes = $userFolder->getById($file->getId());
  61. $file = array_pop($nodes);
  62. if (!$file) {
  63. throw new NotFoundException("version file not found for share owner");
  64. }
  65. }
  66. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  67. $nodes = $userFolder->getById($file->getId());
  68. $file2 = array_pop($nodes);
  69. $versions = Storage::getVersions($user->getUID(), $userFolder->getRelativePath($file2->getPath()));
  70. return array_map(function (array $data) use ($file, $user) {
  71. return new Version(
  72. (int)$data['version'],
  73. (int)$data['version'],
  74. $data['name'],
  75. (int)$data['size'],
  76. $data['mimetype'],
  77. $data['path'],
  78. $file,
  79. $this,
  80. $user
  81. );
  82. }, $versions);
  83. }
  84. public function createVersion(IUser $user, FileInfo $file) {
  85. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  86. $relativePath = $userFolder->getRelativePath($file->getPath());
  87. $userView = new View('/' . $user->getUID());
  88. // create all parent folders
  89. Storage::createMissingDirectories($relativePath, $userView);
  90. Storage::scheduleExpire($user->getUID(), $relativePath);
  91. // store a new version of a file
  92. $userView->copy('files/' . $relativePath, 'files_versions/' . $relativePath . '.v' . $file->getMtime());
  93. // ensure the file is scanned
  94. $userView->getFileInfo('files_versions/' . $relativePath . '.v' . $file->getMtime());
  95. }
  96. public function rollback(IVersion $version) {
  97. if (!$this->currentUserHasPermissions($version, \OCP\Constants::PERMISSION_UPDATE)) {
  98. throw new Forbidden('You cannot restore this version because you do not have update permissions on the source file.');
  99. }
  100. return Storage::rollback($version->getVersionPath(), $version->getRevisionId(), $version->getUser());
  101. }
  102. private function getVersionFolder(IUser $user): Folder {
  103. $userRoot = $this->rootFolder->getUserFolder($user->getUID())
  104. ->getParent();
  105. try {
  106. /** @var Folder $folder */
  107. $folder = $userRoot->get('files_versions');
  108. return $folder;
  109. } catch (NotFoundException $e) {
  110. return $userRoot->newFolder('files_versions');
  111. }
  112. }
  113. public function read(IVersion $version) {
  114. $versions = $this->getVersionFolder($version->getUser());
  115. /** @var File $file */
  116. $file = $versions->get($version->getVersionPath() . '.v' . $version->getRevisionId());
  117. return $file->fopen('r');
  118. }
  119. public function getVersionFile(IUser $user, FileInfo $sourceFile, $revision): File {
  120. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  121. $versionFolder = $this->getVersionFolder($user);
  122. /** @var File $file */
  123. $file = $versionFolder->get($userFolder->getRelativePath($sourceFile->getPath()) . '.v' . $revision);
  124. return $file;
  125. }
  126. private function currentUserHasPermissions(IVersion $version, int $permissions): bool {
  127. $sourceFile = $version->getSourceFile();
  128. $currentUserId = $this->userSession->getUser()->getUID();
  129. if ($currentUserId === null) {
  130. throw new NotFoundException("No user logged in");
  131. }
  132. if ($sourceFile->getOwner()->getUID() !== $currentUserId) {
  133. $nodes = $this->rootFolder->getUserFolder($currentUserId)->getById($sourceFile->getId());
  134. $sourceFile = array_pop($nodes);
  135. if (!$sourceFile) {
  136. throw new NotFoundException("Version file not accessible by current user");
  137. }
  138. }
  139. return ($sourceFile->getPermissions() & $permissions) === $permissions;
  140. }
  141. }