This commit is contained in:
Cassandre Cantet 2017-10-04 14:41:57 +02:00
commit 670ee0e516
1 changed files with 21 additions and 1 deletions

View File

@ -22,6 +22,7 @@ class Image extends Model
protected $table = 'images';
public $fillable = [
'filename',
'content',
'position',
'created',
@ -73,7 +74,8 @@ class Image extends Model
{
$basePath = config('image.path');
$parts = array_slice(str_split($filename, 2), 0, 2);
$parts = array_slice(str_split(mb_strtolower(str_slug($filename, '')), 2), 0, 2);
$path = $basePath . '/' . implode('/', $parts) . '/' . $filename;
@ -85,6 +87,24 @@ class Image extends Model
return mb_strtolower(str_random(60));
}
static function sanitizeFilename($filename)
{
return str_slug($filename, '-');
}
public function setFilenameAttribute($value)
{
$pattern = '/[^a-z_\-\.0-9]/i';
if (preg_match($pattern, $value)) {
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;
}
public function fileExist() {
return is_file($this->getPath());
}
public function generateFilename($force = false)
{
if ($this->filename && !$force) {