]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ImageController.php
Merge branch 'master' of github.com:ssddanbrown/BookStack
[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             $image->thumbnail = $this->getThumbnail($image, 150, 150);
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      * Get the thumbnail for an image.
54      * @param     $image
55      * @param int $width
56      * @param int $height
57      * @return string
58      */
59     public function getThumbnail($image, $width = 220, $height = 220)
60     {
61         $explodedPath = explode('/', $image->url);
62         array_splice($explodedPath, 4, 0, ['thumbs-' . $width . '-' . $height]);
63         $thumbPath = implode('/', $explodedPath);
64         $thumbFilePath = public_path() . $thumbPath;
65
66         // Return the thumbnail url path if already exists
67         if (file_exists($thumbFilePath)) {
68             return $thumbPath;
69         }
70
71         // Otherwise create the thumbnail
72         $thumb = ImageTool::make(public_path() . $image->url);
73         $thumb->fit($width, $height);
74
75         // Create thumbnail folder if it does not exist
76         if (!file_exists(dirname($thumbFilePath))) {
77             mkdir(dirname($thumbFilePath), 0775, true);
78         }
79
80         //Save Thumbnail
81         $thumb->save($thumbFilePath);
82         return $thumbPath;
83     }
84
85     /**
86      * Handles image uploads for use on pages.
87      * @param Request $request
88      * @return \Illuminate\Http\JsonResponse
89      */
90     public function upload(Request $request)
91     {
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;
102         }
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);
112     }
113
114     /**
115      * Update image details
116      * @param         $imageId
117      * @param Request $request
118      * @return \Illuminate\Http\JsonResponse
119      */
120     public function update($imageId, Request $request)
121     {
122         $this->checkPermission('image-update');
123         $this->validate($request, [
124             'name' => 'required|min:2|string'
125         ]);
126         $image = $this->image->findOrFail($imageId);
127         $image->fill($request->all());
128         $image->save();
129         return response()->json($this->image);
130     }
131
132     /**
133      * Deletes an image and all thumbnail/image files
134      * @param PageRepo $pageRepo
135      * @param Request  $request
136      * @param int      $id
137      * @return \Illuminate\Http\JsonResponse
138      */
139     public function destroy(PageRepo $pageRepo, Request $request, $id)
140     {
141         $this->checkPermission('image-delete');
142         $image = $this->image->findOrFail($id);
143
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);
149         }
150
151         // Delete files
152         $folder = public_path() . dirname($image->url);
153         $fileName = basename($image->url);
154
155         // Delete thumbnails
156         foreach (glob($folder . '/*') as $file) {
157             if (is_dir($file)) {
158                 $thumbName = $file . '/' . $fileName;
159                 if (file_exists($file)) {
160                     unlink($thumbName);
161                 }
162                 // Remove thumb folder if empty
163                 if (count(glob($file . '/*')) === 0) {
164                     rmdir($file);
165                 }
166             }
167         }
168
169         // Delete file and database entry
170         unlink($folder . '/' . $fileName);
171         $image->delete();
172
173         // Delete parent folder if empty
174         if (count(glob($folder . '/*')) === 0) {
175             rmdir($folder);
176         }
177         return response()->json('Image Deleted');
178     }
179
180
181 }