uploadcare / uploadcare-rails

Rails API client that handles uploads and further operations with files by wrapping Uploadcare Upload and REST APIs.

Home Page:https://uploadcare.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow UPLOADCARE_CDN_BASE setting.

boxmo opened this issue · comments

There's a way that i can load images on https protocol?

yup, just replace whatever CDN prefix you have with https://ucarecdn.com
Widget has a setting for it: https://uploadcare.com/documentation/widget/#option-cdn-base

@dmitry-mukhin why this isn't an option on uploadcare.yml file?

gem is bit outdated.
you can send a PR that will add this setting to a widget renderer.

👍 really needed

Is this still a planned enhancement or is there a known workaround?

@Oshuma you can always add this setting (along with any other) to your HTML or templates manually without gem's help.

Does it make sense?

@dmitry-mukhin I can add the setting manually to templates, and it does use the CDN URL when saving to the model attribute, however any subsequent methods that use the model's has_uploadcare_file field doesn't use the CDN URL; i.e. #to_s and #url still use the base CDN URL. For example:

// Widget setting:
UPLOADCARE_CDN_BASE = "https://ucarecdn.example.com";
# Image model:
#   has_uploadcare_file :uploadcare

image = Image.last
# In the database, the field is saved using the correct CDN URL:
# uploadcare: "https://ucarecdn.example.com/e379b8b7-178a-4fb5-8729-e1bfec0b1a7e/"

# However, using #to_s or #url returns the standard UC CDN:
image.uploadcare.url
# => "https://ucarecdn.com/e379b8b7-178a-4fb5-8729-e1bfec0b1a7e/"
image.uploadcare.to_s
# => "https://ucarecdn.com/e379b8b7-178a-4fb5-8729-e1bfec0b1a7e/"
image.uploadcare.url(inline: "/scale_crop/80x80/center")
# => "https://ucarecdn.com/e379b8b7-178a-4fb5-8729-e1bfec0b1a7e/-/scale_crop/80x80/center/"

So a workaround that I think is working is setting static_url_base in the uploadcare.yml config. When that set, the methods are now using the correct CDN domain, even for images which have the base UploadCare CDN domain stored in the column.

# ./config/uploadcare.yml
cdn_base: "https://ucarecdn.example.com"
static_url_base: "https://ucarecdn.example.com"
[1] pry(main)> image = Image.first
=> # uploadcare: "https://ucarecdn.com/170f0b2d-4e1a-4997-8e22-4f4600cd9ca1/"
[2] pry(main)> image.uploadcare.to_s
=> "https://ucarecdn.example.com/170f0b2d-4e1a-4997-8e22-4f4600cd9ca1/"
[3] pry(main)> image.uploadcare.url(inline: "/scale_crop/80x80/center")
=> "https://ucarecdn.example.com/170f0b2d-4e1a-4997-8e22-4f4600cd9ca1/-/scale_crop/80x80/center/"

@dmitry-mukhin Are you aware of any side effects of that ^ workaround (setting the static_url_base as our CDN domain). I found that while looking through the library's code, but don't see it documented anywhere.

@Oshuma I'd say that you're safe to use that hack.
@vizvamitra can you please confirm this?

@dmitry-mukhin I've made that change in our production app and have it deployed. It seemed to do the trick. All images are successfully getting uploaded and served through our CDN.

👍
pull requests are most welcome

I actually didn't have to change any code in the library, only added that static_url_base value in the config file. The code already uses that value when building the URL.

https://github.com/uploadcare/uploadcare-ruby/blob/51fab41f46619720a1ceb590a6953643f9bc0395/lib/uploadcare/resources/file.rb#L28-L30

@boxmo, Hi, we released a new v2 version of the gem. You can set this param in the initializer in v2 (uploadcare.rb). Please, see https://github.com/uploadcare/uploadcare-rails#readme in the main branch for more info.