3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Exceptions\FileUploadException;
7 use BookStack\Uploads\Attachment;
8 use BookStack\Uploads\AttachmentService;
10 use Illuminate\Contracts\Filesystem\FileNotFoundException;
11 use Illuminate\Http\Request;
12 use Illuminate\Validation\ValidationException;
14 class AttachmentApiController extends ApiController
16 protected $attachmentService;
18 public function __construct(AttachmentService $attachmentService)
20 $this->attachmentService = $attachmentService;
24 * Get a listing of attachments visible to the user.
25 * The external property indicates whether the attachment is simple a link.
26 * A false value for the external property would indicate a file upload.
28 public function list()
30 return $this->apiListingResponse(Attachment::visible(), [
31 'id', 'name', 'extension', 'uploaded_to', 'external', 'order', 'created_at', 'updated_at', 'created_by', 'updated_by',
36 * Create a new attachment in the system.
37 * An uploaded_to value must be provided containing an ID of the page
38 * that this upload will be related to.
40 * If you're uploading a file the POST data should be provided via
41 * a multipart/form-data type request instead of JSON.
43 * @throws ValidationException
44 * @throws FileUploadException
46 public function create(Request $request)
48 $this->checkPermission('attachment-create-all');
49 $requestData = $this->validate($request, $this->rules()['create']);
51 $pageId = $request->get('uploaded_to');
52 $page = Page::visible()->findOrFail($pageId);
53 $this->checkOwnablePermission('page-update', $page);
55 if ($request->hasFile('file')) {
56 $uploadedFile = $request->file('file');
57 $attachment = $this->attachmentService->saveNewUpload($uploadedFile, $page->id);
59 $attachment = $this->attachmentService->saveNewFromLink(
66 $this->attachmentService->updateFile($attachment, $requestData);
68 return response()->json($attachment);
72 * Get the details & content of a single attachment of the given ID.
73 * The attachment link or file content is provided via a 'content' property.
74 * For files the content will be base64 encoded.
76 * @throws FileNotFoundException
78 public function read(string $id)
80 /** @var Attachment $attachment */
81 $attachment = Attachment::visible()
82 ->with(['createdBy', 'updatedBy'])
85 $attachment->setAttribute('links', [
86 'html' => $attachment->htmlLink(),
87 'markdown' => $attachment->markdownLink(),
90 if (!$attachment->external) {
91 $attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment);
92 $attachment->setAttribute('content', base64_encode($attachmentContents));
94 $attachment->setAttribute('content', $attachment->path);
97 return response()->json($attachment);
101 * Update the details of a single attachment.
102 * As per the create endpoint, if a file is being provided as the attachment content
103 * the request should be formatted as a multipart/form-data request instead of JSON.
105 * @throws ValidationException
106 * @throws FileUploadException
108 public function update(Request $request, string $id)
110 $requestData = $this->validate($request, $this->rules()['update']);
111 /** @var Attachment $attachment */
112 $attachment = Attachment::visible()->findOrFail($id);
114 $page = $attachment->page;
115 if ($requestData['uploaded_to'] ?? false) {
116 $pageId = $request->get('uploaded_to');
117 $page = Page::visible()->findOrFail($pageId);
118 $attachment->uploaded_to = $requestData['uploaded_to'];
121 $this->checkOwnablePermission('page-view', $page);
122 $this->checkOwnablePermission('page-update', $page);
123 $this->checkOwnablePermission('attachment-update', $attachment);
125 if ($request->hasFile('file')) {
126 $uploadedFile = $request->file('file');
127 $attachment = $this->attachmentService->saveUpdatedUpload($uploadedFile, $attachment);
130 $this->attachmentService->updateFile($attachment, $requestData);
132 return response()->json($attachment);
136 * Delete an attachment of the given ID.
140 public function delete(string $id)
142 /** @var Attachment $attachment */
143 $attachment = Attachment::visible()->findOrFail($id);
144 $this->checkOwnablePermission('attachment-delete', $attachment);
146 $this->attachmentService->deleteFile($attachment);
148 return response('', 204);
151 protected function rules(): array
155 'name' => ['required', 'min:1', 'max:255', 'string'],
156 'uploaded_to' => ['required', 'integer', 'exists:pages,id'],
157 'file' => array_merge(['required_without:link'], $this->attachmentService->getFileValidationRules()),
158 'link' => ['required_without:file', 'min:1', 'max:255', 'safe_url'],
161 'name' => ['min:1', 'max:255', 'string'],
162 'uploaded_to' => ['integer', 'exists:pages,id'],
163 'file' => $this->attachmentService->getFileValidationRules(),
164 'link' => ['min:1', 'max:255', 'safe_url'],