cloudinary_gem
cloudinary_gem copied to clipboard
Cant direct upload raw files
When implementing a direct upload in a rails form specifiying direct_upload: true, the api returns a 400 response due to the lack of format in the url search params.
This issue only happens in development and not all the times
Issue Type (Can be multiple)
- [ ] Build - Cannot install or import the SDK
- [ ] Performance - Performance issues
- [x] Behaviour - Functions are not working as expected (such as generate URL)
- [ ] Documentation - Inconsistency between the docs and behaviour
- [ ] Other (Specify)
Steps to reproduce
In a rails form add
<%= f.file_field :data_file, direct_upload: true, accept: 'text/csv' %>
Alert error appears
Error screenshots or Stack Trace (if applicable)
formed url on a successful attempt
https://api.cloudinary.com/v1_1/cloud_name/raw/upload?api_key=key&context=active_storage_key%3DUeaRoJ5eyEs6a3E6cr1UFjEn&format=csv&public_id=id&signature=signature×tamp=1640264587
Format url on a failed attempt
https://api.cloudinary.com/v1_1/cloud_name/image/upload?api_key=444276538933115&context=active_storage_key%3DhBSN6VN1PdqAq4TPtbPS4L69&public_id=id&signature=signature×tamp=1640265086
As you can see in the server logs image im not getting the right url

As you can see is uploading the csv as an image
Operating System
- [ ] Linux
- [ ] Windows
- [x] macOS
- [ ] All
Environment and Libraries (fill in the version numbers)
- Cloudinary Ruby SDK version - 1.21.0
- Ruby Version - 2.7.5
- Rails Version - 5.2.2
- Other Libraries (Carrierwave, ActiveStorage, etc) - ActiveStorage
Repository
If possible, please provide a link to a reproducible repository that showcases the problem
Hi Miguel,
Thanks for contacting us.
I hate to ask a potentially silly question, but are you certainly using v1.21.0 of the SDK in both your production and development environments?
I ask because v1.19.0 of the Ruby SDK included a bugfix to resolve an issue that's very similar to what you've described here, and earlier versions can be expected to have this error in the scenario you've described.
The code that was added and an accompanying note can be seen here: https://github.com/cloudinary/cloudinary_gem/blob/b6f084b3a0f7697aa0d4c96f2201e069ee12d66d/lib/active_storage/service/cloudinary_service.rb#L75-L85
If you do have v1.21.0 (or some other version above 1.19.0), were you able to identify any common factor for the requests that fail? Is the upload filename, including the extension, certainly available to your frontend code, for it to be passed to our SDK and from there to our API?
Thanks, Stephen
Hello @igy thanks for your answer,
I tried to even lock the version in the gemfile gem 'cloudinary', '~> 1.21.0'
but it didn't work It does not detect.
Ive unpacked the gem and debugged using byebug, i found the following results:
Here content_type is '' as io is nil and the key is just a string, this method is called here
thus when calling content_type_to_resource_type
with content_type as empty string it returns an image
Ive done a couple of modifications to sort this issue:
Ive modified the resource_type method to accept a third argument
Also moved the guard clause in the ext_for_file method
It works for me but I dont know if I break any other uses of this gem
If you open rails console in that project, what version do you get?
It's 5.2.2
My apologies, I meant to say, can you do this:
rails c
require 'cloudinary'
Cloudinary::VERSION
I just want to make sure the cloudinary version is right.
Ive been reviewing the code of active storage to check differences between 5.2 and 7.0
5.2 activestorage/app/controllers
Same folder in 7.0
Differences are massive, it is probably the cause of the issue.
I'm checking internally and will keep you posted.
I've opened an internal ticket for this issue and will keep you posted.
Thank you @momoip
@Mrodrides Of course. Will for sure let you know when the issue is fixed and deployed.
@Mrodrides apologies for the delay, so we tested with csv files and cannot replicate on Rails 7.0.4 We got valid upload URLs and everything works as expected. We always saw the file content type detected right.
Are you still having this issue? If so, is it with specific CSV files and can you send us this file? Does upgrading to rails 7.0.4 help as well?
Previously we had to vendorize the gem and modify it there but now we have upgraded raise and teared down all the internal hacks, so is back to normal. Everything works correctly with the images and pdfs, but the raw files are still problematic, and worst of all the bugs are not consitent.
When for example i upload a csv, docx or a txt file, sometimes the url has the extension and sometimes it does not. So when for example it is displayed in the app through the 'attachment.url(secure: true)' it leads to a 404, removing or adding .csv, .txt or .docx on the file solves the issue.
I have to remark here that we only use direct uploads.
On the other side, i notice that when in the console i do

As you can see, one has the format and the other not
@Mrodrides thanks for getting back. How often are you seeing extensions not being included for raw files? Do you see any pattern in them? And is it happening on random raw files?
@Mrodrides cloudinary_url()
is a generic SDK function for building URLs.
For Active Storage integration please only use .url
function. The difference between them is that it uses content type to identify file extension when needed (for raw files in this case).
Can you please provide an example of a csv/docx/txt file that leads to 404? so we can investigate it further?
Thank you!
We were experiencing this same problem. We were on v1.23.0
and the fix included in v.1.24.0
found here resolved it.