diff --git a/src/Model/Image.php b/src/Model/Image.php index d554019..9707706 100755 --- a/src/Model/Image.php +++ b/src/Model/Image.php @@ -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) {