|
4 | 4 |
|
5 | 5 | namespace OCA\Notes\Controller; |
6 | 6 |
|
7 | | -use OCA\Notes\Service\MetaService; |
8 | 7 | use OCA\Notes\Service\NotesService; |
9 | 8 | use OCA\Notes\Service\SettingsService; |
10 | 9 |
|
11 | 10 | use OCP\AppFramework\Controller; |
12 | 11 | use OCP\AppFramework\Http; |
13 | | -use OCP\AppFramework\Http\FileDisplayResponse; |
14 | 12 | use OCP\AppFramework\Http\JSONResponse; |
| 13 | +use OCP\AppFramework\Http\StreamResponse; |
| 14 | +use OCP\Files\IMimeTypeDetector; |
15 | 15 | use OCP\IConfig; |
16 | 16 | use OCP\IL10N; |
17 | 17 | use OCP\IRequest; |
18 | 18 |
|
19 | 19 | class NotesController extends Controller { |
20 | 20 | private NotesService $notesService; |
21 | | - private MetaService $metaService; |
22 | 21 | private SettingsService $settingsService; |
23 | 22 | private Helper $helper; |
24 | 23 | private IConfig $settings; |
25 | 24 | private IL10N $l10n; |
| 25 | + private IMimeTypeDetector $mimeTypeDetector; |
26 | 26 |
|
27 | 27 | public function __construct( |
28 | 28 | string $AppName, |
29 | 29 | IRequest $request, |
30 | 30 | NotesService $notesService, |
31 | | - MetaService $metaService, |
32 | 31 | SettingsService $settingsService, |
33 | 32 | Helper $helper, |
34 | 33 | IConfig $settings, |
35 | | - IL10N $l10n |
| 34 | + IL10N $l10n, |
| 35 | + IMimeTypeDetector $mimeTypeDetector |
36 | 36 | ) { |
37 | 37 | parent::__construct($AppName, $request); |
38 | 38 | $this->notesService = $notesService; |
39 | | - $this->metaService = $metaService; |
40 | 39 | $this->settingsService = $settingsService; |
41 | 40 | $this->helper = $helper; |
42 | 41 | $this->settings = $settings; |
43 | 42 | $this->l10n = $l10n; |
| 43 | + $this->mimeTypeDetector = $mimeTypeDetector; |
44 | 44 | } |
45 | 45 |
|
46 | 46 | /** |
@@ -299,17 +299,20 @@ public function destroy(int $id) : JSONResponse { |
299 | 299 | * With help from: https://github.com/nextcloud/cookbook |
300 | 300 | * @NoAdminRequired |
301 | 301 | * @NoCSRFRequired |
302 | | - * @return JSONResponse|FileDisplayResponse |
| 302 | + * @return JSONResponse|StreamResponse |
303 | 303 | */ |
304 | | - public function getAttachment(int $noteid, string $path) { |
| 304 | + public function getAttachment(int $noteid, string $path): Http\Response { |
305 | 305 | try { |
306 | 306 | $targetimage = $this->notesService->getAttachment( |
307 | 307 | $this->helper->getUID(), |
308 | 308 | $noteid, |
309 | 309 | $path |
310 | 310 | ); |
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; |
313 | 316 | } catch (\Exception $e) { |
314 | 317 | $this->helper->logException($e); |
315 | 318 | return $this->helper->createErrorResponse($e, Http::STATUS_NOT_FOUND); |
|
0 commit comments