You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LegacyVersionsBackend.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\Files_Sharing\SharedStorage;
  28. use OCA\Files_Versions\Storage;
  29. use OCP\Files\File;
  30. use OCP\Files\FileInfo;
  31. use OCP\Files\Folder;
  32. use OCP\Files\IRootFolder;
  33. use OCP\Files\NotFoundException;
  34. use OCP\Files\Storage\IStorage;
  35. use OCP\IUser;
  36. use OCP\IUserManager;
  37. class LegacyVersionsBackend implements IVersionBackend {
  38. /** @var IRootFolder */
  39. private $rootFolder;
  40. /** @var IUserManager */
  41. private $userManager;
  42. public function __construct(IRootFolder $rootFolder, IUserManager $userManager) {
  43. $this->rootFolder = $rootFolder;
  44. $this->userManager = $userManager;
  45. }
  46. public function useBackendForStorage(IStorage $storage): bool {
  47. return true;
  48. }
  49. public function getVersionsForFile(IUser $user, FileInfo $file): array {
  50. $storage = $file->getStorage();
  51. if ($storage->instanceOfStorage(SharedStorage::class)) {
  52. $owner = $storage->getOwner('');
  53. $user = $this->userManager->get($owner);
  54. }
  55. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  56. $nodes = $userFolder->getById($file->getId());
  57. $file2 = array_pop($nodes);
  58. $versions = Storage::getVersions($user->getUID(), $userFolder->getRelativePath($file2->getPath()));
  59. return array_map(function (array $data) use ($file, $user) {
  60. return new Version(
  61. (int)$data['version'],
  62. (int)$data['version'],
  63. $data['name'],
  64. (int)$data['size'],
  65. $data['mimetype'],
  66. $data['path'],
  67. $file,
  68. $this,
  69. $user
  70. );
  71. }, $versions);
  72. }
  73. public function createVersion(IUser $user, FileInfo $file) {
  74. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  75. $relativePath = $userFolder->getRelativePath($file->getPath());
  76. $userView = new View('/' . $user->getUID());
  77. // create all parent folders
  78. Storage::createMissingDirectories($relativePath, $userView);
  79. Storage::scheduleExpire($user->getUID(), $relativePath);
  80. // store a new version of a file
  81. $userView->copy('files/' . $relativePath, 'files_versions/' . $relativePath . '.v' . $file->getMtime());
  82. // ensure the file is scanned
  83. $userView->getFileInfo('files_versions/' . $relativePath . '.v' . $file->getMtime());
  84. }
  85. public function rollback(IVersion $version) {
  86. return Storage::rollback($version->getVersionPath(), $version->getRevisionId(), $version->getUser());
  87. }
  88. private function getVersionFolder(IUser $user): Folder {
  89. $userRoot = $this->rootFolder->getUserFolder($user->getUID())
  90. ->getParent();
  91. try {
  92. /** @var Folder $folder */
  93. $folder = $userRoot->get('files_versions');
  94. return $folder;
  95. } catch (NotFoundException $e) {
  96. return $userRoot->newFolder('files_versions');
  97. }
  98. }
  99. public function read(IVersion $version) {
  100. $versions = $this->getVersionFolder($version->getUser());
  101. /** @var File $file */
  102. $file = $versions->get($version->getVersionPath() . '.v' . $version->getRevisionId());
  103. return $file->fopen('r');
  104. }
  105. public function getVersionFile(IUser $user, FileInfo $sourceFile, $revision): File {
  106. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  107. $versionFolder = $this->getVersionFolder($user);
  108. /** @var File $file */
  109. $file = $versionFolder->get($userFolder->getRelativePath($sourceFile->getPath()) . '.v' . $revision);
  110. return $file;
  111. }
  112. }