Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added incremental option to image field and to file field #1026

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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