LegacyVersionsBackend.php 4.0 KB

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