Skip to content

Commit e9eb8cf

Browse files
committed
feat(internal-link): avoid re-writing of the new fileid
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent b8c8a4c commit e9eb8cf

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

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/public/Files/Events/InternalLinkRequestEvent.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,79 @@
88
*/
99
namespace OCP\Files\Events;
1010

11+
use OCA\Files\Controller\ViewController;
1112
use OCP\AppFramework\Http\Response;
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 Response() to be sent back to the client.
21+
*
22+
* @see ViewController::showFile
1523
* @since 32.0.0
1624
*/
1725
class InternalLinkRequestEvent extends Event {
1826
private ?Response $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 Response
70+
*
4371
* @since 32.0.0
4472
*/
4573
public function setResponse(Response $response): void {
46-
$this->response = $response;
74+
if ($this->response === null) {
75+
$this->response = $response;
76+
} else {
77+
Server::get(LoggerInterface::class)->notice('a Response 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
*/
5286
public function getResponse(): ?Response {

0 commit comments

Comments
 (0)