From 5fcb7c20fefd0a8c6a1d971e97fc8b8811603781 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Mon, 16 Jun 2025 02:01:06 +0200 Subject: [PATCH] feat(internal-link): event on request Signed-off-by: Maxence Lange --- apps/files/lib/Controller/ViewController.php | 6 +- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + .../Files/Events/InternalLinkRequestEvent.php | 67 +++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 lib/public/Files/Events/InternalLinkRequestEvent.php diff --git a/apps/files/lib/Controller/ViewController.php b/apps/files/lib/Controller/ViewController.php index ecf21cef313ac..e53234220e219 100644 --- a/apps/files/lib/Controller/ViewController.php +++ b/apps/files/lib/Controller/ViewController.php @@ -29,6 +29,7 @@ use OCP\Authentication\TwoFactorAuth\IRegistry; use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\Events\InternalLinkRequestEvent; use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; @@ -91,7 +92,10 @@ public function showFile(?string $fileid = null, ?string $opendetails = null, ?s // This is the entry point from the `/f/{fileid}` URL which is hardcoded in the server. try { - return $this->redirectToFile((int)$fileid, $opendetails, $openfile); + $event = new InternalLinkRequestEvent($fileid); + $this->eventDispatcher->dispatchTyped($event); + + return $event->getResponse() ?? $this->redirectToFile((int)$fileid, $opendetails, $openfile); } catch (NotFoundException $e) { // Keep the fileid even if not found, it will be used // to detect the file could not be found and warn the user diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index ee77fbd4cda82..686523859980a 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -434,6 +434,7 @@ 'OCP\\Files\\Events\\FileCacheUpdated' => $baseDir . '/lib/public/Files/Events/FileCacheUpdated.php', 'OCP\\Files\\Events\\FileScannedEvent' => $baseDir . '/lib/public/Files/Events/FileScannedEvent.php', 'OCP\\Files\\Events\\FolderScannedEvent' => $baseDir . '/lib/public/Files/Events/FolderScannedEvent.php', + 'OCP\\Files\\Events\\InternalLinkRequestEvent' => $baseDir . '/lib/public/Files/Events/InternalLinkRequestEvent.php', 'OCP\\Files\\Events\\InvalidateMountCacheEvent' => $baseDir . '/lib/public/Files/Events/InvalidateMountCacheEvent.php', 'OCP\\Files\\Events\\NodeAddedToCache' => $baseDir . '/lib/public/Files/Events/NodeAddedToCache.php', 'OCP\\Files\\Events\\NodeAddedToFavorite' => $baseDir . '/lib/public/Files/Events/NodeAddedToFavorite.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 3b18f00da9697..e8507ac3fa735 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -475,6 +475,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Files\\Events\\FileCacheUpdated' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileCacheUpdated.php', 'OCP\\Files\\Events\\FileScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileScannedEvent.php', 'OCP\\Files\\Events\\FolderScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FolderScannedEvent.php', + 'OCP\\Files\\Events\\InternalLinkRequestEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/InternalLinkRequestEvent.php', 'OCP\\Files\\Events\\InvalidateMountCacheEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/InvalidateMountCacheEvent.php', 'OCP\\Files\\Events\\NodeAddedToCache' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeAddedToCache.php', 'OCP\\Files\\Events\\NodeAddedToFavorite' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeAddedToFavorite.php', diff --git a/lib/public/Files/Events/InternalLinkRequestEvent.php b/lib/public/Files/Events/InternalLinkRequestEvent.php new file mode 100644 index 0000000000000..17a5aed775920 --- /dev/null +++ b/lib/public/Files/Events/InternalLinkRequestEvent.php @@ -0,0 +1,67 @@ +fileId; + } + + /** + * set a new RedirectResponse + * + * @since 33.0.0 + */ + public function setResponse(RedirectResponse $response): void { + if ($this->response === null) { + $this->response = $response; + } else { + Server::get(LoggerInterface::class)->notice('a RedirectResponse was already set', ['exception' => new \Exception('')]); + } + } + + /** + * return the new response + * + * @since 33.0.0 + */ + public function getResponse(): ?RedirectResponse { + return $this->response; + } +}