MAJ image

This commit is contained in:
Cassandre Cantet 2017-10-11 13:37:06 +02:00
parent a34031f914
commit dac71b61a3
6 changed files with 233 additions and 5 deletions

View File

@ -11,7 +11,7 @@
], ],
"require": { "require": {
"php": ">=7.0.0", "php": ">=7.0.0",
"laravel/lumen-framework": "5.4.*", "laravel/lumen-framework": "5.*",
"symfony/process" : "3.*", "symfony/process" : "3.*",
"intervention/image": "^2.4", "intervention/image": "^2.4",
"intervention/imagecache": "^2.3", "intervention/imagecache": "^2.3",

View File

@ -0,0 +1,80 @@
<?php
namespace Meoran\Images\Console\Commands;
use FilesystemIterator;
use Illuminate\Console\Command;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
class CacheGarbageCollectorCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cache:garbage';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Cache garbage collector. Remove useless cache';
/**
* CacheGarbageCollectorCommand constructor.
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$path = storage_path('image.path');
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS));
/**
* @var \SplFileInfo $b
*/
$expiredFileCount = 0;
$activeFileCount = 0;
foreach ($files as $file) {
$time = substr(file_get_contents($file->getPathname()), 0, 10);
if (!is_numeric($time)) {
continue;
}
if ($time <= time()) {
unlink($file->getPathname());
$expiredFileCount++;
} else {
$activeFileCount++;
}
}
$this->line('Total expired cache files removed: '.$expiredFileCount);
$this->line('Total active cache files remaining: '.$activeFileCount);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [];
}
}

View File

@ -0,0 +1,107 @@
<?php
namespace Meoran\Images\Console\Commands;
use FilesystemIterator;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Meoran\Images\Model\Image;
class RemoveUselessPicturesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'images:remove';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Remove useless pictures.';
/**
* RemoveUselessPicturesCommand constructor.
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->removeNonExistentPictures();
// $this->removeNotUsedPictures();
}
private function removeNonExistentPictures()
{
$base = config('image.path');
$it = new FilesystemIterator($base);
$fs = new Filesystem();
$files = $fs->allFiles($base);
var_dump($files);
exit;
/**
* Suppression des images qui n'existent pas physiquement
*/
$linesToDelete = Image::whereNotIn('filename', $files)->get();
$delete = 0;
foreach ($linesToDelete as $lineToDelete) {
$lineToDelete->delete();
$delete++;
}
$this->info("Images supprimées en base : ".$delete);
/**
* Suppression des images qui ne sont pas en base
*/
$pictures = Image::select('filename')->getQuery()->get()->transform(function ($item) {
return $item->filename;
})->all();
$delete = 0;
foreach ($files as $file) {
if (!in_array($file, $pictures)) {
unlink($base . $file);
$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

@ -103,7 +103,7 @@ class ImagesController extends BaseController
*/ */
private function getTemplate($template) private function getTemplate($template)
{ {
$template = config("image.templates.{$template}"); $template = config("images.templates.{$template}");
switch (true) { switch (true) {
// closure template found // closure template found
case is_callable($template): case is_callable($template):

View File

@ -4,6 +4,7 @@ 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;
@ -11,12 +12,15 @@ use Spatie\ImageOptimizer\OptimizerChain;
/** /**
* Class Image * Class Image
* @property Image $content * @property InterventionImage $content
* @package App\Model * @package App\Model
*/ */
class Image extends Model class Image extends Model
{ {
/**
* @var InterventionImage $_content
*/
protected $_content; protected $_content;
protected $table = 'images'; protected $table = 'images';
@ -99,7 +103,8 @@ class Image extends Model
public function setFilenameAttribute($value) public function setFilenameAttribute($value)
{ {
$pattern = '/[^a-z_\-\.0-9]/i'; $pattern = '/[^a-z_\-\.0-9]/i';
if (preg_match($pattern, $value)) { $patternLetter = '/[a-z0-9]+/i';
if (preg_match($pattern, $value) || !preg_match($patternLetter, $value)) {
throw new \InvalidArgumentException("Invalid filename. Must be only composed only with a-z, A-Z, 0-9 and dot minus underscore"); throw new \InvalidArgumentException("Invalid filename. Must be only composed only with a-z, A-Z, 0-9 and dot minus underscore");
} }
$this->attributes['filename'] = $value; $this->attributes['filename'] = $value;
@ -121,6 +126,31 @@ 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()
{
if (!array_key_exists('hash', $this->attributes)) {
$this->setHash();
}
return $this->attributes['hash'];
}
protected function setHash()
{
if (empty($this->content)) {
$this->attributes['hash'] = null;
return;
}
$this->attributes['hash'] = sha1($this->content->getEncoded());
}
public function same(Image $image)
{
$hash1 = $this->hash;
$hash2 = $image->hash;
return !empty($hash1) && !empty($hash2) && $hash1 === $hash2;
} }
public function getContentAttribute($value) public function getContentAttribute($value)
@ -199,4 +229,8 @@ class Image extends Model
return $attributes; return $attributes;
} }
public function scopeContent(Builder $builder, Image $image) {
$builder->where('hash', $image->hash);
}
} }

View File

@ -4,6 +4,8 @@ namespace Meoran\Images\Providers;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Intervention\Image\ImageServiceProvider; use Intervention\Image\ImageServiceProvider;
use Meoran\Images\Console\Commands\CacheGarbageCollectorCommand;
use Meoran\Images\Console\Commands\RemoveUselessPicturesCommand;
use Spatie\LaravelImageOptimizer\ImageOptimizerServiceProvider; use Spatie\LaravelImageOptimizer\ImageOptimizerServiceProvider;
class ImagesServiceProvider extends ServiceProvider class ImagesServiceProvider extends ServiceProvider
@ -14,7 +16,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', 'image'); $this->mergeConfigFrom(__DIR__ . '/../../config/image.php', 'images');
$this->app->register(ImageServiceProvider::class); $this->app->register(ImageServiceProvider::class);
@ -26,6 +28,11 @@ class ImagesServiceProvider extends ServiceProvider
$this->publishes([ $this->publishes([
// __DIR__ . '/../../config/synchronize.php' => base_path('config/synchronize.php') // __DIR__ . '/../../config/synchronize.php' => base_path('config/synchronize.php')
]); ]);
$this->commands([
// CacheGarbageCollectorCommand::class,
// RemoveUselessPicturesCommand::class,
]);
} }
} }