Skip to content

Commit ce63808

Browse files
committed
be explicit about inline vs. download in safe files redirect
if we're redirecting to safe files and it's not for download, it's for inline and we need to include that in the redirect url. also, be explicit about wanting to download when clicking a link labeled "Download". fixes #5433 test-plan: - have s3 storage enabled and a safe files domain - upload an image as a file submission - view the submission in speedgrader; image should be displayed inline rather than being downloaded - upload an image on the files tab - click the download link; should download the image rather than being shown in browser - preview the uploaded image from the files tab; it should display inline - upload a flash file on the files tab - preview the uploaded image from the files tab; it should display inline (independent check necessary because flash is touchy about content-disposition headers) - click the download link; should download the file rather than being shown in browser Change-Id: Ibd48eb4629aa26052d3d1bce90444146033b9eeb Reviewed-on: https://gerrit.instructure.com/8275 Tested-by: Hudson <hudson@instructure.com> Reviewed-by: Brian Palmer <brianp@instructure.com>
1 parent b97f204 commit ce63808

3 files changed

Lines changed: 61 additions & 2 deletions

File tree

app/controllers/application_controller.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,9 +990,21 @@ def safe_domain_file_url(attachment, host=nil, verifier = nil, download = false)
990990
# of content.
991991
opts = { :user_id => @current_user.try(:id), :ts => ts, :sf_verifier => sig }
992992
opts[:verifier] = verifier if verifier.present?
993-
opts[:download_frd] = 1 if download
993+
994+
if download
995+
# download "for realz, dude" (see later comments about :download)
996+
opts[:download_frd] = 1
997+
else
998+
# don't set :download here, because file_download_url won't like it. see
999+
# comment below for why we'd want to set :download
1000+
opts[:inline] = 1
1001+
end
9941002

9951003
if @context && Attachment.relative_context?(@context.class.base_ar_class) && @context == attachment.context
1004+
# so yeah, this is right. :inline=>1 wants :download=>1 to go along with
1005+
# it, so we're setting :download=>1 *because* we want to display inline.
1006+
opts[:download] = 1 unless download
1007+
9961008
# if the context is one that supports relative paths (which requires extra
9971009
# routes and stuff), then we'll build an actual named_context_url with the
9981010
# params for show_relative

app/views/files/show.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</object>
2525
<!--<![endif]-->
2626
</object>
27-
<a style="display:block;" href="<%= safe_domain_file_url(@attachment) %>">Download <%= @attachment.display_name %></a>
27+
<a style="display:block;" href="<%= safe_domain_file_url(@attachment, nil, nil, true) %>">Download <%= @attachment.display_name %></a>
2828
<% elsif @attachment.inline_content? %>
2929
<% jammit_js :file_inline %>
3030
<iframe id="file_content" src="<%= safe_domain_file_url(@attachment) %>" style="width: 100%; height: 400px;"></iframe>

spec/controllers/application_controller_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,53 @@
4242
end
4343
end
4444

45+
describe "safe_domain_file_user" do
46+
before :each do
47+
# safe_domain_file_url wants to use request.protocol
48+
@controller.stubs(:request).returns(mock(:protocol => ''))
49+
50+
@user = User.create!
51+
@attachment = @user.attachments.new(:filename => 'foo.png')
52+
@attachment.content_type = 'image/png'
53+
@attachment.save!
54+
55+
@common_params = {
56+
:user_id => nil,
57+
:ts => nil,
58+
:sf_verifier => nil,
59+
:only_path => true
60+
}
61+
end
62+
63+
it "should include inline=1 in url by default" do
64+
@controller.expects(:file_download_url).
65+
with(@attachment, @common_params.merge(:inline => 1)).
66+
returns('')
67+
@controller.send(:safe_domain_file_url, @attachment)
68+
end
69+
70+
it "should include :download=>1 in inline urls for relative contexts" do
71+
@controller.instance_variable_set(:@context, @attachment.context)
72+
@controller.stubs(:named_context_url).returns('')
73+
url = @controller.send(:safe_domain_file_url, @attachment)
74+
url.should match(/[\?&]download=1(&|$)/)
75+
end
76+
77+
it "should not include :download=>1 in download urls for relative contexts" do
78+
@controller.instance_variable_set(:@context, @attachment.context)
79+
@controller.stubs(:named_context_url).returns('')
80+
url = @controller.send(:safe_domain_file_url, @attachment, nil, nil, true)
81+
url.should_not match(/[\?&]download=1(&|$)/)
82+
end
83+
84+
it "should include download_frd=1 and not include inline=1 in url when specified as for download" do
85+
@controller.expects(:file_download_url).
86+
with(@attachment, @common_params.merge(:download_frd => 1)).
87+
returns('')
88+
@controller.send(:safe_domain_file_url, @attachment, nil, nil, true)
89+
end
90+
end
91+
4592
end
4693

4794

0 commit comments

Comments
 (0)