]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/AttachmentController.php
Apply fixes from StyleCI
[bookstack] / app / Http / Controllers / AttachmentController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Entities\Repos\PageRepo;
6 use BookStack\Exceptions\FileUploadException;
7 use BookStack\Exceptions\NotFoundException;
8 use BookStack\Uploads\Attachment;
9 use BookStack\Uploads\AttachmentService;
10 use Exception;
11 use Illuminate\Contracts\Filesystem\FileNotFoundException;
12 use Illuminate\Http\Request;
13 use Illuminate\Support\MessageBag;
14 use Illuminate\Validation\ValidationException;
15
16 class AttachmentController extends Controller
17 {
18     protected $attachmentService;
19     protected $pageRepo;
20
21     /**
22      * AttachmentController constructor.
23      */
24     public function __construct(AttachmentService $attachmentService, PageRepo $pageRepo)
25     {
26         $this->attachmentService = $attachmentService;
27         $this->pageRepo = $pageRepo;
28     }
29
30     /**
31      * Endpoint at which attachments are uploaded to.
32      *
33      * @throws ValidationException
34      * @throws NotFoundException
35      */
36     public function upload(Request $request)
37     {
38         $this->validate($request, [
39             'uploaded_to' => 'required|integer|exists:pages,id',
40             'file'        => 'required|file',
41         ]);
42
43         $pageId = $request->get('uploaded_to');
44         $page = $this->pageRepo->getById($pageId);
45
46         $this->checkPermission('attachment-create-all');
47         $this->checkOwnablePermission('page-update', $page);
48
49         $uploadedFile = $request->file('file');
50
51         try {
52             $attachment = $this->attachmentService->saveNewUpload($uploadedFile, $pageId);
53         } catch (FileUploadException $e) {
54             return response($e->getMessage(), 500);
55         }
56
57         return response()->json($attachment);
58     }
59
60     /**
61      * Update an uploaded attachment.
62      *
63      * @throws ValidationException
64      */
65     public function uploadUpdate(Request $request, $attachmentId)
66     {
67         $this->validate($request, [
68             'file' => 'required|file',
69         ]);
70
71         $attachment = Attachment::query()->findOrFail($attachmentId);
72         $this->checkOwnablePermission('view', $attachment->page);
73         $this->checkOwnablePermission('page-update', $attachment->page);
74         $this->checkOwnablePermission('attachment-create', $attachment);
75
76         $uploadedFile = $request->file('file');
77
78         try {
79             $attachment = $this->attachmentService->saveUpdatedUpload($uploadedFile, $attachment);
80         } catch (FileUploadException $e) {
81             return response($e->getMessage(), 500);
82         }
83
84         return response()->json($attachment);
85     }
86
87     /**
88      * Get the update form for an attachment.
89      *
90      * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
91      */
92     public function getUpdateForm(string $attachmentId)
93     {
94         $attachment = Attachment::query()->findOrFail($attachmentId);
95
96         $this->checkOwnablePermission('page-update', $attachment->page);
97         $this->checkOwnablePermission('attachment-create', $attachment);
98
99         return view('attachments.manager-edit-form', [
100             'attachment' => $attachment,
101         ]);
102     }
103
104     /**
105      * Update the details of an existing file.
106      */
107     public function update(Request $request, string $attachmentId)
108     {
109         /** @var Attachment $attachment */
110         $attachment = Attachment::query()->findOrFail($attachmentId);
111
112         try {
113             $this->validate($request, [
114                 'attachment_edit_name' => 'required|string|min:1|max:255',
115                 'attachment_edit_url'  => 'string|min:1|max:255|safe_url',
116             ]);
117         } catch (ValidationException $exception) {
118             return response()->view('attachments.manager-edit-form', array_merge($request->only(['attachment_edit_name', 'attachment_edit_url']), [
119                 'attachment' => $attachment,
120                 'errors'     => new MessageBag($exception->errors()),
121             ]), 422);
122         }
123
124         $this->checkOwnablePermission('view', $attachment->page);
125         $this->checkOwnablePermission('page-update', $attachment->page);
126         $this->checkOwnablePermission('attachment-create', $attachment);
127
128         $attachment = $this->attachmentService->updateFile($attachment, [
129             'name' => $request->get('attachment_edit_name'),
130             'link' => $request->get('attachment_edit_url'),
131         ]);
132
133         return view('attachments.manager-edit-form', [
134             'attachment' => $attachment,
135         ]);
136     }
137
138     /**
139      * Attach a link to a page.
140      *
141      * @throws NotFoundException
142      */
143     public function attachLink(Request $request)
144     {
145         $pageId = $request->get('attachment_link_uploaded_to');
146
147         try {
148             $this->validate($request, [
149                 'attachment_link_uploaded_to' => 'required|integer|exists:pages,id',
150                 'attachment_link_name'        => 'required|string|min:1|max:255',
151                 'attachment_link_url'         => 'required|string|min:1|max:255|safe_url',
152             ]);
153         } catch (ValidationException $exception) {
154             return response()->view('attachments.manager-link-form', array_merge($request->only(['attachment_link_name', 'attachment_link_url']), [
155                 'pageId' => $pageId,
156                 'errors' => new MessageBag($exception->errors()),
157             ]), 422);
158         }
159
160         $page = $this->pageRepo->getById($pageId);
161
162         $this->checkPermission('attachment-create-all');
163         $this->checkOwnablePermission('page-update', $page);
164
165         $attachmentName = $request->get('attachment_link_name');
166         $link = $request->get('attachment_link_url');
167         $this->attachmentService->saveNewFromLink($attachmentName, $link, intval($pageId));
168
169         return view('attachments.manager-link-form', [
170             'pageId' => $pageId,
171         ]);
172     }
173
174     /**
175      * Get the attachments for a specific page.
176      */
177     public function listForPage(int $pageId)
178     {
179         $page = $this->pageRepo->getById($pageId);
180         $this->checkOwnablePermission('page-view', $page);
181
182         return view('attachments.manager-list', [
183             'attachments' => $page->attachments->all(),
184         ]);
185     }
186
187     /**
188      * Update the attachment sorting.
189      *
190      * @throws ValidationException
191      * @throws NotFoundException
192      */
193     public function sortForPage(Request $request, int $pageId)
194     {
195         $this->validate($request, [
196             'order' => 'required|array',
197         ]);
198         $page = $this->pageRepo->getById($pageId);
199         $this->checkOwnablePermission('page-update', $page);
200
201         $attachmentOrder = $request->get('order');
202         $this->attachmentService->updateFileOrderWithinPage($attachmentOrder, $pageId);
203
204         return response()->json(['message' => trans('entities.attachments_order_updated')]);
205     }
206
207     /**
208      * Get an attachment from storage.
209      *
210      * @throws FileNotFoundException
211      * @throws NotFoundException
212      */
213     public function get(Request $request, string $attachmentId)
214     {
215         /** @var Attachment $attachment */
216         $attachment = Attachment::query()->findOrFail($attachmentId);
217
218         try {
219             $page = $this->pageRepo->getById($attachment->uploaded_to);
220         } catch (NotFoundException $exception) {
221             throw new NotFoundException(trans('errors.attachment_not_found'));
222         }
223
224         $this->checkOwnablePermission('page-view', $page);
225
226         if ($attachment->external) {
227             return redirect($attachment->path);
228         }
229
230         $fileName = $attachment->getFileName();
231         $attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment);
232
233         if ($request->get('open') === 'true') {
234             return $this->inlineDownloadResponse($attachmentContents, $fileName);
235         }
236
237         return $this->downloadResponse($attachmentContents, $fileName);
238     }
239
240     /**
241      * Delete a specific attachment in the system.
242      *
243      * @throws Exception
244      */
245     public function delete(string $attachmentId)
246     {
247         /** @var Attachment $attachment */
248         $attachment = Attachment::query()->findOrFail($attachmentId);
249         $this->checkOwnablePermission('attachment-delete', $attachment);
250         $this->attachmentService->deleteFile($attachment);
251
252         return response()->json(['message' => trans('entities.attachments_deleted')]);
253     }
254 }