mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-02-21 06:41:39 +08:00
Added command to clean-up old images, Unfinished
This commit is contained in:
parent
61c9324229
commit
c31e6a03ce
68
app/Console/Commands/CleanupImages.php
Normal file
68
app/Console/Commands/CleanupImages.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Console\Commands;
|
||||
|
||||
use BookStack\Services\ImageService;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class CleanupImages extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'bookstack:cleanup-images
|
||||
{--a|all : Include images that are used in page revisions}
|
||||
{--f|force : Actually run the deletions}
|
||||
';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Cleanup images and drawings';
|
||||
|
||||
|
||||
protected $imageService;
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
* @param ImageService $imageService
|
||||
*/
|
||||
public function __construct(ImageService $imageService)
|
||||
{
|
||||
$this->imageService = $imageService;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$checkRevisions = $this->option('all') ? false : true;
|
||||
$dryRun = $this->option('force') ? false : true;
|
||||
|
||||
if (!$dryRun) {
|
||||
$proceed = $this->confirm('This operation is destructive and is not guaranteed to be fully accurate. Ensure you have a backup of your images. Are you sure you want to proceed?');
|
||||
if (!$proceed) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$deleteCount = $this->imageService->deleteUnusedImages($checkRevisions, ['gallery', 'drawio'], $dryRun);
|
||||
|
||||
if ($dryRun) {
|
||||
$this->comment('Dry run, No images have been deleted');
|
||||
$this->comment($deleteCount . ' images found that would have been deleted');
|
||||
$this->comment('Run with -f or --force to perform deletions');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->comment($deleteCount . ' images deleted');
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
namespace BookStack\Providers;
|
||||
|
||||
use BookStack\Activity;
|
||||
use BookStack\Image;
|
||||
use BookStack\Services\ImageService;
|
||||
use BookStack\Services\PermissionService;
|
||||
use BookStack\Services\ViewService;
|
||||
@ -57,6 +58,7 @@ class CustomFacadeProvider extends ServiceProvider
|
||||
|
||||
$this->app->bind('images', function () {
|
||||
return new ImageService(
|
||||
$this->app->make(Image::class),
|
||||
$this->app->make(ImageManager::class),
|
||||
$this->app->make(Factory::class),
|
||||
$this->app->make(Repository::class)
|
||||
|
@ -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,43 @@ class ImageService extends UploadService
|
||||
return $image;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete gallery and drawings that are not within HTML content of pages or page revisions.
|
||||
* @param bool $checkRevisions
|
||||
* @param array $types
|
||||
* @param bool $dryRun
|
||||
* @return int
|
||||
*/
|
||||
public function deleteUnusedImages($checkRevisions = true, $types = ['gallery', 'drawio'], $dryRun = true)
|
||||
{
|
||||
// TODO - The checking here isn't really good enough.
|
||||
// Thumbnails would also need to be searched for as we can't guarantee the full image will be in the content.
|
||||
// Would also be best to simplify the string to not include the base host?
|
||||
$types = array_intersect($types, ['gallery', 'drawio']);
|
||||
$deleteCount = 0;
|
||||
$this->image->newQuery()->whereIn('type', $types)
|
||||
->chunk(1000, function($images) use ($types, $checkRevisions, &$deleteCount, $dryRun) {
|
||||
foreach ($images as $image) {
|
||||
$inPage = DB::table('pages')
|
||||
->where('html', 'like', '%' . $image->url . '%')->count() > 0;
|
||||
$inRevision = false;
|
||||
if ($checkRevisions) {
|
||||
$inRevision = DB::table('page_revisions')
|
||||
->where('html', 'like', '%' . $image->url . '%')->count() > 0;
|
||||
}
|
||||
|
||||
if (!$inPage && !$inRevision) {
|
||||
$deleteCount++;
|
||||
if (!$dryRun) {
|
||||
$this->destroy($image);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return $deleteCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a image URI to a Base64 encoded string.
|
||||
* Attempts to find locally via set storage method first.
|
||||
|
Loading…
x
Reference in New Issue
Block a user