SUKOHI / Surpass

A PHP package mainly developed for Laravel to manage uploading images using Ajax and displaying thumbnail.
29 stars 8 forks source link

Adding the possibility to maintain the original filename #10

Open marco-mazzocchi opened 8 years ago

marco-mazzocchi commented 8 years ago

Hi, I locally add a new feature to give the possibility to manage the file renaming option, may I send you the pull request? It seems that I "may not have permission to access Surpass" did you need to add me to the collaborators or something else?

Thanks

SUKOHI commented 8 years ago

Hi, marco-mazzocchi. Thank you for your message. :)

That's a nice feature. I'd like you to post your code here like the below because I'm not familiar with pull request yet. https://github.com/SUKOHI/Surpass/issues/9

I'll add your code into this package and push it after check! :)))

marco-mazzocchi commented 8 years ago

Hi! Ok so see the following instruction and tell me if it's ok:

I worked only on Surpass.php file.

after

private $_preview_params = ['maxHeight' => 120];

add

private $_rename_files = true;

after "dropZone" function add

public function renameFiles($bool = true) {

        $this->_rename_files = $bool;
        return $this;

    }

in

return view('surpass::js', [
   ...
])->render();

add

'rename_files' => $this->_rename_files,

in "save" function after

$input_id = $this->_ids['input'];

add

$save_path = $this->filePath($this->_dir);
$filename = $this->filename(Input::file($input_id), $save_path);

in the same function after

try {

remove

$save_path = $this->filePath($this->_dir);

change the function "filename" like this

private function filename($file, $save_path) {

        $extension = $file->getClientOriginalExtension();
        $filename = $file->getClientOriginalName();

        if($this->_rename_files) {
            return str_random($this->_filename_length) .'.'. $extension;
        }
        else {
            // check if filename exist
            if(!File::exists($save_path . "/" . $filename)) {
                return $filename;
            }
            // else add a incremental number until file don't exist
            else {
                $i = 1;
                $new_filename = str_replace(".".$extension, "", $filename) . "-" . $i . "." . $extension;
                $file_path = $save_path . "/" . $new_filename;

                while(File::exists($file_path)) {
                    $i++;
                    $new_filename = str_replace(".".$extension, "", $filename) . "-" . $i . "." . $extension;
                    $file_path = $save_path . "/" . $new_filename;
                }
                return $new_filename;
            }
        }

    }

that's all

SUKOHI commented 8 years ago

Hi, marco-mazzocchi. Thank you for your code.

I've just added the code in Surpass. And you can see your name in README.md as Special Thanks. (If you mind it, please let me know. I'll remove it.)

Thank you!

marco-mazzocchi commented 8 years ago

Thanks to you! Happy to be useful :)