shrinerb / shrine

File Attachment toolkit for Ruby applications

Home Page:https://shrinerb.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Specifying host: with plugin :url_options has no effect on the returned URL

dan opened this issue · comments

Adding this plugin with a default host specified as documented has no effect on the generated URLs when using Amazon S3 or Cloudflare R2:

class ImageUploader < Shrine
  plugin :url_options, store: { host: "http://abc123.example.com" }
  # ...
end

irb(main):007:0> photo.image_url
=> "https://bucket-name.f8w9jf489ru.r2.cloudflarestorage.com/123/99/antic-hay-12346-djanj.jpg"

I would expect it to return:

"https://abc123.example.com/123/99/antic-hay-12346-djanj.jpg"

In order to get that response, I need to do:

irb(main):007:0> photo.image_url(host: "https://abc123.example.com")
=> "https://abc123.example.com/123/99/antic-hay-12346-djanj.jpg"

I was under the impression that specifying the plugin :url_options as documented would make this extra step unnecessary.

Is this a bug or am I misunderstanding the purpose of the host: option?

I couldn't reproduce your issue, the self-contained script below applies the default URL host:

require "shrine"
require "shrine/storage/s3"

Shrine.storages = {
  cache: Shrine::Storage::S3.new(bucket: "cache", stub_responses: true),
  store: Shrine::Storage::S3.new(bucket: "store", stub_responses: true),
}

Shrine.plugin :activerecord

class ImageUploader < Shrine
  plugin :url_options, store: { host: "https://abc123.example.com" }
end

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.connection.create_table(:photos) { |t| t.text :image_data }

class Photo < ActiveRecord::Base
  include ImageUploader::Attachment(:image)
end

post = Photo.create(image: StringIO.new("foobar"))
post.image_url #=> "https://abc123.example.com/..."

It's the default_storage plugin.

plugin :default_storage, store: :images

With this enabled, the url_options plugin setting is ignored. Is there another/proper way for me to specify this?

Well, this seems to work:

plugin :url_options, images: { host: "https://abc123.example.com" }

Yes, the key specifies the storage name the URL options will be applied to. I see now that this is not clearly documented, I'll push a fix for this.

Thanks so much for helping me understand my mistake, I appreciate it!