FIX: manifest.json better detection at mime type. Find size if uploaded

This commit is contained in:
Jeff Wong 2018-05-17 14:43:22 -07:00
parent 4fd17abb76
commit 04c7dbafa3
2 changed files with 9 additions and 8 deletions

View File

@ -14,6 +14,7 @@ class MetadataController < ApplicationController
def default_manifest
logo = SiteSetting.large_icon_url.presence || SiteSetting.logo_small_url.presence || SiteSetting.apple_touch_icon_url.presence
file_info = get_file_info(logo)
manifest = {
name: SiteSetting.title,
@ -29,8 +30,8 @@ class MetadataController < ApplicationController
icons: [
{
src: logo,
sizes: "512x512",
type: guess_mime(logo)
sizes: file_info[:size],
type: file_info[:type]
}
]
}
@ -50,10 +51,10 @@ class MetadataController < ApplicationController
manifest
end
def guess_mime(filename)
extension = filename.split(".").last
valid_image_mimes = { png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", ico: "image/x-icon" }
valid_image_mimes[extension.to_sym] || "image/png"
def get_file_info(filename)
type = MiniMime.lookup_by_filename(filename)&.content_type || "image/png"
upload = Upload.find_by_url(filename)
{ size: "#{upload&.width || 512}x#{upload&.height || 512}", type: type }
end
end

View File

@ -17,10 +17,10 @@ RSpec.describe MetadataController do
end
it 'can guess mime types' do
SiteSetting.large_icon_url = "http://big.square/ico.ico"
SiteSetting.large_icon_url = "http://big.square/ico.jpg"
get :manifest
manifest = JSON.parse(response.body)
expect(manifest["icons"].first["type"]).to eq("image/x-icon")
expect(manifest["icons"].first["type"]).to eq("image/jpeg")
end
it 'defaults to png' do