anacronw / multer-s3

multer storage engine for amazon s3
MIT License
660 stars 191 forks source link

Added additional file naming options #4

Closed jasonleibowitz closed 9 years ago

jasonleibowitz commented 9 years ago

Hey, I used this module in a recent project and found I needed to modify it because it didn't provide many options when it came to custom filenames. I added a few additional options that I think some people may find helpful. I also updated the readme as well.

The three additional options that can be used when calling the multer-s3 module in app.js are originalname, filename, and crypto. I explain each in more detail in the readme, but the basic idea is that if you want to upload the file with the same name as the original file, add originalname: true. If you want to have one filename that is constantly overwritten, like a temporary file, then use filename with a string. Otherwise, the original crypto name you made is the default.

anacronw commented 9 years ago

Hi - looks good

Can you add tests and remove the console.logs?

LinusU commented 9 years ago

Actually, how would you both feel about modelling it the same as the builtin DiskStorage? Let the filename be a function that receives (req, file, cb) and that calls the callback with the appropriate name.

That way I think that we provide the most flexibility. On top of this, the possibilities that you mentioned are easily implemented:

function getFilename (req, file, cb) {
  cb(null, file.originalname)
}
function getFilename (req, file, cb) {
  cb(null, 'my-static-name')
}
function getFilename (req, file, cb) {
  crypto.pseudoRandomBytes(16, function (err, raw) {
    cb(err, err ? undefined : raw.toString('hex'))
  })
}
jacobtomlinson commented 9 years ago

I agree with @LinusU. This should be consistent with the DiskStorage method. It would be great to get this in as this is currently stopping me form using this library.

anacronw commented 9 years ago

Closing for PR #6 to match DiskStorage functionality