Skip to content

Commit

Permalink
Use node's owner if no user is connected
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed Jan 26, 2023
1 parent 84e5b9c commit 3da63f4
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions apps/files_versions/lib/Listener/FileEventsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ public function write_hook(Node $node): void {
return;
}

$userFolder = $this->rootFolder->getUserFolder(\OC_User::getUser());
$path = $userFolder->getRelativePath($node->getPath());
$path = $this->getPathForNode($node);
$result = Storage::store($path);

// Store the result of the version creation so it can be used in post_write_hook.
Expand Down Expand Up @@ -244,8 +243,7 @@ public function remove_hook(Node $node): void {
return;
}
$node = $this->versionsDeleted[$path];
$userFolder = $this->rootFolder->getUserFolder(\OC_User::getUser());
$relativePath = $userFolder->getRelativePath($node->getPath());
$relativePath = $this->getPathForNode($node);
unset($this->versionsDeleted[$path]);
Storage::delete($relativePath);
$this->versionsMapper->deleteAllVersionsForFileId($node->getId());
Expand All @@ -255,8 +253,7 @@ public function remove_hook(Node $node): void {
* mark file as "deleted" so that we can clean up the versions if the file is gone
*/
public function pre_remove_hook(Node $node): void {
$userFolder = $this->rootFolder->getUserFolder(\OC_User::getUser());
$path = $userFolder->getRelativePath($node->getPath());
$path = $this->getPathForNode($node);
Storage::markDeletedFile($path);
$this->versionsDeleted[$node->getPath()] = $node;
}
Expand All @@ -268,9 +265,8 @@ public function pre_remove_hook(Node $node): void {
* of the stored versions along the actual file
*/
public function rename_hook(Node $source, Node $target): void {
$userFolder = $this->rootFolder->getUserFolder(\OC_User::getUser());
$oldPath = $userFolder->getRelativePath($source->getPath());
$newPath = $userFolder->getRelativePath($target->getPath());
$oldPath = $this->getPathForNode($source);
$newPath = $this->getPathForNode($target);
Storage::renameOrCopy($oldPath, $newPath, 'rename');
}

Expand All @@ -281,9 +277,8 @@ public function rename_hook(Node $source, Node $target): void {
* the stored versions to the new location
*/
public function copy_hook(Node $source, Node $target): void {
$userFolder = $this->rootFolder->getUserFolder(\OC_User::getUser());
$oldPath = $userFolder->getRelativePath($source->getPath());
$newPath = $userFolder->getRelativePath($target->getPath());
$oldPath = $this->getPathForNode($source);
$newPath = $this->getPathForNode($target);
Storage::renameOrCopy($oldPath, $newPath, 'copy');
}

Expand All @@ -297,9 +292,8 @@ public function copy_hook(Node $source, Node $target): void {
public function pre_renameOrCopy_hook(Node $source, Node $target): void {
// if we rename a movable mount point, then the versions don't have
// to be renamed
$userFolder = $this->rootFolder->getUserFolder(\OC_User::getUser());
$oldPath = $userFolder->getRelativePath($source->getPath());
$newPath = $userFolder->getRelativePath($target->getPath());
$oldPath = $this->getPathForNode($source);
$newPath = $this->getPathForNode($target);
$absOldPath = Filesystem::normalizePath('/' . \OC_User::getUser() . '/files' . $oldPath);
$manager = Filesystem::getMountManager();
$mount = $manager->find($absOldPath);
Expand All @@ -315,4 +309,20 @@ public function pre_renameOrCopy_hook(Node $source, Node $target): void {
Storage::setSourcePathAndUser($oldPath);
}
}

/**
* Retrieve the path relative to the current user root folder.
* If no user is connected, use the node's owner.
*/
private function getPathForNode(Node $node): ?string {
try {
return $this->rootFolder
->getUserFolder(\OC_User::getUser())
->getRelativePath($node->getPath());
} catch (\Throwable $ex) {
return $this->rootFolder
->getUserFolder($node->getOwner()->getUid())
->getRelativePath($node->getPath());
}
}
}

0 comments on commit 3da63f4

Please sign in to comment.