Skip to content

Commit 5fcb7c2

Browse files
committed
feat(internal-link): event on request
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent 9a7aa70 commit 5fcb7c2

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

apps/files/lib/Controller/ViewController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCP\Authentication\TwoFactorAuth\IRegistry;
3030
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
3131
use OCP\EventDispatcher\IEventDispatcher;
32+
use OCP\Files\Events\InternalLinkRequestEvent;
3233
use OCP\Files\Folder;
3334
use OCP\Files\IRootFolder;
3435
use OCP\Files\NotFoundException;
@@ -91,7 +92,10 @@ public function showFile(?string $fileid = null, ?string $opendetails = null, ?s
9192

9293
// This is the entry point from the `/f/{fileid}` URL which is hardcoded in the server.
9394
try {
94-
return $this->redirectToFile((int)$fileid, $opendetails, $openfile);
95+
$event = new InternalLinkRequestEvent($fileid);
96+
$this->eventDispatcher->dispatchTyped($event);
97+
98+
return $event->getResponse() ?? $this->redirectToFile((int)$fileid, $opendetails, $openfile);
9599
} catch (NotFoundException $e) {
96100
// Keep the fileid even if not found, it will be used
97101
// to detect the file could not be found and warn the user

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@
434434
'OCP\\Files\\Events\\FileCacheUpdated' => $baseDir . '/lib/public/Files/Events/FileCacheUpdated.php',
435435
'OCP\\Files\\Events\\FileScannedEvent' => $baseDir . '/lib/public/Files/Events/FileScannedEvent.php',
436436
'OCP\\Files\\Events\\FolderScannedEvent' => $baseDir . '/lib/public/Files/Events/FolderScannedEvent.php',
437+
'OCP\\Files\\Events\\InternalLinkRequestEvent' => $baseDir . '/lib/public/Files/Events/InternalLinkRequestEvent.php',
437438
'OCP\\Files\\Events\\InvalidateMountCacheEvent' => $baseDir . '/lib/public/Files/Events/InvalidateMountCacheEvent.php',
438439
'OCP\\Files\\Events\\NodeAddedToCache' => $baseDir . '/lib/public/Files/Events/NodeAddedToCache.php',
439440
'OCP\\Files\\Events\\NodeAddedToFavorite' => $baseDir . '/lib/public/Files/Events/NodeAddedToFavorite.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
475475
'OCP\\Files\\Events\\FileCacheUpdated' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileCacheUpdated.php',
476476
'OCP\\Files\\Events\\FileScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileScannedEvent.php',
477477
'OCP\\Files\\Events\\FolderScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FolderScannedEvent.php',
478+
'OCP\\Files\\Events\\InternalLinkRequestEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/InternalLinkRequestEvent.php',
478479
'OCP\\Files\\Events\\InvalidateMountCacheEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/InvalidateMountCacheEvent.php',
479480
'OCP\\Files\\Events\\NodeAddedToCache' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeAddedToCache.php',
480481
'OCP\\Files\\Events\\NodeAddedToFavorite' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeAddedToFavorite.php',
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
namespace OCP\Files\Events;
10+
11+
use OCA\Files\Controller\ViewController;
12+
use OCP\AppFramework\Http\RedirectResponse;
13+
use OCP\EventDispatcher\Event;
14+
use OCP\Server;
15+
use Psr\Log\LoggerInterface;
16+
17+
/**
18+
* Allow a modification of the behavior on internal-link request ('/index.php/f/12345')
19+
*
20+
* A listener can generate its own RedirectResponse() to be sent back to the client.
21+
*
22+
* @see ViewController::showFile
23+
* @since 33.0.0
24+
*/
25+
class InternalLinkRequestEvent extends Event {
26+
private ?RedirectResponse $response = null;
27+
28+
/**
29+
* @since 33.0.0
30+
*/
31+
public function __construct(
32+
private readonly string $fileId,
33+
) {
34+
parent::__construct();
35+
}
36+
37+
/**
38+
* returns the requested file id
39+
*
40+
* @since 33.0.0
41+
*/
42+
public function getFileId(): string {
43+
return $this->fileId;
44+
}
45+
46+
/**
47+
* set a new RedirectResponse
48+
*
49+
* @since 33.0.0
50+
*/
51+
public function setResponse(RedirectResponse $response): void {
52+
if ($this->response === null) {
53+
$this->response = $response;
54+
} else {
55+
Server::get(LoggerInterface::class)->notice('a RedirectResponse was already set', ['exception' => new \Exception('')]);
56+
}
57+
}
58+
59+
/**
60+
* return the new response
61+
*
62+
* @since 33.0.0
63+
*/
64+
public function getResponse(): ?RedirectResponse {
65+
return $this->response;
66+
}
67+
}

0 commit comments

Comments
 (0)