3 namespace BookStack\Uploads\Controllers;
5 use BookStack\Entities\EntityExistsRule;
6 use BookStack\Entities\Queries\PageQueries;
7 use BookStack\Exceptions\FileUploadException;
8 use BookStack\Http\ApiController;
9 use BookStack\Permissions\Permission;
10 use BookStack\Uploads\Attachment;
11 use BookStack\Uploads\AttachmentService;
13 use Illuminate\Contracts\Filesystem\FileNotFoundException;
14 use Illuminate\Http\Request;
15 use Illuminate\Validation\ValidationException;
17 class AttachmentApiController extends ApiController
19 public function __construct(
20 protected AttachmentService $attachmentService,
21 protected PageQueries $pageQueries,
26 * Get a listing of attachments visible to the user.
27 * The external property indicates whether the attachment is simple a link.
28 * A false value for the external property would indicate a file upload.
30 public function list()
32 return $this->apiListingResponse(Attachment::visible(), [
33 'id', 'name', 'extension', 'uploaded_to', 'external', 'order', 'created_at', 'updated_at', 'created_by', 'updated_by',
38 * Create a new attachment in the system.
39 * An uploaded_to value must be provided containing an ID of the page
40 * that this upload will be related to.
42 * If you're uploading a file the POST data should be provided via
43 * a multipart/form-data type request instead of JSON.
45 * @throws ValidationException
46 * @throws FileUploadException
48 public function create(Request $request)
50 $this->checkPermission(Permission::AttachmentCreateAll);
51 $requestData = $this->validate($request, $this->rules()['create']);
53 $pageId = $request->get('uploaded_to');
54 $page = $this->pageQueries->findVisibleByIdOrFail($pageId);
55 $this->checkOwnablePermission(Permission::PageUpdate, $page);
57 if ($request->hasFile('file')) {
58 $uploadedFile = $request->file('file');
59 $attachment = $this->attachmentService->saveNewUpload($uploadedFile, $page->id);
61 $attachment = $this->attachmentService->saveNewFromLink(
68 $this->attachmentService->updateFile($attachment, $requestData);
70 return response()->json($attachment);
74 * Get the details & content of a single attachment of the given ID.
75 * The attachment link or file content is provided via a 'content' property.
76 * For files the content will be base64 encoded.
78 * @throws FileNotFoundException
80 public function read(string $id)
82 /** @var Attachment $attachment */
83 $attachment = Attachment::visible()
84 ->with(['createdBy', 'updatedBy'])
87 $attachment->setAttribute('links', [
88 'html' => $attachment->htmlLink(),
89 'markdown' => $attachment->markdownLink(),
92 // Simply return a JSON response of the attachment for link-based attachments
93 if ($attachment->external) {
94 $attachment->setAttribute('content', $attachment->path);
96 return response()->json($attachment);
99 // Build and split our core JSON, at point of content.
100 $splitter = 'CONTENT_SPLIT_LOCATION_' . time() . '_' . rand(1, 40000);
101 $attachment->setAttribute('content', $splitter);
102 $json = $attachment->toJson();
103 $jsonParts = explode($splitter, $json);
104 // Get a stream for the file data from storage
105 $stream = $this->attachmentService->streamAttachmentFromStorage($attachment);
107 return response()->stream(function () use ($jsonParts, $stream) {
108 // Output the pre-content JSON data
111 // Stream out our attachment data as base64 content
112 stream_filter_append($stream, 'convert.base64-encode', STREAM_FILTER_READ);
116 // Output our post-content JSON data
118 }, 200, ['Content-Type' => 'application/json']);
122 * Update the details of a single attachment.
123 * As per the create endpoint, if a file is being provided as the attachment content
124 * the request should be formatted as a multipart/form-data request instead of JSON.
126 * @throws ValidationException
127 * @throws FileUploadException
129 public function update(Request $request, string $id)
131 $requestData = $this->validate($request, $this->rules()['update']);
132 /** @var Attachment $attachment */
133 $attachment = Attachment::visible()->findOrFail($id);
135 $page = $attachment->page;
136 if ($requestData['uploaded_to'] ?? false) {
137 $pageId = $request->get('uploaded_to');
138 $page = $this->pageQueries->findVisibleByIdOrFail($pageId);
139 $attachment->uploaded_to = $requestData['uploaded_to'];
142 $this->checkOwnablePermission(Permission::PageView, $page);
143 $this->checkOwnablePermission(Permission::PageUpdate, $page);
144 $this->checkOwnablePermission(Permission::AttachmentUpdate, $attachment);
146 if ($request->hasFile('file')) {
147 $uploadedFile = $request->file('file');
148 $attachment = $this->attachmentService->saveUpdatedUpload($uploadedFile, $attachment);
151 $this->attachmentService->updateFile($attachment, $requestData);
153 return response()->json($attachment);
157 * Delete an attachment of the given ID.
161 public function delete(string $id)
163 /** @var Attachment $attachment */
164 $attachment = Attachment::visible()->findOrFail($id);
165 $this->checkOwnablePermission(Permission::AttachmentDelete, $attachment);
167 $this->attachmentService->deleteFile($attachment);
169 return response('', 204);
172 protected function rules(): array
176 'name' => ['required', 'string', 'min:1', 'max:255'],
177 'uploaded_to' => ['required', 'integer', new EntityExistsRule('page')],
178 'file' => array_merge(['required_without:link'], $this->attachmentService->getFileValidationRules()),
179 'link' => ['required_without:file', 'string', 'min:1', 'max:2000', 'safe_url'],
182 'name' => ['string', 'min:1', 'max:255'],
183 'uploaded_to' => ['integer', new EntityExistsRule('page')],
184 'file' => $this->attachmentService->getFileValidationRules(),
185 'link' => ['string', 'min:1', 'max:2000', 'safe_url'],