Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Proper response for attachment endpoint #1031

Merged
merged 1 commit into from May 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 13 additions & 10 deletions lib/Controller/NotesController.php
Expand Up @@ -4,43 +4,43 @@

namespace OCA\Notes\Controller;

use OCA\Notes\Service\MetaService;
use OCA\Notes\Service\NotesService;
use OCA\Notes\Service\SettingsService;

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\StreamResponse;
use OCP\Files\IMimeTypeDetector;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;

class NotesController extends Controller {
private NotesService $notesService;
private MetaService $metaService;
private SettingsService $settingsService;
private Helper $helper;
private IConfig $settings;
private IL10N $l10n;
private IMimeTypeDetector $mimeTypeDetector;

public function __construct(
string $AppName,
IRequest $request,
NotesService $notesService,
MetaService $metaService,
SettingsService $settingsService,
Helper $helper,
IConfig $settings,
IL10N $l10n
IL10N $l10n,
IMimeTypeDetector $mimeTypeDetector
) {
parent::__construct($AppName, $request);
$this->notesService = $notesService;
$this->metaService = $metaService;
$this->settingsService = $settingsService;
$this->helper = $helper;
$this->settings = $settings;
$this->l10n = $l10n;
$this->mimeTypeDetector = $mimeTypeDetector;
}

/**
Expand Down Expand Up @@ -299,17 +299,20 @@
* With help from: https://github.com/nextcloud/cookbook
* @NoAdminRequired
* @NoCSRFRequired
* @return JSONResponse|FileDisplayResponse
* @return JSONResponse|StreamResponse
*/
public function getAttachment(int $noteid, string $path) {
public function getAttachment(int $noteid, string $path): Http\Response {
try {
$targetimage = $this->notesService->getAttachment(
$this->helper->getUID(),
$noteid,
$path
);
$headers = ['Content-Type' => $targetimage->getMimetype(), 'Cache-Control' => 'public, max-age=604800'];
return new FileDisplayResponse($targetimage, Http::STATUS_OK, $headers);
$response = new StreamResponse($targetimage->fopen('rb'));
$response->addHeader('Content-Disposition', 'attachment; filename="' . rawurldecode($targetimage->getName()) . '"');

Check warning on line 312 in lib/Controller/NotesController.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

Line exceeds 120 characters; contains 128 characters

Check warning on line 312 in lib/Controller/NotesController.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Line exceeds 120 characters; contains 128 characters
$response->addHeader('Content-Type', $this->mimeTypeDetector->getSecureMimeType($targetimage->getMimeType()));

Check warning on line 313 in lib/Controller/NotesController.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

Line exceeds 120 characters; contains 122 characters

Check warning on line 313 in lib/Controller/NotesController.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Line exceeds 120 characters; contains 122 characters
$response->addHeader('Cache-Control', 'public, max-age=604800');
return $response;
} catch (\Exception $e) {
$this->helper->logException($e);
return $this->helper->createErrorResponse($e, Http::STATUS_NOT_FOUND);
Expand Down