|
8 | 8 | */ |
9 | 9 | namespace OCP\Files\Events; |
10 | 10 |
|
| 11 | +use OCA\Files\Controller\ViewController; |
11 | 12 | use OCP\AppFramework\Http\Response; |
12 | 13 | use OCP\EventDispatcher\Event; |
| 14 | +use OCP\Server; |
| 15 | +use Psr\Log\LoggerInterface; |
13 | 16 |
|
14 | 17 | /** |
| 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 |
15 | 23 | * @since 32.0.0 |
16 | 24 | */ |
17 | 25 | class InternalLinkRequestEvent extends Event { |
18 | 26 | private ?Response $response = null; |
| 27 | + private ?string $newFileId = null; |
19 | 28 | /** |
20 | 29 | * @since 32.0.0 |
21 | 30 | */ |
22 | 31 | public function __construct( |
23 | | - private string &$fileId, |
| 32 | + private readonly string $fileId, |
24 | 33 | ) { |
25 | 34 | parent::__construct(); |
26 | 35 | } |
27 | 36 |
|
28 | 37 | /** |
| 38 | + * returns the original fileId |
| 39 | +
|
29 | 40 | * @since 32.0.0 |
30 | 41 | */ |
31 | | - public function setFileId(string $fileId): void { |
32 | | - $this->fileId = $fileId; |
| 42 | + public function getFileId(): string { |
| 43 | + return $this->fileId; |
33 | 44 | } |
34 | 45 |
|
35 | 46 | /** |
| 47 | + * Set a new fileId that will be used by the original RedirectResponse |
| 48 | + * |
36 | 49 | * @since 32.0.0 |
37 | 50 | */ |
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; |
40 | 66 | } |
41 | 67 |
|
42 | 68 | /** |
| 69 | + * set a new Response |
| 70 | + * |
43 | 71 | * @since 32.0.0 |
44 | 72 | */ |
45 | 73 | 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 | + } |
47 | 79 | } |
48 | 80 |
|
49 | 81 | /** |
| 82 | + * return the new response to send back to client |
| 83 | + * |
50 | 84 | * @since 32.0.0 |
51 | 85 | */ |
52 | 86 | public function getResponse(): ?Response { |
|
0 commit comments