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

View File

@ -17,10 +17,10 @@ RSpec.describe MetadataController do
end end
it 'can guess mime types' do 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 get :manifest
manifest = JSON.parse(response.body) 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 end
it 'defaults to png' do it 'defaults to png' do