Original file doesn't have extension, but derivatives do

Hello,

I noticed a weird behaviour when using derivatives, and I’m wondering if this is a bug or if it’s by design: the original file is uploaded (I’m using Google Cloud Storage) without any extension (e.g. the filename is 104dc6eb540fe93fae7625f0c415db72), but all derivatives have an extension (e.g.
114a682aafd54b36284b0b0a251ea0a5.jpg).

Is this expected? And if so, how could I change this behaviour to a more consistent one, ie, I’d like to either: 1) save all files with extension or 2) save all files without extension.

I’m using Shrine 3.0.0.beta3.

Thank you!

For saving original file with extension, you have two options:

  1. Load the infer_extension plugin which will infer file extension from MIME type. This will work if you also have the determine_mime_type plugin loaded.
Shrine.plugin :infer_extension
  1. Have the original file be a File/Tempfile object with an extension at the time of the upload.
photo.image = File.open("image")     # uploaded file will not have an extension
photo.image = File.open("image.jpg") # uploaded file will have an extension

To make all files without an extension, override #generate_location to remove the extension:

require "pathname"

class MyUploader < Shrine
  def generate_location(io, **options)
    location = Pathname(super)
    location = location.sub_ext("")
    location.to_s
  end
end