shrinerb / shrine

File Attachment toolkit for Ruby applications

Home Page:https://shrinerb.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using :url_options alongside :default_storage and :dynamic_storage does not work as expected

camiloforero opened this issue · comments

Hi

Checking out the :url_options plugin, I noticed that if I declare something very straightforward like plugin :url_options, store: { expires_in: 60 * 60 * 24 }, this value gets stored at shrine_class.opts[:url_options][:store], so this line will later retrieve and merge these url options whenever the current storage_key is :store

However, let's assume I am also using the :dynamic_storage and :default_storage plugins, like this:

plugin :default_storage, store: -> {
  :"#{record.subdomain}_store"
}

plugin :dynamic_storage
storage /(\w+)_store/ do |match|
Shrine::Storage::S3.new(
    prefix: match[1],
    # Storage config
  )
end

In this case, the :url_options plugin no longer works properly, because the value of storage_key inside the shrine file context is not :store, but whichever value was dynamically generated (something like :mysubdomain_store.

As an idea for a solution, I tried to find a way to check whether the current storage_key equals the Attacher's :store_key or :cache_key, but I couldn't find a good way to reach the Attacher from inside the UploadedFile instance. Maybe you have a better idea