Lucky Framework: upload a file with Shrine while keeping the original filename

 - Vincent Jousse

On the Lucky’s website, the way to go to upload files is to use The problem is that, by default, is not using the original name of the file to store it, but instead uses a generated id. And of course, for my particuliar use case (serving files directly through Nginx) I need the file to be stored with it’s original filename.

Overriding the generate_location method

It’s pretty simple to achieve (once you know it 😅), the only thing you have to do is to override the generate_location method of the Shrine class.

class FileImport::AssetUploader < Shrine
  def generate_location(io : IO | UploadedFile, metadata, **options)

    if metadata.has_key?("filename")
      super(io, metadata, **options)


Then, you just have to replace the default uploader

result = Shrine.upload(, "store", metadata: { "filename" => pic.filename })

with your custom one:

result = FileImport::AssetUploader.upload(, "store", metadata: { "filename" => pic.filename })

And you’re done! 🎉