add custom filename option #93

Closed
wants to merge 9 commits into
from

5 participants

@intuxicated

hi and thanks for great plugin.
i explained about how to use custom filename option in readme file. my English is not good so maybe i missed something in readme.

i added 2 function to UploadBehavior and modified afterSave to add this feature.
there is several ways to implement custom filename, i decide to use Pattern. its very simple and easy to use.

for example this pattern :

{#NAME}_{id}

will return something like this : orginal_name_1.
you can create function in your model and call it to generate custom filename:

{!myMethod}-suffix
class Model extends AppModel {
    public function myMethod($filename = ''){
        return md5($filename);
    }
}

in above example suffix is just a simple text. and $filename is optional.it will be passed to each method you call in Pattern.

i used http://stackoverflow.com/a/13767184/440754 sanitizeFilename function to remove unsupported character from filename.

@intuxicated

filename changed currectly , but in database , i have the orginal name instead custom filename, so , what you prefer ? update again ? or should i move actions from afterSave to beforeSave ?

@vxtrim

You have mistake in document. In your example it's not working.
Shoul be:
'photo' => array(
'fields' => array(
'dir' => 'photo_dir',

),
'customName' => '{!getName}'
)

@intuxicated

@vxtrim where exactly ?

@awtprod

Thanks for taking the time to solve this problem!

I have an issue with the code though, I wanted to use the md5 hash for a file name (the last option). However, my filenames are all renamed to "1." Also, the new filename is not saved to my database.

https://gist.github.com/awtprod/4751891

Any ideas? Thanks!!

@intuxicated

@awtprod hi, in last commit i fixed the database problem.

Sorry i didn't see the gist :)

now i find out you did not apply the last commit :)

@awtprod

That worked! Thanks!

However, I can't figure out why the filename is always changed to "1." Is it because it's not reading the filename in my model?

@intuxicated

if filename changed in database correctly so you must have the exact filename.
in line 288 check $filePath value, and make sure you are in right path.

@awtprod

I'm not quite sure what you mean by that....I haven't changed that line. $path is set to the default path.

@wink-

Great improvement intuxicated. Any idea on how I could set the file name to be the same as the id of the entry it is going to be stored in the table?

@intuxicated

@wink- sorry that's not possible now , but you can use afterSave method

jlis and others added some commits Mar 1, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment