]> BookStack Code Mirror - bookstack/blobdiff - app/Services/ImageService.php
Added Swedish locale to config
[bookstack] / app / Services / ImageService.php
index dfe2cf453705e07a7b311b73b0f2bffaa28bfb6a..43375ee094355dd214e7cb9e77af1d328438de8b 100644 (file)
@@ -46,7 +46,6 @@ class ImageService extends UploadService
         return $this->saveNew($imageName, $imageData, $type, $uploadedTo);
     }
 
-
     /**
      * Gets an image from url and saves it to the database.
      * @param             $url
@@ -59,7 +58,7 @@ class ImageService extends UploadService
     {
         $imageName = $imageName ? $imageName : basename($url);
         $imageData = file_get_contents($url);
-        if($imageData === false) throw new \Exception('Cannot get image from ' . $url);
+        if($imageData === false) throw new \Exception(trans('errors.cannot_get_image_from_url', ['url' => $url]));
         return $this->saveNew($imageName, $imageData, $type);
     }
 
@@ -82,8 +81,6 @@ class ImageService extends UploadService
 
         $imagePath = '/uploads/images/' . $type . '/' . Date('Y-m-M') . '/';
 
-        if ($this->isLocal()) $imagePath = '/public' . $imagePath;
-
         while ($storage->exists($imagePath . $imageName)) {
             $imageName = str_random(3) . $imageName;
         }
@@ -93,11 +90,9 @@ class ImageService extends UploadService
             $storage->put($fullPath, $imageData);
             $storage->setVisibility($fullPath, 'public');
         } catch (Exception $e) {
-            throw new ImageUploadException('Image Path ' . $fullPath . ' is not writable by the server.');
+            throw new ImageUploadException(trans('errors.path_not_writable', ['filePath' => $fullPath]));
         }
 
-        if ($this->isLocal()) $fullPath = str_replace_first('/public', '', $fullPath);
-
         $imageDetails = [
             'name'       => $imageName,
             'path'       => $fullPath,
@@ -112,8 +107,8 @@ class ImageService extends UploadService
             $imageDetails['updated_by'] = $userId;
         }
 
-        $image = Image::forceCreate($imageDetails);
-
+        $image = (new Image());
+        $image->forceFill($imageDetails)->save();
         return $image;
     }
 
@@ -124,14 +119,13 @@ class ImageService extends UploadService
      */
     protected function getPath(Image $image)
     {
-        return ($this->isLocal()) ? ('public/' . $image->path) : $image->path;
+        return $image->path;
     }
 
     /**
      * Get the thumbnail for an image.
      * If $keepRatio is true only the width will be used.
      * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
-     *
      * @param Image $image
      * @param int $width
      * @param int $height
@@ -151,7 +145,6 @@ class ImageService extends UploadService
         }
 
         $storage = $this->getStorage();
-
         if ($storage->exists($thumbFilePath)) {
             return $this->getPublicUrl($thumbFilePath);
         }
@@ -160,10 +153,9 @@ class ImageService extends UploadService
             $thumb = $this->imageTool->make($storage->get($imagePath));
         } catch (Exception $e) {
             if ($e instanceof \ErrorException || $e instanceof NotSupportedException) {
-                throw new ImageUploadException('The server cannot create thumbnails. Please check you have the GD PHP extension installed.');
-            } else {
-                throw $e;
+                throw new ImageUploadException(trans('errors.cannot_create_thumbs'));
             }
+            throw $e;
         }
 
         if ($keepRatio) {
@@ -252,13 +244,11 @@ class ImageService extends UploadService
                     $storageUrl = 'https://s3-' . $storageDetails['region'] . '.amazonaws.com/' . $storageDetails['bucket'];
                 }
             }
-
             $this->storageUrl = $storageUrl;
         }
 
-        if ($this->isLocal()) $filePath = str_replace_first('public/', '', $filePath);
-
-        return ($this->storageUrl == false ? rtrim(baseUrl(''), '/') : rtrim($this->storageUrl, '/')) . $filePath;
+        $basePath = ($this->storageUrl == false) ? baseUrl('/') : $this->storageUrl;
+        return rtrim($basePath, '/') . $filePath;
     }