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