|
27 | 27 |
|
28 | 28 | namespace OCA\Files_Versions\Versions; |
29 | 29 |
|
| 30 | +use Exception; |
30 | 31 | use OC\Files\View; |
31 | 32 | use OCA\DAV\Connector\Sabre\Exception\Forbidden; |
32 | 33 | use OCA\Files_Sharing\ISharedStorage; |
|
46 | 47 | use OCP\IUserManager; |
47 | 48 | use OCP\IUserSession; |
48 | 49 |
|
49 | | -class LegacyVersionsBackend implements IVersionBackend, IDeletableVersionBackend, INeedSyncVersionBackend, IMetadataVersionBackend { |
| 50 | +class LegacyVersionsBackend implements IVersionBackend, IDeletableVersionBackend, INeedSyncVersionBackend, IMetadataVersionBackend, IVersionsImporterBackend { |
50 | 51 | public function __construct( |
51 | 52 | private IRootFolder $rootFolder, |
52 | 53 | private IUserManager $userManager, |
@@ -296,4 +297,69 @@ public function setMetadataValue(Node $node, int $revision, string $key, string |
296 | 297 | $versionEntity->setMetadataValue($key, $value); |
297 | 298 | $this->versionsMapper->update($versionEntity); |
298 | 299 | } |
| 300 | + |
| 301 | + |
| 302 | + /** |
| 303 | + * @inheritdoc |
| 304 | + */ |
| 305 | + public function importVersionsForFile(IUser $user, Node $source, Node $target, array $versions): void { |
| 306 | + $userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
| 307 | + $relativePath = $userFolder->getRelativePath($target->getPath()); |
| 308 | + |
| 309 | + if ($relativePath === null) { |
| 310 | + throw new \Exception('Target does not have a relative path' . $target->getPath()); |
| 311 | + } |
| 312 | + |
| 313 | + $userView = new View('/' . $user->getUID()); |
| 314 | + // create all parent folders |
| 315 | + // Storage::createMissingDirectories($relativePath, $userView); |
| 316 | + Storage::scheduleExpire($user->getUID(), $relativePath); |
| 317 | + |
| 318 | + foreach ($versions as $version) { |
| 319 | + // 1. Move the file to the new location |
| 320 | + if ($version->getTimestamp() !== $source->getMTime()) { |
| 321 | + $backend = $version->getBackend(); |
| 322 | + $versionFile = $backend->getVersionFile($user, $source, $version->getRevisionId()); |
| 323 | + $newVersionPath = 'files_versions/' . $relativePath . '.v' . $version->getTimestamp(); |
| 324 | + |
| 325 | + $versionContent = $versionFile->fopen('r'); |
| 326 | + if ($versionContent === false) { |
| 327 | + throw new Exception('Fail to open version file.'); |
| 328 | + } |
| 329 | + |
| 330 | + $userView->file_put_contents($newVersionPath, $versionContent); |
| 331 | + $userView->getFileInfo($newVersionPath); |
| 332 | + } |
| 333 | + |
| 334 | + // 2. Create the entity in the database |
| 335 | + $versionEntity = new VersionEntity(); |
| 336 | + $versionEntity->setFileId($source->getId()); |
| 337 | + $versionEntity->setTimestamp($version->getTimestamp()); |
| 338 | + $versionEntity->setSize($version->getSize()); |
| 339 | + $versionEntity->setMimetype($this->mimeTypeLoader->getId($version->getMimetype())); |
| 340 | + if ($version instanceof IMetadataVersion) { |
| 341 | + $versionEntity->setMetadata($version->getMetadata()); |
| 342 | + } |
| 343 | + $this->versionsMapper->insert($versionEntity); |
| 344 | + } |
| 345 | + } |
| 346 | + |
| 347 | + /** |
| 348 | + * @inheritdoc |
| 349 | + */ |
| 350 | + public function clearVersionsForFile(IUser $user, Node $node): void { |
| 351 | + $userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
| 352 | + |
| 353 | + $relativePath = $userFolder->getRelativePath($node->getPath()); |
| 354 | + if ($relativePath === null) { |
| 355 | + throw new Exception("Relative path not found for node with path: " . $node->getPath()); |
| 356 | + } |
| 357 | + |
| 358 | + $versions = Storage::getVersions($user->getUID(), $relativePath); |
| 359 | + foreach ($versions as $v) { |
| 360 | + Storage::deleteRevision($relativePath, (int)$v['version']); |
| 361 | + } |
| 362 | + |
| 363 | + $this->versionsMapper->deleteAllVersionsForFileId($node->getId()); |
| 364 | + } |
299 | 365 | } |
0 commit comments