From 915c82a25b8278a3c2f201c1301966b105625b72 Mon Sep 17 00:00:00 2001 From: Antonio Dal Sie Date: Wed, 18 May 2016 14:47:47 +0200 Subject: [PATCH] Added incremental option to image field and to file field --- src/Frozennode/Administrator/Fields/File.php | 4 ++-- src/Frozennode/Administrator/Fields/Image.php | 2 +- .../Administrator/Includes/Multup.php | 23 ++++++++++++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/Frozennode/Administrator/Fields/File.php b/src/Frozennode/Administrator/Fields/File.php index 3cde16662..38ac32d97 100644 --- a/src/Frozennode/Administrator/Fields/File.php +++ b/src/Frozennode/Administrator/Fields/File.php @@ -25,7 +25,7 @@ class File extends Field { */ protected $rules = array( 'location' => 'required|string|directory', - 'naming' => 'in:keep,random', + 'naming' => 'in:keep,random,incremental', 'length' => 'integer|min:0', 'mimes' => 'string', ); @@ -56,7 +56,7 @@ public function doUpload() //use the multup library to perform the upload $result = Multup::open('file', 'max:' . $this->getOption('size_limit') * 1000 . $mimes, $this->getOption('location'), - $this->getOption('naming') === 'random') + $this->getOption('naming')) ->set_length($this->getOption('length')) ->upload(); diff --git a/src/Frozennode/Administrator/Fields/Image.php b/src/Frozennode/Administrator/Fields/Image.php index 214412463..fd2ea6ab3 100644 --- a/src/Frozennode/Administrator/Fields/Image.php +++ b/src/Frozennode/Administrator/Fields/Image.php @@ -32,7 +32,7 @@ public function doUpload() { //use the multup library to perform the upload $result = Multup::open('file', 'image|max:' . $this->getOption('size_limit') * 1000, $this->getOption('location'), - $this->getOption('naming') === 'random') + $this->getOption('naming')) ->sizes($this->getOption('sizes')) ->set_length($this->getOption('length')) ->upload(); diff --git a/src/Frozennode/Administrator/Includes/Multup.php b/src/Frozennode/Administrator/Includes/Multup.php index 2916a6368..4d147c68c 100644 --- a/src/Frozennode/Administrator/Includes/Multup.php +++ b/src/Frozennode/Administrator/Includes/Multup.php @@ -193,13 +193,15 @@ private function upload_image() $errors = implode('. ', $validation->messages()->all()); } else { - if($this->random){ + if($this->random == "random"){ if(is_callable($this->random_cb)){ $filename = call_user_func( $this->random_cb, $original_name ); } else { $ext = File::extension($original_name); $filename = $this->generate_random_filename().'.'.$ext; } + } else if($this->random == "incremental"){ + $filename = $this->generate_incremental_filename($original_name); } else { $filename = $original_name; } @@ -223,6 +225,25 @@ private function upload_image() return compact('errors', 'path', 'filename', 'original_name', 'resizes' ); } + + /* + * Create incremental filename generation + */ + private function generate_incremental_filename($original_name,$count = "") + { + $name = pathinfo($original_name, PATHINFO_FILENAME); + $ext = File::extension($original_name); + $counter = $count != "" ? '_'.($count-1) : ""; + + if(!File::isFile($this->path.$name.$counter.".".$ext)){ + + return $name.$counter.".".$ext; + }else{ + $count = $count != "" ? $count+1 : 1; + + return $this->generate_incremental_filename($original_name,$count); + } + } /* * Default random filename generation