]> BookStack Code Mirror - bookstack/blobdiff - app/Services/ImageService.php
Improves and adds missing pt_BR strings
[bookstack] / app / Services / ImageService.php
index c087c1e03221f037df53219479345f5184ae51a8..73a677ac23ecf144634b3447aff95c83b45c1fd8 100644 (file)
@@ -3,6 +3,7 @@
 use BookStack\Exceptions\ImageUploadException;
 use BookStack\Image;
 use BookStack\User;
+use DB;
 use Exception;
 use Intervention\Image\Exception\NotSupportedException;
 use Intervention\Image\ImageManager;
@@ -16,15 +17,18 @@ class ImageService extends UploadService
     protected $imageTool;
     protected $cache;
     protected $storageUrl;
+    protected $image;
 
     /**
      * ImageService constructor.
-     * @param $imageTool
-     * @param $fileSystem
-     * @param $cache
+     * @param Image $image
+     * @param ImageManager $imageTool
+     * @param FileSystem $fileSystem
+     * @param Cache $cache
      */
-    public function __construct(ImageManager $imageTool, FileSystem $fileSystem, Cache $cache)
+    public function __construct(Image $image, ImageManager $imageTool, FileSystem $fileSystem, Cache $cache)
     {
+        $this->image = $image;
         $this->imageTool = $imageTool;
         $this->cache = $cache;
         parent::__construct($fileSystem);
@@ -146,7 +150,7 @@ class ImageService extends UploadService
             $imageDetails['updated_by'] = $userId;
         }
 
-        $image = (new Image());
+        $image = $this->image->newInstance();
         $image->forceFill($imageDetails)->save();
         return $image;
     }
@@ -294,6 +298,46 @@ class ImageService extends UploadService
         return $image;
     }
 
+
+    /**
+     * Delete gallery and drawings that are not within HTML content of pages or page revisions.
+     * Checks based off of only the image name.
+     * Could be much improved to be more specific but kept it generic for now to be safe.
+     *
+     * Returns the path of the images that would be/have been deleted.
+     * @param bool $checkRevisions
+     * @param bool $dryRun
+     * @param array $types
+     * @return array
+     */
+    public function deleteUnusedImages($checkRevisions = true, $dryRun = true, $types = ['gallery', 'drawio'])
+    {
+        $types = array_intersect($types, ['gallery', 'drawio']);
+        $deletedPaths = [];
+
+        $this->image->newQuery()->whereIn('type', $types)
+            ->chunk(1000, function($images) use ($types, $checkRevisions, &$deletedPaths, $dryRun) {
+             foreach ($images as $image) {
+                 $searchQuery = '%' . basename($image->path) . '%';
+                 $inPage = DB::table('pages')
+                         ->where('html', 'like', $searchQuery)->count() > 0;
+                 $inRevision = false;
+                 if ($checkRevisions) {
+                     $inRevision =  DB::table('page_revisions')
+                             ->where('html', 'like', $searchQuery)->count() > 0;
+                 }
+
+                 if (!$inPage && !$inRevision) {
+                     $deletedPaths[] = $image->path;
+                     if (!$dryRun) {
+                         $this->destroy($image);
+                     }
+                 }
+             }
+        });
+        return $deletedPaths;
+    }
+
     /**
      * Convert a image URI to a Base64 encoded string.
      * Attempts to find locally via set storage method first.