diff --git a/app/Http/Controllers/Api/AttachmentApiController.php b/app/Http/Controllers/Api/AttachmentApiController.php new file mode 100644 index 000000000..353cb058d --- /dev/null +++ b/app/Http/Controllers/Api/AttachmentApiController.php @@ -0,0 +1,162 @@ + [ + 'name' => 'required|min:1|max:255|string', + 'uploaded_to' => 'required|integer|exists:pages,id', + 'file' => 'required_without:link|file', + 'link' => 'required_without:file|min:1|max:255|safe_url' + ], + 'update' => [ + 'name' => 'min:1|max:255|string', + 'uploaded_to' => 'integer|exists:pages,id', + 'file' => 'file', + 'link' => 'min:1|max:255|safe_url' + ], + ]; + + public function __construct(AttachmentService $attachmentService) + { + $this->attachmentService = $attachmentService; + } + + /** + * Get a listing of attachments visible to the user. + * The external property indicates whether the attachment is simple a link. + * A false value for the external property would indicate a file upload. + */ + public function list() + { + return $this->apiListingResponse(Attachment::visible(), [ + 'id', 'name', 'extension', 'uploaded_to', 'external', 'order', 'created_at', 'updated_at', 'created_by', 'updated_by', + ]); + } + + /** + * Create a new attachment in the system. + * An uploaded_to value must be provided containing an ID of the page + * that this upload will be related to. + * + * If you're uploading a file the POST data should be provided via + * a multipart/form-data type request instead of JSON. + * + * @throws ValidationException + * @throws FileUploadException + */ + public function create(Request $request) + { + $this->checkPermission('attachment-create-all'); + $requestData = $this->validate($request, $this->rules['create']); + + $pageId = $request->get('uploaded_to'); + $page = Page::visible()->findOrFail($pageId); + $this->checkOwnablePermission('page-update', $page); + + if ($request->hasFile('file')) { + $uploadedFile = $request->file('file'); + $attachment = $this->attachmentService->saveNewUpload($uploadedFile, $page->id); + } else { + $attachment = $this->attachmentService->saveNewFromLink( + $requestData['name'], $requestData['link'], $page->id + ); + } + + $this->attachmentService->updateFile($attachment, $requestData); + return response()->json($attachment); + } + + /** + * Get the details & content of a single attachment of the given ID. + * The attachment link or file content is provided via a 'content' property. + * For files the content will be base64 encoded. + * + * @throws FileNotFoundException + */ + public function read(string $id) + { + /** @var Attachment $attachment */ + $attachment = Attachment::visible() + ->with(['createdBy', 'updatedBy']) + ->findOrFail($id); + + $attachment->setAttribute('links', [ + 'html' => $attachment->htmlLink(), + 'markdown' => $attachment->markdownLink(), + ]); + + if (!$attachment->external) { + $attachmentContents = $this->attachmentService->getAttachmentFromStorage($attachment); + $attachment->setAttribute('content', base64_encode($attachmentContents)); + } else { + $attachment->setAttribute('content', $attachment->path); + } + + return response()->json($attachment); + } + + /** + * Update the details of a single attachment. + * As per the create endpoint, if a file is being provided as the attachment content + * the request should be formatted as a multipart/form-data request instead of JSON. + * + * @throws ValidationException + * @throws FileUploadException + */ + public function update(Request $request, string $id) + { + $requestData = $this->validate($request, $this->rules['update']); + /** @var Attachment $attachment */ + $attachment = Attachment::visible()->findOrFail($id); + + $page = $attachment->page; + if ($requestData['uploaded_to'] ?? false) { + $pageId = $request->get('uploaded_to'); + $page = Page::visible()->findOrFail($pageId); + $attachment->uploaded_to = $requestData['uploaded_to']; + } + + $this->checkOwnablePermission('page-view', $page); + $this->checkOwnablePermission('page-update', $page); + $this->checkOwnablePermission('attachment-update', $attachment); + + if ($request->hasFile('file')) { + $uploadedFile = $request->file('file'); + $attachment = $this->attachmentService->saveUpdatedUpload($uploadedFile, $attachment); + } + + $this->attachmentService->updateFile($attachment, $requestData); + return response()->json($attachment); + } + + /** + * Delete an attachment of the given ID. + * + * @throws Exception + */ + public function delete(string $id) + { + /** @var Attachment $attachment */ + $attachment = Attachment::visible()->findOrFail($id); + $this->checkOwnablePermission('attachment-delete', $attachment); + + $this->attachmentService->deleteFile($attachment); + + return response('', 204); + } + +} diff --git a/app/Http/Controllers/AttachmentController.php b/app/Http/Controllers/AttachmentController.php index 046b8c19d..56503a694 100644 --- a/app/Http/Controllers/AttachmentController.php +++ b/app/Http/Controllers/AttachmentController.php @@ -121,9 +121,9 @@ class AttachmentController extends Controller ]), 422); } - $this->checkOwnablePermission('view', $attachment->page); + $this->checkOwnablePermission('page-view', $attachment->page); $this->checkOwnablePermission('page-update', $attachment->page); - $this->checkOwnablePermission('attachment-create', $attachment); + $this->checkOwnablePermission('attachment-update', $attachment); $attachment = $this->attachmentService->updateFile($attachment, [ 'name' => $request->get('attachment_edit_name'), diff --git a/app/Uploads/Attachment.php b/app/Uploads/Attachment.php index 5acd4f141..410a7d4dd 100644 --- a/app/Uploads/Attachment.php +++ b/app/Uploads/Attachment.php @@ -2,24 +2,37 @@ namespace BookStack\Uploads; +use BookStack\Auth\Permissions\PermissionService; +use BookStack\Auth\User; +use BookStack\Entities\Models\Entity; use BookStack\Entities\Models\Page; use BookStack\Model; use BookStack\Traits\HasCreatorAndUpdater; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\BelongsTo; /** - * @property int id - * @property string name - * @property string path - * @property string extension - * @property ?Page page - * @property bool external + * @property int $id + * @property string $name + * @property string $path + * @property string $extension + * @property ?Page $page + * @property bool $external + * @property int $uploaded_to + * @property User $updatedBy + * @property User $createdBy + * + * @method static Entity|Builder visible() */ class Attachment extends Model { use HasCreatorAndUpdater; protected $fillable = ['name', 'order']; + protected $hidden = ['path', 'page']; + protected $casts = [ + 'external' => 'bool', + ]; /** * Get the downloadable file name for this upload. @@ -70,4 +83,18 @@ class Attachment extends Model { return '[' . $this->name . '](' . $this->getUrl() . ')'; } + + /** + * Scope the query to those attachments that are visible based upon related page permissions. + */ + public function scopeVisible(): Builder + { + $permissionService = app()->make(PermissionService::class); + return $permissionService->filterRelatedEntity( + Page::class, + Attachment::query(), + 'attachments', + 'uploaded_to' + ); + } } diff --git a/app/Uploads/AttachmentService.php b/app/Uploads/AttachmentService.php index 3de0a0dae..2ad1663ff 100644 --- a/app/Uploads/AttachmentService.php +++ b/app/Uploads/AttachmentService.php @@ -78,18 +78,18 @@ class AttachmentService * * @throws FileUploadException */ - public function saveNewUpload(UploadedFile $uploadedFile, int $page_id): Attachment + public function saveNewUpload(UploadedFile $uploadedFile, int $pageId): Attachment { $attachmentName = $uploadedFile->getClientOriginalName(); $attachmentPath = $this->putFileInStorage($uploadedFile); - $largestExistingOrder = Attachment::query()->where('uploaded_to', '=', $page_id)->max('order'); + $largestExistingOrder = Attachment::query()->where('uploaded_to', '=', $pageId)->max('order'); /** @var Attachment $attachment */ $attachment = Attachment::query()->forceCreate([ 'name' => $attachmentName, 'path' => $attachmentPath, 'extension' => $uploadedFile->getClientOriginalExtension(), - 'uploaded_to' => $page_id, + 'uploaded_to' => $pageId, 'created_by' => user()->id, 'updated_by' => user()->id, 'order' => $largestExistingOrder + 1, @@ -159,18 +159,19 @@ class AttachmentService public function updateFile(Attachment $attachment, array $requestData): Attachment { $attachment->name = $requestData['name']; + $link = trim($requestData['link'] ?? ''); - if (isset($requestData['link']) && trim($requestData['link']) !== '') { - $attachment->path = $requestData['link']; + if (!empty($link)) { if (!$attachment->external) { $this->deleteFileInStorage($attachment); $attachment->external = true; + $attachment->extension = ''; } + $attachment->path = $requestData['link']; } $attachment->save(); - - return $attachment; + return $attachment->refresh(); } /** @@ -180,13 +181,10 @@ class AttachmentService */ public function deleteFile(Attachment $attachment) { - if ($attachment->external) { - $attachment->delete(); - - return; + if (!$attachment->external) { + $this->deleteFileInStorage($attachment); } - $this->deleteFileInStorage($attachment); $attachment->delete(); } diff --git a/dev/api/requests/attachments-create.json b/dev/api/requests/attachments-create.json new file mode 100644 index 000000000..8ed34b24e --- /dev/null +++ b/dev/api/requests/attachments-create.json @@ -0,0 +1,5 @@ +{ + "name": "My uploaded attachment", + "uploaded_to": 8, + "link": "https://link.example.com" +} \ No newline at end of file diff --git a/dev/api/requests/attachments-update.json b/dev/api/requests/attachments-update.json new file mode 100644 index 000000000..062050b3a --- /dev/null +++ b/dev/api/requests/attachments-update.json @@ -0,0 +1,5 @@ +{ + "name": "My updated attachment", + "uploaded_to": 4, + "link": "https://link.example.com/updated" +} \ No newline at end of file diff --git a/dev/api/responses/attachments-create.json b/dev/api/responses/attachments-create.json new file mode 100644 index 000000000..5af524e1a --- /dev/null +++ b/dev/api/responses/attachments-create.json @@ -0,0 +1,12 @@ +{ + "id": 5, + "name": "My uploaded attachment", + "extension": "", + "uploaded_to": 8, + "external": true, + "order": 2, + "created_by": 1, + "updated_by": 1, + "created_at": "2021-10-20 06:35:46", + "updated_at": "2021-10-20 06:35:46" +} \ No newline at end of file diff --git a/dev/api/responses/attachments-list.json b/dev/api/responses/attachments-list.json new file mode 100644 index 000000000..946dd542a --- /dev/null +++ b/dev/api/responses/attachments-list.json @@ -0,0 +1,29 @@ +{ + "data": [ + { + "id": 3, + "name": "datasheet.pdf", + "extension": "pdf", + "uploaded_to": 8, + "external": false, + "order": 1, + "created_at": "2021-10-11 06:18:49", + "updated_at": "2021-10-20 06:31:10", + "created_by": 1, + "updated_by": 1 + }, + { + "id": 4, + "name": "Cat reference", + "extension": "", + "uploaded_to": 9, + "external": true, + "order": 1, + "created_at": "2021-10-20 06:30:11", + "updated_at": "2021-10-20 06:30:11", + "created_by": 1, + "updated_by": 1 + } + ], + "total": 2 +} \ No newline at end of file diff --git a/dev/api/responses/attachments-read.json b/dev/api/responses/attachments-read.json new file mode 100644 index 000000000..e22f4e5fe --- /dev/null +++ b/dev/api/responses/attachments-read.json @@ -0,0 +1,25 @@ +{ + "id": 5, + "name": "My link attachment", + "extension": "", + "uploaded_to": 4, + "external": true, + "order": 2, + "created_by": { + "id": 1, + "name": "Admin", + "slug": "admin" + }, + "updated_by": { + "id": 1, + "name": "Admin", + "slug": "admin" + }, + "created_at": "2021-10-20 06:35:46", + "updated_at": "2021-10-20 06:37:11", + "links": { + "html": "My updated attachment", + "markdown": "[My updated attachment](https://bookstack.local/attachments/5)" + }, + "content": "https://link.example.com/updated" +} \ No newline at end of file diff --git a/dev/api/responses/attachments-update.json b/dev/api/responses/attachments-update.json new file mode 100644 index 000000000..8054b0e48 --- /dev/null +++ b/dev/api/responses/attachments-update.json @@ -0,0 +1,12 @@ +{ + "id": 5, + "name": "My updated attachment", + "extension": "", + "uploaded_to": 4, + "external": true, + "order": 2, + "created_by": 1, + "updated_by": 1, + "created_at": "2021-10-20 06:35:46", + "updated_at": "2021-10-20 06:37:11" +} \ No newline at end of file diff --git a/resources/views/api-docs/parts/getting-started.blade.php b/resources/views/api-docs/parts/getting-started.blade.php index ba0f85fc7..ca28a7d90 100644 --- a/resources/views/api-docs/parts/getting-started.blade.php +++ b/resources/views/api-docs/parts/getting-started.blade.php @@ -44,7 +44,7 @@
Parameter | +Parameter | Details | Examples |
---|