Skip to content

Commit

Permalink
Added incremental option to image field and to file field
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Dal Sie authored and Antonio Dal Sie committed May 18, 2016
1 parent b5b59c4 commit 915c82a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Frozennode/Administrator/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/Frozennode/Administrator/Fields/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
23 changes: 22 additions & 1 deletion src/Frozennode/Administrator/Includes/Multup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down

0 comments on commit 915c82a

Please sign in to comment.