From 04c7dbafa32991836a688e979e03b117e8ec8c19 Mon Sep 17 00:00:00 2001 From: Jeff Wong Date: Thu, 17 May 2018 14:43:22 -0700 Subject: [PATCH] FIX: manifest.json better detection at mime type. Find size if uploaded --- app/controllers/metadata_controller.rb | 13 +++++++------ spec/controllers/metadata_controller_spec.rb | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/controllers/metadata_controller.rb b/app/controllers/metadata_controller.rb index 3852daf8a6f..0ddb212ebdd 100644 --- a/app/controllers/metadata_controller.rb +++ b/app/controllers/metadata_controller.rb @@ -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 diff --git a/spec/controllers/metadata_controller_spec.rb b/spec/controllers/metadata_controller_spec.rb index 842e4664748..96558ec0c76 100644 --- a/spec/controllers/metadata_controller_spec.rb +++ b/spec/controllers/metadata_controller_spec.rb @@ -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