uploadcare / uploadcare-ruby

Ruby API client that handles uploads and further operations with files by wrapping Uploadcare Upload and REST APIs.
https://uploadcare.com
MIT License
39 stars 28 forks source link

MIME type of uploaded files is always "application/octet-stream" #104

Closed wreckah closed 1 year ago

wreckah commented 1 year ago

Describe the bug

Ruby client always uploads files with mime_type=application/octet-stream:

ENV["UPLOADCARE_PUBLIC_KEY"] = "pub_key"
ENV["UPLOADCARE_SECRET_KEY"] = "secret"
require "uploadcare"

@file_to_upload = File.open("your-file.png")
@uc_file = Uploadcare::Uploader.upload(@file_to_upload)
@uc_file.mime_type

# => "application/octet-stream"

Expected behavior

Other UC clients provide an original mime_type of uploaded file, e.g.:

$ curl -F "UPLOADCARE_PUB_KEY=pub_key" -F "file=@cat.jpg" "https://upload.uploadcare.com/base/"

# {"file":"43f09fdb-28b1-4785-b1d1-3c5fd6217549"}

$ curl -H "Authorization: Uploadcare.Simple pub_key:secret" "https://api.uploadcare.com/files/43f09fdb-28b1-4785-b1d1-3c5fd6217549/" | json_pp | grep mime

#   "mime_type" : "image/jpeg",
from pyuploadcare import File, Uploadcare

uploadcare = Uploadcare(public_key='pub_key', secret_key='secret')
with open('cat.jpg', 'rb') as file_object:
     uc_file: File = uploadcare.upload(file_object)
     print(uc_file.mime_type)

# image/jpeg

Code / screenshots

Environment

wreckah commented 1 year ago

I found this PR in Python client's repo, but I am not sure, that it's completely correct, because now Python client tries to detect MIME type by file extension. But according to cURL example, I suppose, that MIME type is automatically detected by using direct upload API resource. And is not detected automatically by using multipart upload API resource.

kraft001 commented 1 year ago

@wreckah I tested the scenario on the latest gem version: It works well for me

def upload_and_print(filename)
  @file_to_upload = File.open(filename)
  @uc_file = Uploadcare::Uploader.upload(@file_to_upload)
  puts @uc_file.mime_type
end

upload_and_print('test.png') # => image/png
upload_and_print('kitten.jpeg') # => image/jpeg

Library version: 4.3.0 Language/framework version: 2.7.0p0 OS version: Ubuntu 20.04.5 LTS