if remote_url
photo.image_attacher(cache: :remote_url)
photo.image_attacher.assign({url: remote_url, storage: :remote_url, metadata: {}}.to_json)
else
photo.image = file
end
photo.save
uploaded_file # cached file uploaded to shrine-url
uploaded_file.open(headers: { "Authorization" => "...", ... }) # "opens" the file with headers
store = Shrine.new(:store)
store.upload(uploaded_file) # here Storage#open is called internally
Shrine.plugin :module_include
Shrine.attacher_module do
def promote(cached_file, **options)
if cached_file.storage.is_a?(Shrine::Storage::Url)
cached_file.open(headers: { ... }) # open the Shrine::UploadedFile ourselves
end
super
end
end
Shrine.plugin :module_include
Shrine.file_methods do
def open(**options)
if storage.is_a?(Shrine::Storage::Url)
super(headers: {...}, **options)
else
super
end
end
end
photo.image_attacher.assign({ id: remote_url, storage: :remote_url, headers: { ... } }.to_json)
uploaded_file.data["headers"] #=> { ... }
--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/6e09ebcb-32b1-4678-bacc-576dbbb7ac80%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thank you!
Before I got this reply, I took a completely different approach -- a plugin that actually allows the shrine uploader object to recognize multiple different storage keys as "cache", and thus be willing to let them be assigned and promoted.
That actually works fine too. I wonder what the pro's and con's are.
I haven't tackled the "headers" issue yet though.
My code isn't actually committed anywhere yet, but it is definitely working (with tests).
custom plugin: https://gist.github.com/jrochkind/fd112d724a51f01e246aafcd94d7012f
tests: https://gist.github.com/jrochkind/c75b8cf9e5649a270a4576573a1e6df8
My approach was arrived at by basically figuring out what in shrine was _not_ letting me assign an UploadedFile that didn't match the `cache` storage, or _not_ letting such a thing be promoted... and then fixing it so it didn't. Only took a few lines of code, although they're weird ones.
If you felt like it and had time, I'd be curious of your opinion of this approach vs the one you suggested.
PS: One thing I like about where I ended up, is with the plugin I arrived at, you can use the shrine model and attacher just like normal, no need for special assignment code like "photo.image_attacher(cache: :remote_url" in the controller.I like it when I can make it work without changing the API that callers interacting with the model/attacher/uploader have to use.
I think there could conceivably be other cases where people want multiple cache storages for different use cases, all of which get promoted to one store storage (always one store). And it's very nice to have this be something you can configure on uploader/attacher, without having to use different API to assign cache data.
I still think it makes a lot of sense for my use case. But if it doesn't make sense for shrine, fortunately I can pretty easily write my own custom plugin to make it so (even easier after your suggestion for https://github.com/shrinerb/shrine/pull/319)
Alternately, I might consider a feature for shrine-url where you can set a max filesize when defining a shrine-url storage?
What you've done with shrine so far gives reasons to be optimistic, shrine's history is pretty stable and backwards-compat-maintaining.