3 namespace BookStack\Http\Controllers;
5 use Illuminate\Filesystem\Filesystem as File;
6 use Illuminate\Http\Request;
7 use Illuminate\Support\Facades\Auth;
8 use Intervention\Image\Facades\Image as ImageTool;
9 use Illuminate\Support\Facades\DB;
11 use BookStack\Repos\PageRepo;
13 class ImageController extends Controller
19 * ImageController constructor.
23 public function __construct(Image $image, File $file)
25 $this->image = $image;
27 parent::__construct();
32 * Get all images, Paginated
34 * @return \Illuminate\Http\JsonResponse
36 public function getAll($page = 0)
39 $images = $this->image->orderBy('created_at', 'desc')
40 ->skip($page * $pageSize)->take($pageSize)->get();
41 foreach ($images as $image) {
42 $image->thumbnail = $this->getThumbnail($image, 150, 150);
44 $hasMore = $this->image->orderBy('created_at', 'desc')
45 ->skip(($page + 1) * $pageSize)->take($pageSize)->count() > 0;
46 return response()->json([
53 * Get the thumbnail for an image.
59 public function getThumbnail($image, $width = 220, $height = 220)
61 $explodedPath = explode('/', $image->url);
62 array_splice($explodedPath, 4, 0, ['thumbs-' . $width . '-' . $height]);
63 $thumbPath = implode('/', $explodedPath);
64 $thumbFilePath = public_path() . $thumbPath;
66 // Return the thumbnail url path if already exists
67 if (file_exists($thumbFilePath)) {
71 // Otherwise create the thumbnail
72 $thumb = ImageTool::make(public_path() . $image->url);
73 $thumb->fit($width, $height);
75 // Create thumbnail folder if it does not exist
76 if (!file_exists(dirname($thumbFilePath))) {
77 mkdir(dirname($thumbFilePath), 0775, true);
81 $thumb->save($thumbFilePath);
86 * Handles image uploads for use on pages.
87 * @param Request $request
88 * @return \Illuminate\Http\JsonResponse
90 public function upload(Request $request)
92 $this->checkPermission('image-create');
93 $imageUpload = $request->file('file');
94 $name = str_replace(' ', '-', $imageUpload->getClientOriginalName());
95 $storageName = substr(sha1(time()), 0, 10) . '-' . $name;
96 $imagePath = '/uploads/images/' . Date('Y-m-M') . '/';
97 $storagePath = public_path() . $imagePath;
98 $fullPath = $storagePath . $storageName;
99 while (file_exists($fullPath)) {
100 $storageName = substr(sha1(rand()), 0, 3) . $storageName;
101 $fullPath = $storagePath . $storageName;
103 $imageUpload->move($storagePath, $storageName);
104 // Create and save image object
105 $this->image->name = $name;
106 $this->image->url = $imagePath . $storageName;
107 $this->image->created_by = auth()->user()->id;
108 $this->image->updated_by = auth()->user()->id;
109 $this->image->save();
110 $this->image->thumbnail = $this->getThumbnail($this->image, 150, 150);
111 return response()->json($this->image);
115 * Update image details
117 * @param Request $request
118 * @return \Illuminate\Http\JsonResponse
120 public function update($imageId, Request $request)
122 $this->checkPermission('image-update');
123 $this->validate($request, [
124 'name' => 'required|min:2|string'
126 $image = $this->image->findOrFail($imageId);
127 $image->fill($request->all());
129 return response()->json($this->image);
133 * Deletes an image and all thumbnail/image files
134 * @param PageRepo $pageRepo
135 * @param Request $request
137 * @return \Illuminate\Http\JsonResponse
139 public function destroy(PageRepo $pageRepo, Request $request, $id)
141 $this->checkPermission('image-delete');
142 $image = $this->image->findOrFail($id);
144 // Check if this image is used on any pages
145 $pageSearch = $pageRepo->searchForImage($image->url);
146 $isForced = ($request->has('force') && ($request->get('force') === 'true') || $request->get('force') === true);
147 if ($pageSearch !== false && !$isForced) {
148 return response()->json($pageSearch, 400);
152 $folder = public_path() . dirname($image->url);
153 $fileName = basename($image->url);
156 foreach (glob($folder . '/*') as $file) {
158 $thumbName = $file . '/' . $fileName;
159 if (file_exists($file)) {
162 // Remove thumb folder if empty
163 if (count(glob($file . '/*')) === 0) {
169 // Delete file and database entry
170 unlink($folder . '/' . $fileName);
173 // Delete parent folder if empty
174 if (count(glob($folder . '/*')) === 0) {
177 return response()->json('Image Deleted');