3 namespace BookStack\Uploads\Controllers;
5 use BookStack\Entities\Queries\PageQueries;
6 use BookStack\Exceptions\FileUploadException;
7 use BookStack\Http\ApiController;
8 use BookStack\Uploads\Attachment;
9 use BookStack\Uploads\AttachmentService;
11 use Illuminate\Contracts\Filesystem\FileNotFoundException;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\ValidationException;
15 class AttachmentApiController extends ApiController
17 public function __construct(
18 protected AttachmentService $attachmentService,
19 protected PageQueries $pageQueries,
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 = $this->pageQueries->findVisibleByIdOrFail($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 // Simply return a JSON response of the attachment for link-based attachments
91 if ($attachment->external) {
92 $attachment->setAttribute('content', $attachment->path);
94 return response()->json($attachment);
97 // Build and split our core JSON, at point of content.
98 $splitter = 'CONTENT_SPLIT_LOCATION_' . time() . '_' . rand(1, 40000);
99 $attachment->setAttribute('content', $splitter);
100 $json = $attachment->toJson();
101 $jsonParts = explode($splitter, $json);
102 // Get a stream for the file data from storage
103 $stream = $this->attachmentService->streamAttachmentFromStorage($attachment);
105 return response()->stream(function () use ($jsonParts, $stream) {
106 // Output the pre-content JSON data
109 // Stream out our attachment data as base64 content
110 stream_filter_append($stream, 'convert.base64-encode', STREAM_FILTER_READ);
114 // Output our post-content JSON data
116 }, 200, ['Content-Type' => 'application/json']);
120 * Update the details of a single attachment.
121 * As per the create endpoint, if a file is being provided as the attachment content
122 * the request should be formatted as a multipart/form-data request instead of JSON.
124 * @throws ValidationException
125 * @throws FileUploadException
127 public function update(Request $request, string $id)
129 $requestData = $this->validate($request, $this->rules()['update']);
130 /** @var Attachment $attachment */
131 $attachment = Attachment::visible()->findOrFail($id);
133 $page = $attachment->page;
134 if ($requestData['uploaded_to'] ?? false) {
135 $pageId = $request->get('uploaded_to');
136 $page = $this->pageQueries->findVisibleByIdOrFail($pageId);
137 $attachment->uploaded_to = $requestData['uploaded_to'];
140 $this->checkOwnablePermission('page-view', $page);
141 $this->checkOwnablePermission('page-update', $page);
142 $this->checkOwnablePermission('attachment-update', $attachment);
144 if ($request->hasFile('file')) {
145 $uploadedFile = $request->file('file');
146 $attachment = $this->attachmentService->saveUpdatedUpload($uploadedFile, $attachment);
149 $this->attachmentService->updateFile($attachment, $requestData);
151 return response()->json($attachment);
155 * Delete an attachment of the given ID.
159 public function delete(string $id)
161 /** @var Attachment $attachment */
162 $attachment = Attachment::visible()->findOrFail($id);
163 $this->checkOwnablePermission('attachment-delete', $attachment);
165 $this->attachmentService->deleteFile($attachment);
167 return response('', 204);
170 protected function rules(): array
174 'name' => ['required', 'min:1', 'max:255', 'string'],
175 'uploaded_to' => ['required', 'integer', 'exists:pages,id'],
176 'file' => array_merge(['required_without:link'], $this->attachmentService->getFileValidationRules()),
177 'link' => ['required_without:file', 'min:1', 'max:2000', 'safe_url'],
180 'name' => ['min:1', 'max:255', 'string'],
181 'uploaded_to' => ['integer', 'exists:pages,id'],
182 'file' => $this->attachmentService->getFileValidationRules(),
183 'link' => ['min:1', 'max:2000', 'safe_url'],