1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Entities\Repos\PageRepo;
4 use BookStack\Exceptions\FileUploadException;
5 use BookStack\Exceptions\NotFoundException;
6 use BookStack\Uploads\Attachment;
7 use BookStack\Uploads\AttachmentService;
9 use Illuminate\Contracts\Filesystem\FileNotFoundException;
10 use Illuminate\Http\Request;
11 use Illuminate\Validation\ValidationException;
13 class AttachmentController extends Controller
15 protected $attachmentService;
16 protected $attachment;
20 * AttachmentController constructor.
22 public function __construct(AttachmentService $attachmentService, Attachment $attachment, PageRepo $pageRepo)
24 $this->attachmentService = $attachmentService;
25 $this->attachment = $attachment;
26 $this->pageRepo = $pageRepo;
27 parent::__construct();
32 * Endpoint at which attachments are uploaded to.
33 * @throws ValidationException
34 * @throws NotFoundException
36 public function upload(Request $request)
38 $this->validate($request, [
39 'uploaded_to' => 'required|integer|exists:pages,id',
40 'file' => 'required|file'
43 $pageId = $request->get('uploaded_to');
44 $page = $this->pageRepo->getById($pageId);
46 $this->checkPermission('attachment-create-all');
47 $this->checkOwnablePermission('page-update', $page);
49 $uploadedFile = $request->file('file');
52 $attachment = $this->attachmentService->saveNewUpload($uploadedFile, $pageId);
53 } catch (FileUploadException $e) {
54 return response($e->getMessage(), 500);
57 return response()->json($attachment);
61 * Update an uploaded attachment.
62 * @throws ValidationException
63 * @throws NotFoundException
65 public function uploadUpdate(Request $request, $attachmentId)
67 $this->validate($request, [
68 'uploaded_to' => 'required|integer|exists:pages,id',
69 'file' => 'required|file'
72 $pageId = $request->get('uploaded_to');
73 $page = $this->pageRepo->getById($pageId);
74 $attachment = $this->attachment->findOrFail($attachmentId);
76 $this->checkOwnablePermission('page-update', $page);
77 $this->checkOwnablePermission('attachment-create', $attachment);
79 if (intval($pageId) !== intval($attachment->uploaded_to)) {
80 return $this->jsonError(trans('errors.attachment_page_mismatch'));
83 $uploadedFile = $request->file('file');
86 $attachment = $this->attachmentService->saveUpdatedUpload($uploadedFile, $attachment);
87 } catch (FileUploadException $e) {
88 return response($e->getMessage(), 500);
91 return response()->json($attachment);
95 * Update the details of an existing file.
96 * @throws ValidationException
97 * @throws NotFoundException
99 public function update(Request $request, $attachmentId)
101 $this->validate($request, [
102 'uploaded_to' => 'required|integer|exists:pages,id',
103 'name' => 'required|string|min:1|max:255',
104 'link' => 'string|min:1|max:255'
107 $pageId = $request->get('uploaded_to');
108 $page = $this->pageRepo->getById($pageId);
109 $attachment = $this->attachment->findOrFail($attachmentId);
111 $this->checkOwnablePermission('page-update', $page);
112 $this->checkOwnablePermission('attachment-create', $attachment);
114 if (intval($pageId) !== intval($attachment->uploaded_to)) {
115 return $this->jsonError(trans('errors.attachment_page_mismatch'));
118 $attachment = $this->attachmentService->updateFile($attachment, $request->all());
119 return response()->json($attachment);
123 * Attach a link to a page.
124 * @throws ValidationException
125 * @throws NotFoundException
127 public function attachLink(Request $request)
129 $this->validate($request, [
130 'uploaded_to' => 'required|integer|exists:pages,id',
131 'name' => 'required|string|min:1|max:255',
132 'link' => 'required|string|min:1|max:255'
135 $pageId = $request->get('uploaded_to');
136 $page = $this->pageRepo->getById($pageId);
138 $this->checkPermission('attachment-create-all');
139 $this->checkOwnablePermission('page-update', $page);
141 $attachmentName = $request->get('name');
142 $link = $request->get('link');
143 $attachment = $this->attachmentService->saveNewFromLink($attachmentName, $link, $pageId);
145 return response()->json($attachment);
149 * Get the attachments for a specific page.
151 public function listForPage(int $pageId)
153 $page = $this->pageRepo->getById($pageId);
154 $this->checkOwnablePermission('page-view', $page);
155 return response()->json($page->attachments);
159 * Update the attachment sorting.
160 * @throws ValidationException
161 * @throws NotFoundException
163 public function sortForPage(Request $request, int $pageId)
165 $this->validate($request, [
166 'files' => 'required|array',
167 'files.*.id' => 'required|integer',
169 $page = $this->pageRepo->getById($pageId);
170 $this->checkOwnablePermission('page-update', $page);
172 $attachments = $request->get('files');
173 $this->attachmentService->updateFileOrderWithinPage($attachments, $pageId);
174 return response()->json(['message' => trans('entities.attachments_order_updated')]);
178 * Get an attachment from storage.
179 * @throws FileNotFoundException
180 * @throws NotFoundException
182 public function get(int $attachmentId)
184 $attachment = $this->attachment->findOrFail($attachmentId);
186 $page = $this->pageRepo->getById($attachment->uploaded_to);
187 } catch (NotFoundException $exception) {
188 throw new NotFoundException(trans('errors.attachment_not_found'));
191 $this->checkOwnablePermission('page-view', $page);
193 if ($attachment->external) {
194 return redirect($attachment->path);
197 $attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment);
198 return $this->downloadResponse($attachmentContents, $attachment->getFileName());
202 * Delete a specific attachment in the system.
203 * @param $attachmentId
207 public function delete(int $attachmentId)
209 $attachment = $this->attachment->findOrFail($attachmentId);
210 $this->checkOwnablePermission('attachment-delete', $attachment);
211 $this->attachmentService->deleteFile($attachment);
212 return response()->json(['message' => trans('entities.attachments_deleted')]);