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

View File

@ -44,18 +44,26 @@ class RemoveUselessPicturesCommand extends Command
private function removeNonExistentPictures()
{
$base = config('image.path');
$it = new FilesystemIterator($base);
if (empty($base)) {
throw new \Exception("Config image.path must be defined");
}
$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
*/
$linesToDelete = Image::whereNotIn('filename', $files)->get();
$linesToDelete = Image::whereNotIn('filename', $filenames)->get();
$delete = 0;
foreach ($linesToDelete as $lineToDelete) {
$lineToDelete->delete();
@ -80,28 +88,4 @@ class RemoveUselessPicturesCommand extends Command
}
/**
* 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\Relations\MorphToMany;
use Illuminate\Database\Query\Builder;
use Intervention\Image\Exception\NotReadableException;
use Intervention\Image\Image as InterventionImage;
use Meoran\Images\Exception\InvalidContent;
@ -81,10 +80,10 @@ class Image extends Model
static function getAbsolutePath($filename)
{
$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);
$path = $basePath . '/' . implode('/', $parts) . '/' . $filename;
return $path;
@ -126,24 +125,33 @@ class Image extends Model
public function setContentAttribute($content)
{
$this->_content = app('image')->make($content);
$this->setHash();
}
public function getHashAttribute()
{
if (!array_key_exists('hash', $this->attributes)) {
$this->setHash();
if (empty($this->content)) {
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)) {
$this->attributes['hash'] = null;
return;
$mime = $this->content->mime();
if (empty($mime)) {
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)
@ -230,7 +238,4 @@ class Image extends Model
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';
$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
$this->mergeConfigFrom(__DIR__ . '/../../config/image.php', 'images');
$this->mergeConfigFrom(__DIR__ . '/../../config/image.php', 'image');
$this->app->register(ImageServiceProvider::class);
@ -34,8 +34,8 @@ class ImagesServiceProvider extends ServiceProvider
]);
$this->commands([
// CacheGarbageCollectorCommand::class,
// RemoveUselessPicturesCommand::class,
CacheGarbageCollectorCommand::class,
RemoveUselessPicturesCommand::class,
]);
}
}