Skip to content

Commit d69517a

Browse files
committed
fix: Proper response for attachment endpoint
Signed-off-by: Julius Härtl <[email protected]>
1 parent 1736b4e commit d69517a

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

lib/Controller/NotesController.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,43 @@
44

55
namespace OCA\Notes\Controller;
66

7-
use OCA\Notes\Service\MetaService;
87
use OCA\Notes\Service\NotesService;
98
use OCA\Notes\Service\SettingsService;
109

1110
use OCP\AppFramework\Controller;
1211
use OCP\AppFramework\Http;
13-
use OCP\AppFramework\Http\FileDisplayResponse;
1412
use OCP\AppFramework\Http\JSONResponse;
13+
use OCP\AppFramework\Http\StreamResponse;
14+
use OCP\Files\IMimeTypeDetector;
1515
use OCP\IConfig;
1616
use OCP\IL10N;
1717
use OCP\IRequest;
1818

1919
class NotesController extends Controller {
2020
private NotesService $notesService;
21-
private MetaService $metaService;
2221
private SettingsService $settingsService;
2322
private Helper $helper;
2423
private IConfig $settings;
2524
private IL10N $l10n;
25+
private IMimeTypeDetector $mimeTypeDetector;
2626

2727
public function __construct(
2828
string $AppName,
2929
IRequest $request,
3030
NotesService $notesService,
31-
MetaService $metaService,
3231
SettingsService $settingsService,
3332
Helper $helper,
3433
IConfig $settings,
35-
IL10N $l10n
34+
IL10N $l10n,
35+
IMimeTypeDetector $mimeTypeDetector
3636
) {
3737
parent::__construct($AppName, $request);
3838
$this->notesService = $notesService;
39-
$this->metaService = $metaService;
4039
$this->settingsService = $settingsService;
4140
$this->helper = $helper;
4241
$this->settings = $settings;
4342
$this->l10n = $l10n;
43+
$this->mimeTypeDetector = $mimeTypeDetector;
4444
}
4545

4646
/**
@@ -299,17 +299,20 @@ public function destroy(int $id) : JSONResponse {
299299
* With help from: https://github.com/nextcloud/cookbook
300300
* @NoAdminRequired
301301
* @NoCSRFRequired
302-
* @return JSONResponse|FileDisplayResponse
302+
* @return JSONResponse|StreamResponse
303303
*/
304-
public function getAttachment(int $noteid, string $path) {
304+
public function getAttachment(int $noteid, string $path): Http\Response {
305305
try {
306306
$targetimage = $this->notesService->getAttachment(
307307
$this->helper->getUID(),
308308
$noteid,
309309
$path
310310
);
311-
$headers = ['Content-Type' => $targetimage->getMimetype(), 'Cache-Control' => 'public, max-age=604800'];
312-
return new FileDisplayResponse($targetimage, Http::STATUS_OK, $headers);
311+
$response = new StreamResponse($targetimage->fopen('rb'));
312+
$response->addHeader('Content-Disposition', 'attachment; filename="' . rawurldecode($targetimage->getName()) . '"');
313+
$response->addHeader('Content-Type', $this->mimeTypeDetector->getSecureMimeType($targetimage->getMimeType()));
314+
$response->addHeader('Cache-Control', 'public, max-age=604800');
315+
return $response;
313316
} catch (\Exception $e) {
314317
$this->helper->logException($e);
315318
return $this->helper->createErrorResponse($e, Http::STATUS_NOT_FOUND);

0 commit comments

Comments
 (0)