X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/d41452f39c90deaca98b4fe0e8c87f7d7aa395b8..3dda622f0a6e0446e2fdb02b64ce88701f42f609:/app/Http/Controllers/AttachmentController.php diff --git a/app/Http/Controllers/AttachmentController.php b/app/Http/Controllers/AttachmentController.php index 0830693bc..be20cda93 100644 --- a/app/Http/Controllers/AttachmentController.php +++ b/app/Http/Controllers/AttachmentController.php @@ -14,18 +14,15 @@ use Illuminate\Validation\ValidationException; class AttachmentController extends Controller { protected $attachmentService; - protected $attachment; protected $pageRepo; /** * AttachmentController constructor. */ - public function __construct(AttachmentService $attachmentService, Attachment $attachment, PageRepo $pageRepo) + public function __construct(AttachmentService $attachmentService, PageRepo $pageRepo) { $this->attachmentService = $attachmentService; - $this->attachment = $attachment; $this->pageRepo = $pageRepo; - parent::__construct(); } @@ -68,7 +65,7 @@ class AttachmentController extends Controller 'file' => 'required|file' ]); - $attachment = $this->attachment->newQuery()->findOrFail($attachmentId); + $attachment = Attachment::query()->findOrFail($attachmentId); $this->checkOwnablePermission('view', $attachment->page); $this->checkOwnablePermission('page-update', $attachment->page); $this->checkOwnablePermission('attachment-create', $attachment); @@ -90,7 +87,7 @@ class AttachmentController extends Controller */ public function getUpdateForm(string $attachmentId) { - $attachment = $this->attachment->findOrFail($attachmentId); + $attachment = Attachment::query()->findOrFail($attachmentId); $this->checkOwnablePermission('page-update', $attachment->page); $this->checkOwnablePermission('attachment-create', $attachment); @@ -105,12 +102,12 @@ class AttachmentController extends Controller */ public function update(Request $request, string $attachmentId) { - $attachment = $this->attachment->newQuery()->findOrFail($attachmentId); - + /** @var Attachment $attachment */ + $attachment = Attachment::query()->findOrFail($attachmentId); try { $this->validate($request, [ 'attachment_edit_name' => 'required|string|min:1|max:255', - 'attachment_edit_url' => 'string|min:1|max:255' + 'attachment_edit_url' => 'string|min:1|max:255|safe_url' ]); } catch (ValidationException $exception) { return response()->view('attachments.manager-edit-form', array_merge($request->only(['attachment_edit_name', 'attachment_edit_url']), [ @@ -145,7 +142,7 @@ class AttachmentController extends Controller $this->validate($request, [ 'attachment_link_uploaded_to' => 'required|integer|exists:pages,id', 'attachment_link_name' => 'required|string|min:1|max:255', - 'attachment_link_url' => 'required|string|min:1|max:255' + 'attachment_link_url' => 'required|string|min:1|max:255|safe_url' ]); } catch (ValidationException $exception) { return response()->view('attachments.manager-link-form', array_merge($request->only(['attachment_link_name', 'attachment_link_url']), [ @@ -161,7 +158,7 @@ class AttachmentController extends Controller $attachmentName = $request->get('attachment_link_name'); $link = $request->get('attachment_link_url'); - $attachment = $this->attachmentService->saveNewFromLink($attachmentName, $link, $pageId); + $this->attachmentService->saveNewFromLink($attachmentName, $link, intval($pageId)); return view('attachments.manager-link-form', [ 'pageId' => $pageId, @@ -203,9 +200,10 @@ class AttachmentController extends Controller * @throws FileNotFoundException * @throws NotFoundException */ - public function get(string $attachmentId) + public function get(Request $request, string $attachmentId) { - $attachment = $this->attachment->findOrFail($attachmentId); + /** @var Attachment $attachment */ + $attachment = Attachment::query()->findOrFail($attachmentId); try { $page = $this->pageRepo->getById($attachment->uploaded_to); } catch (NotFoundException $exception) { @@ -218,8 +216,13 @@ class AttachmentController extends Controller return redirect($attachment->path); } + $fileName = $attachment->getFileName(); $attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment); - return $this->downloadResponse($attachmentContents, $attachment->getFileName()); + + if ($request->get('open') === 'true') { + return $this->inlineDownloadResponse($attachmentContents, $fileName); + } + return $this->downloadResponse($attachmentContents, $fileName); } /** @@ -228,7 +231,8 @@ class AttachmentController extends Controller */ public function delete(string $attachmentId) { - $attachment = $this->attachment->findOrFail($attachmentId); + /** @var Attachment $attachment */ + $attachment = Attachment::query()->findOrFail($attachmentId); $this->checkOwnablePermission('attachment-delete', $attachment); $this->attachmentService->deleteFile($attachment); return response()->json(['message' => trans('entities.attachments_deleted')]);