diff options
author | Louis Chemineau <louis@chmn.me> | 2022-11-22 18:14:07 +0100 |
---|---|---|
committer | Louis (Rebase PR Action) <artonge@users.noreply.github.com> | 2023-01-26 10:12:22 +0000 |
commit | 1ade482797e2d26eb006b433ed71e6e7ac407f1f (patch) | |
tree | 0869e097de8fb90c34925888f6e2990e241ceb4d /apps/files_versions | |
parent | 3eb73d0a3d081194e676b7e7ba220a118e01e18d (diff) | |
download | nextcloud-server-1ade482797e2d26eb006b433ed71e6e7ac407f1f.tar.gz nextcloud-server-1ade482797e2d26eb006b433ed71e6e7ac407f1f.zip |
Modernise hook listener of files_versions
Signed-off-by: Louis Chemineau <louis@chmn.me>
Diffstat (limited to 'apps/files_versions')
-rw-r--r-- | apps/files_versions/lib/AppInfo/Application.php | 20 | ||||
-rw-r--r-- | apps/files_versions/lib/Hooks.php | 123 |
2 files changed, 88 insertions, 55 deletions
diff --git a/apps/files_versions/lib/AppInfo/Application.php b/apps/files_versions/lib/AppInfo/Application.php index 357a4179666..a6eab420742 100644 --- a/apps/files_versions/lib/AppInfo/Application.php +++ b/apps/files_versions/lib/AppInfo/Application.php @@ -44,6 +44,13 @@ use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\Files\Events\Node\BeforeNodeCopiedEvent; +use OCP\Files\Events\Node\BeforeNodeDeletedEvent; +use OCP\Files\Events\Node\BeforeNodeRenamedEvent; +use OCP\Files\Events\Node\BeforeNodeWrittenEvent; +use OCP\Files\Events\Node\NodeCopiedEvent; +use OCP\Files\Events\Node\NodeDeletedEvent; +use OCP\Files\Events\Node\NodeRenamedEvent; use OCP\IConfig; use OCP\IGroupManager; use OCP\IServerContainer; @@ -96,15 +103,18 @@ class Application extends App implements IBootstrap { */ $context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class); $context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class); + + $context->registerEventListener(BeforeNodeWrittenEvent::class, Hooks::class); + $context->registerEventListener(BeforeNodeDeletedEvent::class, Hooks::class); + $context->registerEventListener(NodeDeletedEvent::class, Hooks::class); + $context->registerEventListener(NodeRenamedEvent::class, Hooks::class); + $context->registerEventListener(NodeCopiedEvent::class, Hooks::class); + $context->registerEventListener(BeforeNodeRenamedEvent::class, Hooks::class); + $context->registerEventListener(BeforeNodeCopiedEvent::class, Hooks::class); } public function boot(IBootContext $context): void { $context->injectFn(\Closure::fromCallable([$this, 'registerVersionBackends'])); - - /** - * Register hooks - */ - Hooks::connectHooks(); } public function registerVersionBackends(ContainerInterface $container, IAppManager $appManager, LoggerInterface $logger): void { diff --git a/apps/files_versions/lib/Hooks.php b/apps/files_versions/lib/Hooks.php index 02f562b1874..d4408190ea3 100644 --- a/apps/files_versions/lib/Hooks.php +++ b/apps/files_versions/lib/Hooks.php @@ -32,85 +32,107 @@ namespace OCA\Files_Versions; use OC\Files\Filesystem; use OC\Files\Mount\MoveableMount; use OC\Files\View; -use OCP\Util; - -class Hooks { - public static function connectHooks() { - // Listen to write signals - Util::connectHook('OC_Filesystem', 'write', Hooks::class, 'write_hook'); - // Listen to delete and rename signals - Util::connectHook('OC_Filesystem', 'post_delete', Hooks::class, 'remove_hook'); - Util::connectHook('OC_Filesystem', 'delete', Hooks::class, 'pre_remove_hook'); - Util::connectHook('OC_Filesystem', 'post_rename', Hooks::class, 'rename_hook'); - Util::connectHook('OC_Filesystem', 'post_copy', Hooks::class, 'copy_hook'); - Util::connectHook('OC_Filesystem', 'rename', Hooks::class, 'pre_renameOrCopy_hook'); - Util::connectHook('OC_Filesystem', 'copy', Hooks::class, 'pre_renameOrCopy_hook'); +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Files\Events\Node\BeforeNodeCopiedEvent; +use OCP\Files\Events\Node\BeforeNodeDeletedEvent; +use OCP\Files\Events\Node\BeforeNodeRenamedEvent; +use OCP\Files\Events\Node\BeforeNodeWrittenEvent; +use OCP\Files\Events\Node\NodeCopiedEvent; +use OCP\Files\Events\Node\NodeDeletedEvent; +use OCP\Files\Events\Node\NodeRenamedEvent; +use OCP\Files\Folder; +use OCP\Files\Node; + +class Hooks implements IEventListener { + public Folder $userFolder; + + public function __construct( + Folder $userFolder + ) { + $this->userFolder = $userFolder; + } + + public function handle(Event $event): void { + if ($event instanceof BeforeNodeWrittenEvent) { + $this->write_hook($event->getNode()); + } + + if ($event instanceof BeforeNodeDeletedEvent) { + $this->pre_remove_hook($event->getNode()); + } + + if ($event instanceof NodeDeletedEvent) { + $this->remove_hook($event->getNode()); + } + + if ($event instanceof NodeRenamedEvent) { + $this->rename_hook($event->getSource(), $event->getTarget()); + } + + if ($event instanceof NodeCopiedEvent) { + $this->copy_hook($event->getSource(), $event->getTarget()); + } + + if ($event instanceof BeforeNodeRenamedEvent) { + $this->pre_renameOrCopy_hook($event->getSource(), $event->getTarget()); + } + + if ($event instanceof BeforeNodeCopiedEvent) { + $this->pre_renameOrCopy_hook($event->getSource(), $event->getTarget()); + } } /** * listen to write event. */ - public static function write_hook(array $params): void { - $path = $params[Filesystem::signal_param_path]; - if ($path !== '') { - Storage::store($path); - } + public function write_hook(Node $node): void { + $path = $this->userFolder->getRelativePath($node->getPath()); + Storage::store($path); } /** * Erase versions of deleted file - * @param array $params * * This function is connected to the delete signal of OC_Filesystem * cleanup the versions directory if the actual file gets deleted */ - public static function remove_hook(array $params): void { - $path = $params[Filesystem::signal_param_path]; - if ($path !== '') { - Storage::delete($path); - } + public function remove_hook(Node $node): void { + $path = $this->userFolder->getRelativePath($node->getPath()); + Storage::delete($path); } /** * mark file as "deleted" so that we can clean up the versions if the file is gone - * @param array $params */ - public static function pre_remove_hook(array $params): void { - $path = $params[Filesystem::signal_param_path]; - if ($path !== '') { - Storage::markDeletedFile($path); - } + public function pre_remove_hook(Node $node): void { + $path = $this->userFolder->getRelativePath($node->getPath()); + Storage::markDeletedFile($path); } /** * rename/move versions of renamed/moved files - * @param array $params array with oldpath and newpath * * This function is connected to the rename signal of OC_Filesystem and adjust the name and location * of the stored versions along the actual file */ - public static function rename_hook(array $params): void { - $oldpath = $params['oldpath']; - $newpath = $params['newpath']; - if ($oldpath !== '' && $newpath !== '') { - Storage::renameOrCopy($oldpath, $newpath, 'rename'); - } + public function rename_hook(Node $source, Node $target): void { + $oldPath = $this->userFolder->getRelativePath($source->getPath()); + $newPath = $this->userFolder->getRelativePath($target->getPath()); + Storage::renameOrCopy($oldPath, $newPath, 'rename'); } /** * copy versions of copied files - * @param array $params array with oldpath and newpath * * This function is connected to the copy signal of OC_Filesystem and copies the * the stored versions to the new location */ - public static function copy_hook(array $params): void { - $oldpath = $params['oldpath']; - $newpath = $params['newpath']; - if ($oldpath !== '' && $newpath !== '') { - Storage::renameOrCopy($oldpath, $newpath, 'copy'); - } + public function copy_hook(Node $source, Node $target): void { + $oldPath = $this->userFolder->getRelativePath($source->getPath()); + $newPath = $this->userFolder->getRelativePath($target->getPath()); + Storage::renameOrCopy($oldPath, $newPath, 'copy'); } /** @@ -118,13 +140,14 @@ class Hooks { * If the file already exists, then it was a upload of a existing file * over the web interface and we call Storage::store() directly * - * @param array $params array with oldpath and newpath * */ - public static function pre_renameOrCopy_hook(array $params): 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 - $absOldPath = Filesystem::normalizePath('/' . \OC_User::getUser() . '/files' . $params['oldpath']); + $oldPath = $this->userFolder->getRelativePath($source->getPath()); + $newPath = $this->userFolder->getRelativePath($target->getPath()); + $absOldPath = Filesystem::normalizePath('/' . \OC_User::getUser() . '/files' . $oldPath); $manager = Filesystem::getMountManager(); $mount = $manager->find($absOldPath); $internalPath = $mount->getInternalPath($absOldPath); @@ -133,10 +156,10 @@ class Hooks { } $view = new View(\OC_User::getUser() . '/files'); - if ($view->file_exists($params['newpath'])) { - Storage::store($params['newpath']); + if ($view->file_exists($newPath)) { + Storage::store($newPath); } else { - Storage::setSourcePathAndUser($params['oldpath']); + Storage::setSourcePathAndUser($oldPath); } } } |