Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct the solution to get the content type #238

Open
yopaz-giapnh opened this issue Sep 18, 2021 · 1 comment
Open

Correct the solution to get the content type #238

yopaz-giapnh opened this issue Sep 18, 2021 · 1 comment

Comments

@yopaz-giapnh
Copy link

case file.path
when /\.jpe?g\z/i then 'image/jpeg'
when /\.png\z/i then 'image/png'
else
raise ArgumentError, "invalid file extension: #{file.path}"
end

If using the file with no extension, this caused an error. For eg: "/var/folders/m3/p364c0313fv0mhbl3llm4bvw0000gp/T/ActiveStorage-27-20210918-49325-1bu8npn"

@kimoto
Copy link
Contributor

kimoto commented Sep 21, 2021

You're right, It's too difficult to use for content_type detection.
But you can use the following method to get the content_type of the file instead of the file extension.

irb> file = open('https://www.ruby-lang.org/images/header-ruby-logo.png')
irb> file.content-type
=> "image/png"

if file.respond_to?(:content_type)
content_type = file.content_type
raise ArgumentError, "invalid content type: #{content_type}" unless ['image/jpeg', 'image/png'].include?(content_type)
content_type

But this use case is required use open-uri :(
https://ruby-doc.org/stdlib-2.5.1/libdoc/open-uri/rdoc/OpenURI/Meta.html#method-i-content_type

In my opinion, I think it would be good to be able to pass the content_type as an argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants