Modifs cache

This commit is contained in:
Cassandre Cantet 2017-10-11 17:20:42 +02:00
parent ed9f373f4f
commit a1dea266c8
4 changed files with 48 additions and 53 deletions

View File

@ -4,6 +4,7 @@ namespace Meoran\Images\Console\Commands;
use FilesystemIterator; use FilesystemIterator;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use RecursiveDirectoryIterator; use RecursiveDirectoryIterator;
use RecursiveIteratorIterator; use RecursiveIteratorIterator;
@ -39,8 +40,13 @@ class CacheGarbageCollectorCommand extends Command
*/ */
public function handle() public function handle()
{ {
$path = storage_path('image.path'); $base = config('image.cache.path');
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)); if (empty($base)) {
$this->line("Le cache n'est pas configuré. End...");
return;
}
$fs = new Filesystem();
$files = collect($fs->allFiles($base));
/** /**
* @var \SplFileInfo $b * @var \SplFileInfo $b

View File

@ -44,24 +44,32 @@ class RemoveUselessPicturesCommand extends Command
private function removeNonExistentPictures() private function removeNonExistentPictures()
{ {
$base = config('image.path'); $base = config('image.path');
if (empty($base)) {
$it = new FilesystemIterator($base); throw new \Exception("Config image.path must be defined");
}
$fs = new Filesystem(); $fs = new Filesystem();
$files = $fs->allFiles($base); $files = collect($fs->allFiles($base));
$filenames = $files->filter(function ($el) {
$mime = mime_content_type($el->getPathName());
if (strpos($mime, 'image/') === false) {
return false;
}
return true;
})->map(function ($el) {
return $el->getFilename();
});
var_dump($files);
exit;
/** /**
* Suppression des images qui n'existent pas physiquement * Suppression des images qui n'existent pas physiquement
*/ */
$linesToDelete = Image::whereNotIn('filename', $files)->get(); $linesToDelete = Image::whereNotIn('filename', $filenames)->get();
$delete = 0; $delete = 0;
foreach ($linesToDelete as $lineToDelete) { foreach ($linesToDelete as $lineToDelete) {
$lineToDelete->delete(); $lineToDelete->delete();
$delete++; $delete++;
} }
$this->info("Images supprimées en base : ".$delete); $this->info("Images supprimées en base : " . $delete);
/** /**
* Suppression des images qui ne sont pas en base * Suppression des images qui ne sont pas en base
@ -76,32 +84,8 @@ class RemoveUselessPicturesCommand extends Command
$delete++; $delete++;
} }
} }
$this->info("Images supprimées physiquement : ".$delete); $this->info("Images supprimées physiquement : " . $delete);
} }
/**
* Suppression des images qui existent physiquement et en base mais qui ne sont pas utilisées
*/
private function removeNotUsedPictures()
{
$base = config('picturesPath');
$pictures = Image::whereDoesntHave('section', function ($query) {
$query->withTrashed();
})->whereDoesntHave('news', function ($query) {
$query->withTrashed();
})->whereDoesntHave('pages', function ($query) {
$query->withTrashed();
})->get();
$delete = 0;
foreach ($pictures as $picture) {
if (is_file($base . $picture->filename)) {
unlink($base . $picture->filename);
}
$picture->delete();
$delete++;
}
$this->info("Images supprimées car non utilisées : ".$delete);
}
} }

View File

@ -4,7 +4,6 @@ namespace Meoran\Images\Model;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Query\Builder;
use Intervention\Image\Exception\NotReadableException; use Intervention\Image\Exception\NotReadableException;
use Intervention\Image\Image as InterventionImage; use Intervention\Image\Image as InterventionImage;
use Meoran\Images\Exception\InvalidContent; use Meoran\Images\Exception\InvalidContent;
@ -81,10 +80,10 @@ class Image extends Model
static function getAbsolutePath($filename) static function getAbsolutePath($filename)
{ {
$basePath = config('image.path'); $basePath = config('image.path');
if (empty($basePath)) {
throw new \Exception('You must defined config image.path');
}
$parts = array_slice(str_split(mb_strtolower(str_slug($filename, '')), 2), 0, 2); $parts = array_slice(str_split(mb_strtolower(str_slug($filename, '')), 2), 0, 2);
$path = $basePath . '/' . implode('/', $parts) . '/' . $filename; $path = $basePath . '/' . implode('/', $parts) . '/' . $filename;
return $path; return $path;
@ -126,24 +125,33 @@ class Image extends Model
public function setContentAttribute($content) public function setContentAttribute($content)
{ {
$this->_content = app('image')->make($content); $this->_content = app('image')->make($content);
$this->setHash();
} }
public function getHashAttribute() public function getHashAttribute()
{ {
if (!array_key_exists('hash', $this->attributes)) { if (empty($this->content)) {
$this->setHash(); return null;
} }
return $this->attributes['hash']; if (empty($this->content->getEncoded())) {
$this->content->encode();
}
return sha1($this->content->getEncoded());
} }
protected function setHash() public function getExtensionAttribute()
{ {
if (empty($this->content)) { $mime = $this->content->mime();
$this->attributes['hash'] = null; if (empty($mime)) {
return; return null;
} }
$this->attributes['hash'] = sha1($this->content->getEncoded()); return str_replace('image/', '', $mime);
}
public function getMimeAttribute() {
if (empty($this->content)) {
return null;
}
return $this->content->mime();
} }
public function same(Image $image) public function same(Image $image)
@ -230,7 +238,4 @@ class Image extends Model
return $attributes; return $attributes;
} }
public function scopeContent(Builder $builder, Image $image) {
$builder->where('hash', $image->hash);
}
} }

View File

@ -20,7 +20,7 @@ class ImagesServiceProvider extends ServiceProvider
require_once __DIR__ . '/../functions.php'; require_once __DIR__ . '/../functions.php';
$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations'); $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
$this->mergeConfigFrom(__DIR__ . '/../../config/image.php', 'images'); $this->mergeConfigFrom(__DIR__ . '/../../config/image.php', 'image');
$this->app->register(ImageServiceProvider::class); $this->app->register(ImageServiceProvider::class);
@ -34,8 +34,8 @@ class ImagesServiceProvider extends ServiceProvider
]); ]);
$this->commands([ $this->commands([
// CacheGarbageCollectorCommand::class, CacheGarbageCollectorCommand::class,
// RemoveUselessPicturesCommand::class, RemoveUselessPicturesCommand::class,
]); ]);
} }
} }