]> BookStack Code Mirror - bookstack/blobdiff - app/Uploads/ImageRepo.php
Allow toggling between grid and list view in shelf view (shelves.show)
[bookstack] / app / Uploads / ImageRepo.php
index 235889eee330939a563fb93a11db9bb0ec04b699..da0b7d379eddd2fb4a227dd071af248c2ba44d07 100644 (file)
@@ -2,7 +2,6 @@
 
 use BookStack\Auth\Permissions\PermissionService;
 use BookStack\Entities\Page;
-use BookStack\Http\Requests\Request;
 use Illuminate\Database\Eloquent\Builder;
 use Symfony\Component\HttpFoundation\File\UploadedFile;
 
@@ -26,8 +25,7 @@ class ImageRepo
         ImageService $imageService,
         PermissionService $permissionService,
         Page $page
-    )
-    {
+    ) {
         $this->image = $image;
         $this->imageService = $imageService;
         $this->restrictionService = $permissionService;
@@ -38,7 +36,7 @@ class ImageRepo
     /**
      * Get an image with the given id.
      * @param $id
-     * @return mixed
+     * @return Image
      */
     public function getById($id)
     {
@@ -88,8 +86,7 @@ class ImageRepo
         int $uploadedTo = null,
         string $search = null,
         callable $whereClause = null
-    )
-    {
+    ) {
         $imageQuery = $this->image->newQuery()->where('type', '=', strtolower($type));
 
         if ($uploadedTo !== null) {
@@ -100,16 +97,8 @@ class ImageRepo
             $imageQuery = $imageQuery->where('name', 'LIKE', '%' . $search . '%');
         }
 
-        // Filter by page access if gallery
-        if ($type === 'gallery') {
-            $imageQuery = $this->restrictionService->filterRelatedEntity('page', $imageQuery, 'images', 'uploaded_to');
-        }
-
-        // Filter by entity if cover
-        if (strpos($type, 'cover_') === 0) {
-            $entityType = explode('_', $type)[1];
-            $imageQuery = $this->restrictionService->filterRelatedEntity($entityType, $imageQuery, 'images', 'uploaded_to');
-        }
+        // Filter by page access
+        $imageQuery = $this->restrictionService->filterRelatedEntity('page', $imageQuery, 'images', 'uploaded_to');
 
         if ($whereClause !== null) {
             $imageQuery = $imageQuery->where($whereClause);
@@ -135,13 +124,12 @@ class ImageRepo
         int $pageSize = 24,
         int $uploadedTo = null,
         string $search = null
-    )
-    {
+    ) {
         $contextPage = $this->page->findOrFail($uploadedTo);
         $parentFilter = null;
 
         if ($filterType === 'book' || $filterType === 'page') {
-            $parentFilter = function(Builder $query) use ($filterType, $contextPage) {
+            $parentFilter = function (Builder $query) use ($filterType, $contextPage) {
                 if ($filterType === 'page') {
                     $query->where('uploaded_to', '=', $contextPage->id);
                 } elseif ($filterType === 'book') {
@@ -157,15 +145,17 @@ class ImageRepo
     /**
      * Save a new image into storage and return the new image.
      * @param UploadedFile $uploadFile
-     * @param  string $type
+     * @param string $type
      * @param int $uploadedTo
+     * @param int|null $resizeWidth
+     * @param int|null $resizeHeight
+     * @param bool $keepRatio
      * @return Image
      * @throws \BookStack\Exceptions\ImageUploadException
-     * @throws \Exception
      */
-    public function saveNew(UploadedFile $uploadFile, $type, $uploadedTo = 0)
+    public function saveNew(UploadedFile $uploadFile, $type, $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true)
     {
-        $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo);
+        $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
         $this->loadThumbs($image);
         return $image;
     }
@@ -208,12 +198,27 @@ class ImageRepo
      * @return bool
      * @throws \Exception
      */
-    public function destroyImage(Image $image)
+    public function destroyImage(Image $image = null)
     {
-        $this->imageService->destroy($image);
+        if ($image) {
+            $this->imageService->destroy($image);
+        }
         return true;
     }
 
+    /**
+     * Destroy all images of a certain type.
+     * @param string $imageType
+     * @throws \Exception
+     */
+    public function destroyByType(string $imageType)
+    {
+        $images = $this->image->where('type', '=', $imageType)->get();
+        foreach ($images as $image) {
+            $this->destroyImage($image);
+        }
+    }
+
 
     /**
      * Load thumbnails onto an image object.
@@ -224,8 +229,8 @@ class ImageRepo
     protected function loadThumbs(Image $image)
     {
         $image->thumbs = [
-            'gallery' => $this->getThumbnail($image, 150, 150),
-            'display' => $this->getThumbnail($image, 840, 0, true)
+            'gallery' => $this->getThumbnail($image, 150, 150, false),
+            'display' => $this->getThumbnail($image, 1680, null, true)
         ];
     }
 
@@ -241,7 +246,7 @@ class ImageRepo
      * @throws \BookStack\Exceptions\ImageUploadException
      * @throws \Exception
      */
-    public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
+    protected function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
     {
         try {
             return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
@@ -264,18 +269,6 @@ class ImageRepo
         }
     }
 
-    /**
-     * Check if the provided image type is valid.
-     * @param $type
-     * @return bool
-     */
-    public function isValidType($type)
-    {
-        // TODO - To delete?
-        $validTypes = ['gallery', 'cover', 'system', 'user'];
-        return in_array($type, $validTypes);
-    }
-
     /**
      * Get the validation rules for image files.
      * @return string