cloudinary_gem icon indicating copy to clipboard operation
cloudinary_gem copied to clipboard

Cant direct upload raw files

Open Mrodrides opened this issue 3 years ago • 17 comments

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)

error message

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&timestamp=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&timestamp=1640265086

As you can see in the server logs image im not getting the right url

Captura de pantalla 2021-12-23 a las 14 29 30

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

Mrodrides avatar Dec 23 '21 13:12 Mrodrides

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

igy avatar Dec 23 '21 14:12 igy

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: image Here content_type is '' as io is nil and the key is just a string, this method is called here

image

thus when calling content_type_to_resource_type with content_type as empty string it returns an image

image

Mrodrides avatar Jan 03 '22 12:01 Mrodrides

Ive done a couple of modifications to sort this issue:

image Ive modified the resource_type method to accept a third argument image Also moved the guard clause in the ext_for_file method image It works for me but I dont know if I break any other uses of this gem

Mrodrides avatar Jan 03 '22 13:01 Mrodrides

If you open rails console in that project, what version do you get?

d-mendoza avatar Jan 05 '22 00:01 d-mendoza

It's 5.2.2

Mrodrides avatar Jan 05 '22 08:01 Mrodrides

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.

d-mendoza avatar Jan 05 '22 17:01 d-mendoza

image

Mrodrides avatar Jan 10 '22 09:01 Mrodrides

Ive been reviewing the code of active storage to check differences between 5.2 and 7.0

5.2 activestorage/app/controllers image

Same folder in 7.0 image

Differences are massive, it is probably the cause of the issue.

Mrodrides avatar Jan 10 '22 09:01 Mrodrides

I'm checking internally and will keep you posted.

momoip avatar Jan 14 '22 21:01 momoip

I've opened an internal ticket for this issue and will keep you posted.

momoip avatar Jan 17 '22 17:01 momoip

Thank you @momoip

Mrodrides avatar Jan 18 '22 19:01 Mrodrides

@Mrodrides Of course. Will for sure let you know when the issue is fixed and deployed.

momoip avatar Jan 18 '22 20:01 momoip

@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?

tommyg-cld avatar Dec 06 '22 10:12 tommyg-cld

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

image

As you can see, one has the format and the other not

Mrodrides avatar Dec 07 '22 10:12 Mrodrides

@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?

tommyg-cld avatar Dec 07 '22 14:12 tommyg-cld

@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!

const-cloudinary avatar Dec 07 '22 23:12 const-cloudinary

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.

austenmadden avatar Jun 26 '23 20:06 austenmadden