keystone-storage-adapter-s3 icon indicating copy to clipboard operation
keystone-storage-adapter-s3 copied to clipboard

No option to use original filename

Open mhoffmeyerDC opened this issue 8 years ago • 5 comments

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)?

mhoffmeyerDC avatar Oct 30 '16 13:10 mhoffmeyerDC

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.

mhoffmeyerDC avatar Oct 30 '16 14:10 mhoffmeyerDC

CC: @JedWatson

mhoffmeyerDC avatar Dec 03 '16 01:12 mhoffmeyerDC

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 avatar Apr 14 '17 06:04 robksawyer

@robksawyer thank you - that worked a treat 🎉

n-devr avatar Oct 02 '18 17:10 n-devr

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.

internetErik avatar Dec 11 '18 17:12 internetErik