cloudinary_gem
cloudinary_gem copied to clipboard
ActiveStorage: Using upload_preset with folder creates invalid image links
Describe the bug in a sentence or two.
I use different upload presets in my app which default to a different folder each (e.g. "production"). storage.yml:
cloudinary:
service: Cloudinary
upload_preset: <%= ENV['CLOUDINARY_UPLOAD_PRESET'] %>
What the SDK cannot know is that the upload preset is configured to upload files into a specified folder. Therefore, when it generates links to the uploaded images, they miss the folder part and result in dead links.
Issue Type
[ ] Build - Can’t install or import the SDK [ ] Performance - Performance issues [X] Behaviour - Functions aren’t working as expected (Such as generate URL) [ ] Documentation - Inconsistency between the docs and behaviour [ ] Other (Specify)
Steps to reproduce
- Configure an upload preset using an upload folder (e.g. "test") in the Cloudinary administration interface.
- Configure Rails' storage.yml to use
upload_preset: testwith the configured cloudinary service (see above). - Upload a file attachment via Rails and ActiveStorage, then use url_for() to generate a link to that image.
- Observe: the link misses the "test"-folder part.
Operating System
[ ] Linux [ ] Windows [ ] OSX [X] All
Environment and Libraries
Cloudinary Ruby SDK version - 1.13.2 Ruby Version - 2.6.6 Rails Version - 5.2.4.1 ActiveStorage - 5.2.4.1
Suggestion
We could use the Cloudinary API to fetch the configured upload folder for the specified upload preset. I'm not sure that's a good idea - we might end up calling the API every time someone uses an upload_prefix. We may also introduce another storage.yml option "preset_folder" to be used together with upload_preset only.
I need this fixed quite urgently so I'm absolutely willing to submit a PR. Any hints from the maintainers would be totally helpful!
@gr8bit , storage.yml already supports folder option (it can be used without upload preset). It is used for both upload and url generation. In case you use upload presets, the folder option is ignored during upload, but still can be used for url generation. If you set it to exact same folder that is configured in the preset, it will work. If you don't have the folder name in your app, you can use admin API to fetch it and set programmatically (preferably once, on initialisation).
@const-cloudinary 😮 Seriously? I must have missed that the whole time! Will try right now.
It works. Thanks a lot!
I had to turn off enhance_image_tag though, else all sprockets-generated URLs would break (e.g. image_url('/test/test.png') would become /images/development/http://localhost:3000/test..., but that's another story. Saved me a lot of work. <3 Where in the documentation did I miss this feature?