Skip to content

Commit dcfe94a

Browse files
committed
feat(internal-link): avoid re-writing of the new fileid
Signed-off-by: Maxence Lange <[email protected]>
1 parent b8c8a4c commit dcfe94a

4 files changed

Lines changed: 48 additions & 12 deletions

File tree

apps/files/lib/Controller/ViewController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ public function showFile(?string $fileid = null, ?string $opendetails = null, ?s
9292
try {
9393
$event = new InternalLinkRequestEvent($fileid);
9494
$this->eventDispatcher->dispatchTyped($event);
95-
96-
return $event->getResponse() ?? $this->redirectToFile((int)$fileid, $opendetails, $openfile);
95+
96+
return $event->getResponse() ?? $this->redirectToFile((int)($event->getNewFileId() ?? $fileid), $opendetails, $openfile);
9797
} catch (NotFoundException $e) {
9898
// Keep the fileid even if not found, it will be used
9999
// 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
@@ -407,6 +407,7 @@
407407
'OCP\\Files\\Events\\FileCacheUpdated' => $baseDir . '/lib/public/Files/Events/FileCacheUpdated.php',
408408
'OCP\\Files\\Events\\FileScannedEvent' => $baseDir . '/lib/public/Files/Events/FileScannedEvent.php',
409409
'OCP\\Files\\Events\\FolderScannedEvent' => $baseDir . '/lib/public/Files/Events/FolderScannedEvent.php',
410+
'OCP\\Files\\Events\\InternalLinkRequestEvent' => $baseDir . '/lib/public/Files/Events/InternalLinkRequestEvent.php',
410411
'OCP\\Files\\Events\\InvalidateMountCacheEvent' => $baseDir . '/lib/public/Files/Events/InvalidateMountCacheEvent.php',
411412
'OCP\\Files\\Events\\NodeAddedToCache' => $baseDir . '/lib/public/Files/Events/NodeAddedToCache.php',
412413
'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
@@ -448,6 +448,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
448448
'OCP\\Files\\Events\\FileCacheUpdated' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileCacheUpdated.php',
449449
'OCP\\Files\\Events\\FileScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileScannedEvent.php',
450450
'OCP\\Files\\Events\\FolderScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FolderScannedEvent.php',
451+
'OCP\\Files\\Events\\InternalLinkRequestEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/InternalLinkRequestEvent.php',
451452
'OCP\\Files\\Events\\InvalidateMountCacheEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/InvalidateMountCacheEvent.php',
452453
'OCP\\Files\\Events\\NodeAddedToCache' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeAddedToCache.php',
453454
'OCP\\Files\\Events\\NodeAddedToFavorite' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeAddedToFavorite.php',

lib/public/Files/Events/InternalLinkRequestEvent.php

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,82 @@
88
*/
99
namespace OCP\Files\Events;
1010

11-
use OCP\AppFramework\Http\Response;
11+
use OCA\Files\Controller\ViewController;
12+
use OCP\AppFramework\Http\RedirectResponse;
1213
use OCP\EventDispatcher\Event;
14+
use OCP\Server;
15+
use Psr\Log\LoggerInterface;
1316

1417
/**
18+
* Allow a modification of the behavior on internal-link request ('/index.php/f/12345')
19+
*
20+
* A listener can change the value of the FileId or even force a new RedirectResponse() to be sent back to the client.
21+
*
22+
* @see ViewController::showFile
1523
* @since 32.0.0
1624
*/
1725
class InternalLinkRequestEvent extends Event {
18-
private ?Response $response = null;
26+
private ?RedirectResponse $response = null;
27+
private ?string $newFileId = null;
1928
/**
2029
* @since 32.0.0
2130
*/
2231
public function __construct(
23-
private string &$fileId,
32+
private readonly string $fileId,
2433
) {
2534
parent::__construct();
2635
}
2736

2837
/**
38+
* returns the original fileId
39+
*
2940
* @since 32.0.0
3041
*/
31-
public function setFileId(string $fileId): void {
32-
$this->fileId = $fileId;
42+
public function getFileId(): string {
43+
return $this->fileId;
3344
}
3445

3546
/**
47+
* Set a new fileId that will be used by the original RedirectResponse
48+
*
3649
* @since 32.0.0
3750
*/
38-
public function getFileId(): string {
39-
return $this->fileId;
51+
public function setNewFileId(string $fileId): void {
52+
if ($this->newFileId === null) {
53+
$this->newFileId = $fileId;
54+
} else {
55+
Server::get(LoggerInterface::class)->notice('a new file id was already set', ['exception' => new \Exception('')]);
56+
}
57+
}
58+
59+
/**
60+
* return new fileId, or NULL if not defined
61+
*
62+
* @since 32.0.0
63+
*/
64+
public function getNewFileId(): ?string {
65+
return $this->newFileId;
4066
}
4167

4268
/**
69+
* set a new RedirectResponse
70+
*
4371
* @since 32.0.0
4472
*/
45-
public function setResponse(Response $response): void {
46-
$this->response = $response;
73+
public function setResponse(RedirectResponse $response): void {
74+
if ($this->response === null) {
75+
$this->response = $response;
76+
} else {
77+
Server::get(LoggerInterface::class)->notice('a RedirectResponse was already set', ['exception' => new \Exception('')]);
78+
}
4779
}
4880

4981
/**
82+
* return the new response to send back to client
83+
*
5084
* @since 32.0.0
5185
*/
52-
public function getResponse(): ?Response {
86+
public function getResponse(): ?RedirectResponse {
5387
return $this->response;
5488
}
5589
}

0 commit comments

Comments
 (0)