rails / marcel

Find the mime type of files, examining file, filename and declared type
Apache License 2.0
386 stars 67 forks source link

JSON file with html content detected as text/html since v1.0.0 #79

Open adamzapasnik opened 2 years ago

adamzapasnik commented 2 years ago

Hello, I've noticed that json files with html content are reported as text/html, which isn't correct, is it?. Accidentally, I tested also against v0.3.3 and it reported it correctly as application/json, so one could say it's a regression?

I'm raising this issue, but not sure if it's fixable or if it's even a bug? 🙈

require 'bundler/inline'
require "stringio"

gemfile do
  source 'https://rubygems.org'
  gem 'marcel', '1.0.2'
  # gem 'marcel', '0.3.3'
end

puts Marcel::VERSION
string = StringIO.new('{"a": "b"}')
puts string.read + ": " + Marcel::MimeType.for(string, name: "test.json")
string = StringIO.new('{"a": "<html"}')
puts string.read + ": " + Marcel::MimeType.for(string, name: "test.json")
string = StringIO.new('{"a": "<htm"}')
puts string.read + ": " + Marcel::MimeType.for(string, name: "test.json")

output for 1.0.2:

1.0.2
{"a": "b"}: application/json
{"a": "<html"}: text/html
{"a": "<htm"}: application/json

output for 0.3.3

0.3.3
{"a": "b"}: application/json
{"a": "<html"}: application/json
{"a": "<htm"}: application/json

Let me know if you need help with this 😅

janklimo commented 2 years ago

Seeing something similar with HTML data in CSV:

Marcel::VERSION
# => "1.0.2"
string = StringIO.new('a,b,c,d,<html><u>category</u>:description</html>')
Marcel::MimeType.for(string, name: "test.csv")
# => "text/html"