]> BookStack Code Mirror - bookstack/blob - app/Repos/ImageRepo.php
replace GPL diff lib with MIT lib
[bookstack] / app / Repos / ImageRepo.php
1 <?php namespace BookStack\Repos;
2
3
4 use BookStack\Image;
5 use BookStack\Page;
6 use BookStack\Services\ImageService;
7 use BookStack\Services\PermissionService;
8 use Setting;
9 use Symfony\Component\HttpFoundation\File\UploadedFile;
10
11 class ImageRepo
12 {
13
14     protected $image;
15     protected $imageService;
16     protected $restictionService;
17     protected $page;
18
19     /**
20      * ImageRepo constructor.
21      * @param Image $image
22      * @param ImageService $imageService
23      * @param PermissionService $permissionService
24      * @param Page $page
25      */
26     public function __construct(Image $image, ImageService $imageService, PermissionService $permissionService, Page $page)
27     {
28         $this->image = $image;
29         $this->imageService = $imageService;
30         $this->restictionService = $permissionService;
31         $this->page = $page;
32     }
33
34
35     /**
36      * Get an image with the given id.
37      * @param $id
38      * @return mixed
39      */
40     public function getById($id)
41     {
42         return $this->image->findOrFail($id);
43     }
44
45     /**
46      * Execute a paginated query, returning in a standard format.
47      * Also runs the query through the restriction system.
48      * @param $query
49      * @param int $page
50      * @param int $pageSize
51      * @return array
52      */
53     private function returnPaginated($query, $page = 0, $pageSize = 24)
54     {
55         $images = $this->restictionService->filterRelatedPages($query, 'images', 'uploaded_to');
56         $images = $images->orderBy('created_at', 'desc')->skip($pageSize * $page)->take($pageSize + 1)->get();
57         $hasMore = count($images) > $pageSize;
58
59         $returnImages = $images->take(24);
60         $returnImages->each(function ($image) {
61             $this->loadThumbs($image);
62         });
63
64         return [
65             'images'  => $returnImages,
66             'hasMore' => $hasMore
67         ];
68     }
69
70     /**
71      * Gets a load images paginated, filtered by image type.
72      * @param string $type
73      * @param int $page
74      * @param int $pageSize
75      * @param bool|int $userFilter
76      * @return array
77      */
78     public function getPaginatedByType($type, $page = 0, $pageSize = 24, $userFilter = false)
79     {
80         $images = $this->image->where('type', '=', strtolower($type));
81
82         if ($userFilter !== false) {
83             $images = $images->where('created_by', '=', $userFilter);
84         }
85
86         return $this->returnPaginated($images, $page, $pageSize);
87     }
88
89     /**
90      * Search for images by query, of a particular type.
91      * @param string $type
92      * @param int $page
93      * @param int $pageSize
94      * @param string $searchTerm
95      * @return array
96      */
97     public function searchPaginatedByType($type, $page = 0, $pageSize = 24, $searchTerm)
98     {
99         $images = $this->image->where('type', '=', strtolower($type))->where('name', 'LIKE', '%' . $searchTerm . '%');
100         return $this->returnPaginated($images, $page, $pageSize);
101     }
102
103     /**
104      * Get gallery images with a particular filter criteria such as
105      * being within the current book or page.
106      * @param int $pagination
107      * @param int $pageSize
108      * @param $filter
109      * @param $pageId
110      * @return array
111      */
112     public function getGalleryFiltered($pagination = 0, $pageSize = 24, $filter, $pageId)
113     {
114         $images = $this->image->where('type', '=', 'gallery');
115
116         $page = $this->page->findOrFail($pageId);
117
118         if ($filter === 'page') {
119             $images = $images->where('uploaded_to', '=', $page->id);
120         } elseif ($filter === 'book') {
121             $validPageIds = $page->book->pages->pluck('id')->toArray();
122             $images = $images->whereIn('uploaded_to', $validPageIds);
123         }
124
125         return $this->returnPaginated($images, $pagination, $pageSize);
126     }
127
128     /**
129      * Save a new image into storage and return the new image.
130      * @param UploadedFile $uploadFile
131      * @param  string $type
132      * @param int $uploadedTo
133      * @return Image
134      */
135     public function saveNew(UploadedFile $uploadFile, $type, $uploadedTo = 0)
136     {
137         $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo);
138         $this->loadThumbs($image);
139         return $image;
140     }
141
142     /**
143      * Update the details of an image via an array of properties.
144      * @param Image $image
145      * @param array $updateDetails
146      * @return Image
147      */
148     public function updateImageDetails(Image $image, $updateDetails)
149     {
150         $image->fill($updateDetails);
151         $image->save();
152         $this->loadThumbs($image);
153         return $image;
154     }
155
156
157     /**
158      * Destroys an Image object along with its files and thumbnails.
159      * @param Image $image
160      * @return bool
161      */
162     public function destroyImage(Image $image)
163     {
164         $this->imageService->destroyImage($image);
165         return true;
166     }
167
168
169     /**
170      * Load thumbnails onto an image object.
171      * @param Image $image
172      */
173     private function loadThumbs(Image $image)
174     {
175         $image->thumbs = [
176             'gallery' => $this->getThumbnail($image, 150, 150),
177             'display' => $this->getThumbnail($image, 840, 0, true)
178         ];
179     }
180
181     /**
182      * Get the thumbnail for an image.
183      * If $keepRatio is true only the width will be used.
184      * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
185      *
186      * @param Image $image
187      * @param int $width
188      * @param int $height
189      * @param bool $keepRatio
190      * @return string
191      */
192     public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
193     {
194         return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
195     }
196
197
198 }