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;
20 'name' => 'required|min:1|max:255|string',
21 'uploaded_to' => 'required|integer|exists:pages,id',
22 'file' => 'required_without:link|file',
23 'link' => 'required_without:file|min:1|max:255|safe_url'
26 'name' => 'min:1|max:255|string',
27 'uploaded_to' => 'integer|exists:pages,id',
29 'link' => 'min:1|max:255|safe_url'
33 public function __construct(AttachmentService $attachmentService)
35 $this->attachmentService = $attachmentService;
39 * Get a listing of attachments visible to the user.
40 * The external property indicates whether the attachment is simple a link.
41 * A false value for the external property would indicate a file upload.
43 public function list()
45 return $this->apiListingResponse(Attachment::visible(), [
46 'id', 'name', 'extension', 'uploaded_to', 'external', 'order', 'created_at', 'updated_at', 'created_by', 'updated_by',
51 * Create a new attachment in the system.
52 * An uploaded_to value must be provided containing an ID of the page
53 * that this upload will be related to.
55 * If you're uploading a file the POST data should be provided via
56 * a multipart/form-data type request instead of JSON.
58 * @throws ValidationException
59 * @throws FileUploadException
61 public function create(Request $request)
63 $this->checkPermission('attachment-create-all');
64 $requestData = $this->validate($request, $this->rules['create']);
66 $pageId = $request->get('uploaded_to');
67 $page = Page::visible()->findOrFail($pageId);
68 $this->checkOwnablePermission('page-update', $page);
70 if ($request->hasFile('file')) {
71 $uploadedFile = $request->file('file');
72 $attachment = $this->attachmentService->saveNewUpload($uploadedFile, $page->id);
74 $attachment = $this->attachmentService->saveNewFromLink(
75 $requestData['name'], $requestData['link'], $page->id
79 $this->attachmentService->updateFile($attachment, $requestData);
80 return response()->json($attachment);
84 * Get the details & content of a single attachment of the given ID.
85 * The attachment link or file content is provided via a 'content' property.
86 * For files the content will be base64 encoded.
88 * @throws FileNotFoundException
90 public function read(string $id)
92 /** @var Attachment $attachment */
93 $attachment = Attachment::visible()
94 ->with(['createdBy', 'updatedBy'])
97 $attachment->setAttribute('links', [
98 'html' => $attachment->htmlLink(),
99 'markdown' => $attachment->markdownLink(),
102 if (!$attachment->external) {
103 $attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment);
104 $attachment->setAttribute('content', base64_encode($attachmentContents));
106 $attachment->setAttribute('content', $attachment->path);
109 return response()->json($attachment);
113 * Update the details of a single attachment.
114 * As per the create endpoint, if a file is being provided as the attachment content
115 * the request should be formatted as a multipart/form-data request instead of JSON.
117 * @throws ValidationException
118 * @throws FileUploadException
120 public function update(Request $request, string $id)
122 $requestData = $this->validate($request, $this->rules['update']);
123 /** @var Attachment $attachment */
124 $attachment = Attachment::visible()->findOrFail($id);
126 $page = $attachment->page;
127 if ($requestData['uploaded_to'] ?? false) {
128 $pageId = $request->get('uploaded_to');
129 $page = Page::visible()->findOrFail($pageId);
130 $attachment->uploaded_to = $requestData['uploaded_to'];
133 $this->checkOwnablePermission('page-view', $page);
134 $this->checkOwnablePermission('page-update', $page);
135 $this->checkOwnablePermission('attachment-update', $attachment);
137 if ($request->hasFile('file')) {
138 $uploadedFile = $request->file('file');
139 $attachment = $this->attachmentService->saveUpdatedUpload($uploadedFile, $attachment);
142 $this->attachmentService->updateFile($attachment, $requestData);
143 return response()->json($attachment);
147 * Delete an attachment of the given ID.
151 public function delete(string $id)
153 /** @var Attachment $attachment */
154 $attachment = Attachment::visible()->findOrFail($id);
155 $this->checkOwnablePermission('attachment-delete', $attachment);
157 $this->attachmentService->deleteFile($attachment);
159 return response('', 204);