]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ImageController.php
Added limit to books shown on homepage and make alphabetical
[bookstack] / app / Http / Controllers / ImageController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
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;
10 use BookStack\Image;
11 use BookStack\Repos\PageRepo;
12
13 class ImageController extends Controller
14 {
15     protected $image;
16     protected $file;
17
18     /**
19      * ImageController constructor.
20      * @param Image $image
21      * @param File  $file
22      */
23     public function __construct(Image $image, File $file)
24     {
25         $this->image = $image;
26         $this->file = $file;
27         parent::__construct();
28     }
29
30
31     /**
32      * Get all images, Paginated
33      * @param int $page
34      * @return \Illuminate\Http\JsonResponse
35      */
36     public function getAll($page = 0)
37     {
38         $pageSize = 30;
39         $images = $this->image->orderBy('created_at', 'desc')
40             ->skip($page * $pageSize)->take($pageSize)->get();
41         foreach ($images as $image) {
42             $this->loadSizes($image);
43         }
44         $hasMore = $this->image->orderBy('created_at', 'desc')
45                 ->skip(($page + 1) * $pageSize)->take($pageSize)->count() > 0;
46         return response()->json([
47             'images' => $images,
48             'hasMore' => $hasMore
49         ]);
50     }
51
52     /**
53      * Loads the standard thumbnail sizes for an image.
54      * @param Image $image
55      */
56     private function loadSizes(Image $image)
57     {
58         $image->thumbnail = $this->getThumbnail($image, 150, 150);
59         $image->display = $this->getThumbnail($image, 840, 0, true);
60     }
61
62     /**
63      * Get the thumbnail for an image.
64      * If $keepRatio is true only the width will be used.
65      * @param      $image
66      * @param int  $width
67      * @param int  $height
68      * @param bool $keepRatio
69      * @return string
70      */
71     public function getThumbnail($image, $width = 220, $height = 220, $keepRatio = false)
72     {
73         $explodedPath = explode('/', $image->url);
74         $dirPrefix = $keepRatio ? 'scaled-' : 'thumbs-';
75         array_splice($explodedPath, 4, 0, [$dirPrefix . $width . '-' . $height]);
76         $thumbPath = implode('/', $explodedPath);
77         $thumbFilePath = public_path() . $thumbPath;
78
79         // Return the thumbnail url path if already exists
80         if (file_exists($thumbFilePath)) {
81             return $thumbPath;
82         }
83
84         // Otherwise create the thumbnail
85         $thumb = ImageTool::make(public_path() . $image->url);
86         if($keepRatio) {
87             $thumb->resize($width, null, function ($constraint) {
88                 $constraint->aspectRatio();
89                 $constraint->upsize();
90             });
91         } else {
92             $thumb->fit($width, $height);
93         }
94
95         // Create thumbnail folder if it does not exist
96         if (!file_exists(dirname($thumbFilePath))) {
97             mkdir(dirname($thumbFilePath), 0775, true);
98         }
99
100         //Save Thumbnail
101         $thumb->save($thumbFilePath);
102         return $thumbPath;
103     }
104
105     /**
106      * Handles image uploads for use on pages.
107      * @param Request $request
108      * @return \Illuminate\Http\JsonResponse
109      */
110     public function upload(Request $request)
111     {
112         $this->checkPermission('image-create');
113         $imageUpload = $request->file('file');
114         $name = str_replace(' ', '-', $imageUpload->getClientOriginalName());
115         $storageName = substr(sha1(time()), 0, 10) . '-' . $name;
116         $imagePath = '/uploads/images/' . Date('Y-m-M') . '/';
117         $storagePath = public_path() . $imagePath;
118         $fullPath = $storagePath . $storageName;
119         while (file_exists($fullPath)) {
120             $storageName = substr(sha1(rand()), 0, 3) . $storageName;
121             $fullPath = $storagePath . $storageName;
122         }
123         $imageUpload->move($storagePath, $storageName);
124         // Create and save image object
125         $this->image->name = $name;
126         $this->image->url = $imagePath . $storageName;
127         $this->image->created_by = auth()->user()->id;
128         $this->image->updated_by = auth()->user()->id;
129         $this->image->save();
130         $this->loadSizes($this->image);
131         return response()->json($this->image);
132     }
133
134     /**
135      * Update image details
136      * @param         $imageId
137      * @param Request $request
138      * @return \Illuminate\Http\JsonResponse
139      */
140     public function update($imageId, Request $request)
141     {
142         $this->checkPermission('image-update');
143         $this->validate($request, [
144             'name' => 'required|min:2|string'
145         ]);
146         $image = $this->image->findOrFail($imageId);
147         $image->fill($request->all());
148         $image->save();
149         $this->loadSizes($image);
150         return response()->json($this->image);
151     }
152
153     /**
154      * Deletes an image and all thumbnail/image files
155      * @param PageRepo $pageRepo
156      * @param Request  $request
157      * @param int      $id
158      * @return \Illuminate\Http\JsonResponse
159      */
160     public function destroy(PageRepo $pageRepo, Request $request, $id)
161     {
162         $this->checkPermission('image-delete');
163         $image = $this->image->findOrFail($id);
164
165         // Check if this image is used on any pages
166         $pageSearch = $pageRepo->searchForImage($image->url);
167         $isForced = ($request->has('force') && ($request->get('force') === 'true') || $request->get('force') === true);
168         if ($pageSearch !== false && !$isForced) {
169             return response()->json($pageSearch, 400);
170         }
171
172         // Delete files
173         $folder = public_path() . dirname($image->url);
174         $fileName = basename($image->url);
175
176         // Delete thumbnails
177         foreach (glob($folder . '/*') as $file) {
178             if (is_dir($file)) {
179                 $thumbName = $file . '/' . $fileName;
180                 if (file_exists($file)) {
181                     unlink($thumbName);
182                 }
183                 // Remove thumb folder if empty
184                 if (count(glob($file . '/*')) === 0) {
185                     rmdir($file);
186                 }
187             }
188         }
189
190         // Delete file and database entry
191         unlink($folder . '/' . $fileName);
192         $image->delete();
193
194         // Delete parent folder if empty
195         if (count(glob($folder . '/*')) === 0) {
196             rmdir($folder);
197         }
198         return response()->json('Image Deleted');
199     }
200
201
202 }