3 namespace BookStack\Uploads\Controllers;
5 use BookStack\Entities\EntityExistsRule;
6 use BookStack\Entities\Queries\PageQueries;
7 use BookStack\Entities\Repos\PageRepo;
8 use BookStack\Exceptions\FileUploadException;
9 use BookStack\Exceptions\NotFoundException;
10 use BookStack\Http\Controller;
11 use BookStack\Permissions\Permission;
12 use BookStack\Uploads\Attachment;
13 use BookStack\Uploads\AttachmentService;
15 use Illuminate\Contracts\Filesystem\FileNotFoundException;
16 use Illuminate\Http\Request;
17 use Illuminate\Support\MessageBag;
18 use Illuminate\Validation\ValidationException;
20 class AttachmentController extends Controller
22 public function __construct(
23 protected AttachmentService $attachmentService,
24 protected PageQueries $pageQueries,
25 protected PageRepo $pageRepo
30 * Endpoint at which attachments are uploaded to.
32 * @throws ValidationException
33 * @throws NotFoundException
35 public function upload(Request $request)
37 $this->validate($request, [
38 'uploaded_to' => ['required', 'integer', new EntityExistsRule('page')],
39 'file' => array_merge(['required'], $this->attachmentService->getFileValidationRules()),
42 $pageId = $request->get('uploaded_to');
43 $page = $this->pageQueries->findVisibleByIdOrFail($pageId);
45 $this->checkPermission(Permission::AttachmentCreateAll);
46 $this->checkOwnablePermission(Permission::PageUpdate, $page);
48 $uploadedFile = $request->file('file');
51 $attachment = $this->attachmentService->saveNewUpload($uploadedFile, $pageId);
52 } catch (FileUploadException $e) {
53 return response($e->getMessage(), 500);
56 return response()->json($attachment);
60 * Update an uploaded attachment.
62 * @throws ValidationException
64 public function uploadUpdate(Request $request, $attachmentId)
66 $this->validate($request, [
67 'file' => array_merge(['required'], $this->attachmentService->getFileValidationRules()),
70 /** @var Attachment $attachment */
71 $attachment = Attachment::query()->findOrFail($attachmentId);
72 $this->checkOwnablePermission(Permission::PageView, $attachment->page);
73 $this->checkOwnablePermission(Permission::PageUpdate, $attachment->page);
74 $this->checkOwnablePermission(Permission::AttachmentUpdate, $attachment);
76 $uploadedFile = $request->file('file');
79 $attachment = $this->attachmentService->saveUpdatedUpload($uploadedFile, $attachment);
80 } catch (FileUploadException $e) {
81 return response($e->getMessage(), 500);
84 return response()->json($attachment);
88 * Get the update form for an attachment.
90 public function getUpdateForm(string $attachmentId)
92 /** @var Attachment $attachment */
93 $attachment = Attachment::query()->findOrFail($attachmentId);
95 $this->checkOwnablePermission(Permission::PageUpdate, $attachment->page);
96 $this->checkOwnablePermission(Permission::AttachmentCreate, $attachment);
98 return view('attachments.manager-edit-form', [
99 'attachment' => $attachment,
104 * Update the details of an existing file.
106 public function update(Request $request, string $attachmentId)
108 /** @var Attachment $attachment */
109 $attachment = Attachment::query()->findOrFail($attachmentId);
112 $this->validate($request, [
113 'attachment_edit_name' => ['required', 'string', 'min:1', 'max:255'],
114 'attachment_edit_url' => ['string', 'min:1', 'max:2000', 'safe_url'],
116 } catch (ValidationException $exception) {
117 return response()->view('attachments.manager-edit-form', array_merge($request->only(['attachment_edit_name', 'attachment_edit_url']), [
118 'attachment' => $attachment,
119 'errors' => new MessageBag($exception->errors()),
123 $this->checkOwnablePermission(Permission::PageView, $attachment->page);
124 $this->checkOwnablePermission(Permission::PageUpdate, $attachment->page);
125 $this->checkOwnablePermission(Permission::AttachmentUpdate, $attachment);
127 $attachment = $this->attachmentService->updateFile($attachment, [
128 'name' => $request->get('attachment_edit_name'),
129 'link' => $request->get('attachment_edit_url'),
132 return view('attachments.manager-edit-form', [
133 'attachment' => $attachment,
138 * Attach a link to a page.
140 * @throws NotFoundException
142 public function attachLink(Request $request)
144 $pageId = $request->get('attachment_link_uploaded_to');
147 $this->validate($request, [
148 'attachment_link_uploaded_to' => ['required', 'integer', new EntityExistsRule('page')],
149 'attachment_link_name' => ['required', 'string', 'min:1', 'max:255'],
150 'attachment_link_url' => ['required', 'string', 'min:1', 'max:2000', 'safe_url'],
152 } catch (ValidationException $exception) {
153 return response()->view('attachments.manager-link-form', array_merge($request->only(['attachment_link_name', 'attachment_link_url']), [
155 'errors' => new MessageBag($exception->errors()),
159 $page = $this->pageQueries->findVisibleByIdOrFail($pageId);
161 $this->checkPermission(Permission::AttachmentCreateAll);
162 $this->checkOwnablePermission(Permission::PageUpdate, $page);
164 $attachmentName = $request->get('attachment_link_name');
165 $link = $request->get('attachment_link_url');
166 $this->attachmentService->saveNewFromLink($attachmentName, $link, intval($pageId));
168 return view('attachments.manager-link-form', [
174 * Get the attachments for a specific page.
176 * @throws NotFoundException
178 public function listForPage(int $pageId)
180 $page = $this->pageQueries->findVisibleByIdOrFail($pageId);
182 return view('attachments.manager-list', [
183 'attachments' => $page->attachments->all(),
188 * Update the attachment sorting.
190 * @throws ValidationException
191 * @throws NotFoundException
193 public function sortForPage(Request $request, int $pageId)
195 $this->validate($request, [
196 'order' => ['required', 'array'],
198 $page = $this->pageQueries->findVisibleByIdOrFail($pageId);
199 $this->checkOwnablePermission(Permission::PageUpdate, $page);
201 $attachmentOrder = $request->get('order');
202 $this->attachmentService->updateFileOrderWithinPage($attachmentOrder, $pageId);
204 return response()->json(['message' => trans('entities.attachments_order_updated')]);
208 * Get an attachment from storage.
210 * @throws FileNotFoundException
211 * @throws NotFoundException
213 public function get(Request $request, string $attachmentId)
215 /** @var Attachment $attachment */
216 $attachment = Attachment::query()->findOrFail($attachmentId);
219 $page = $this->pageQueries->findVisibleByIdOrFail($attachment->uploaded_to);
220 } catch (NotFoundException $exception) {
221 throw new NotFoundException(trans('errors.attachment_not_found'));
224 $this->checkOwnablePermission(Permission::PageView, $page);
226 if ($attachment->external) {
227 return redirect($attachment->path);
230 $fileName = $attachment->getFileName();
231 $attachmentStream = $this->attachmentService->streamAttachmentFromStorage($attachment);
232 $attachmentSize = $this->attachmentService->getAttachmentFileSize($attachment);
234 if ($request->get('open') === 'true') {
235 return $this->download()->streamedInline($attachmentStream, $fileName, $attachmentSize);
238 return $this->download()->streamedDirectly($attachmentStream, $fileName, $attachmentSize);
242 * Delete a specific attachment in the system.
246 public function delete(string $attachmentId)
248 /** @var Attachment $attachment */
249 $attachment = Attachment::query()->findOrFail($attachmentId);
250 $this->checkOwnablePermission(Permission::AttachmentDelete, $attachment);
251 $this->attachmentService->deleteFile($attachment);
253 return response()->json(['message' => trans('entities.attachments_deleted')]);