keystone-storage-adapter-s3
keystone-storage-adapter-s3 copied to clipboard
No option to use original filename
It looks like the generateFilename
function is set to nameFunctions.randomFilename
- meaning any upload using this storage adapter will always have a random name.
Suggest adding an option to allow the original name to be used instead.
Concept:
if(options.s3.originalname) {
this.options.generateFilename = function(file, attempt, callback) {
return callback(null, file.originalname);
}
} else {
this.options.generateFilename = ensureCallback(this.options.generateFilename);
}
Or should this be handled somewhere higher up in the keystone Types? Or be changed to support a filename function in the Field definition (e.g., filename: (file) => file.originalName
)?
Looking at the forks it seems like @GwonHyeok already found a fix for the issue.
Looks like a better, more robust solution rather than more options.
CC: @JedWatson
For anyone that came here looking for an example of how to use the original file name with this plugin, check out https://gist.github.com/robksawyer/6060412c3f86ddbc68b29ed09eef75c3.
@robksawyer thank you - that worked a treat 🎉
Just wanted to note that I was having difficulty in s3 with files that have spaces in them, so when I want to keep the original file name I use this:
generateFilename: file => file.originalname.replace(/ /g, '-'),
I'll also mention this for anyone who wants to generate a public url that is saved. I usually use a method that looks like this:
publicUrl : file => `https:${process.env.S3_BASE_URL}${s3FilePath}${file.filename}`,
The S3_BASE_URL is saved in the .env
file. Instead you may have a cloudfront path or whatever else you want.