From ea99ab07cc8cdcb715096b7dbb412518baf590ba Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Sat, 22 Oct 2022 21:15:37 +0200 Subject: [PATCH] Always provide format with attachment URLs When linking to an attachment without the format, the browser does not necessarily know what to do with the file. Adding the suffix by default makes that better: Firefox won't, for example, try rendering a PDF file as HTML. --- app/models/alchemy/attachment/url.rb | 1 + spec/models/alchemy/attachment_spec.rb | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/alchemy/attachment/url.rb b/app/models/alchemy/attachment/url.rb index 9a23e808b5..1a45204e5b 100644 --- a/app/models/alchemy/attachment/url.rb +++ b/app/models/alchemy/attachment/url.rb @@ -23,6 +23,7 @@ def initialize(attachment) # @return [String] # def call(options = {}) + options[:format] ||= @attachment.suffix if options.delete(:download) routes.download_attachment_path(@attachment, options) else diff --git a/spec/models/alchemy/attachment_spec.rb b/spec/models/alchemy/attachment_spec.rb index 8ae673a7ba..f8df895354 100644 --- a/spec/models/alchemy/attachment_spec.rb +++ b/spec/models/alchemy/attachment_spec.rb @@ -63,14 +63,14 @@ module Alchemy let(:attachment) { create(:alchemy_attachment) } it "returns local path" do - is_expected.to eq "/attachment/#{attachment.id}/show" + is_expected.to eq "/attachment/#{attachment.id}/show.png" end context "with download enabled" do subject { attachment.url(download: true) } it "returns local download path" do - is_expected.to eq "/attachment/#{attachment.id}/download" + is_expected.to eq "/attachment/#{attachment.id}/download.png" end context "with extra params given" do @@ -88,7 +88,7 @@ module Alchemy subject { attachment.url(download: false) } it "returns local path" do - is_expected.to eq "/attachment/#{attachment.id}/show" + is_expected.to eq "/attachment/#{attachment.id}/show.png" end context "with extra params given" do