3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Uploads\Image;
6 use BookStack\Uploads\ImageRepo;
7 use Illuminate\Http\Request;
9 class ImageGalleryApiController extends ApiController
11 protected array $fieldsToExpose = [
12 'id', 'name', 'url', 'path', 'type', 'uploaded_to', 'created_by', 'updated_by', 'created_at', 'updated_at',
15 public function __construct(
16 protected ImageRepo $imageRepo
20 protected function rules(): array
24 'type' => ['required', 'string', 'in:gallery,drawio'],
25 'uploaded_to' => ['required', 'integer', 'exists:pages,id'],
26 'image' => ['required', 'file', ...$this->getImageValidationRules()],
27 'name' => ['string', 'max:180'],
30 'name' => ['string', 'max:180'],
36 * Get a listing of gallery images and drawings in the system.
37 * Requires visibility of the content they're originally uploaded to.
39 public function list()
41 $images = Image::query()->scopes(['visible'])
42 ->select($this->fieldsToExpose)
43 ->whereIn('type', ['gallery', 'drawio']);
45 return $this->apiListingResponse($images, [
46 ...$this->fieldsToExpose
51 * Create a new image in the system.
53 public function create(Request $request)
55 $data = $this->validate($request, $this->rules()['create']);
57 $image = $this->imageRepo->saveNew($data['image'], $data['type'], $data['uploaded_to']);
59 return response()->json($this->formatForSingleResponse($image));
63 * View the details of a single image.
65 public function read(string $id)
67 $image = $this->imageRepo->getById($id);
68 $this->checkOwnablePermission('page-view', $image->getPage());
70 return response()->json($this->formatForSingleResponse($image));
74 * Update an existing image in the system.
76 public function update(Request $request, string $id)
78 $data = $this->validate($request, $this->rules()['update']);
79 $image = $this->imageRepo->getById($id);
80 $this->checkOwnablePermission('page-view', $image->getPage());
81 $this->checkOwnablePermission('image-update', $image);
83 $this->imageRepo->updateImageDetails($image, $data);
85 return response()->json($this->formatForSingleResponse($image));
89 * Delete an image from the system.
91 public function delete(string $id)
93 $image = $this->imageRepo->getById($id);
94 $this->checkOwnablePermission('page-view', $image->getPage());
95 $this->checkOwnablePermission('image-delete', $image);
96 $this->imageRepo->destroyImage($image);
98 return response('', 204);
102 * Format the given image model for single-result display.
104 protected function formatForSingleResponse(Image $image): array
106 $this->imageRepo->loadThumbs($image);
107 $data = $image->getAttributes();
108 $data['created_by'] = $image->createdBy;
109 $data['updated_by'] = $image->updatedBy;
110 $data['content'] = [];
112 $escapedUrl = htmlentities($image->url);
113 $escapedName = htmlentities($image->name);
114 if ($image->type === 'drawio') {
115 $data['content']['html'] = "<div drawio-diagram=\"{$image->id}\"><img src=\"{$escapedUrl}\"></div>";
116 $data['content']['markdown'] = $data['content']['html'];
118 $escapedDisplayThumb = htmlentities($image->thumbs['display']);
119 $data['content']['html'] = "<a href=\"{$escapedUrl}\" target=\"_blank\"><img src=\"{$escapedDisplayThumb}\" alt=\"{$escapedName}\"></a>";
120 $mdEscapedName = str_replace(']', '', str_replace('[', '', $image->name));
121 $mdEscapedThumb = str_replace(']', '', str_replace('[', '', $image->thumbs['display']));
122 $data['content']['markdown'] = "";