Appends the filename to the attachment url so that clients that ignore content-disposition http header get the real filename (#1649).

git-svn-id: http://redmine.rubyforge.org/svn/trunk@1686 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2008-07-22 17:55:19 +00:00
parent 8a7bfc72b2
commit 9b579de9e2
10 changed files with 40 additions and 10 deletions

View File

@ -43,6 +43,9 @@ class AttachmentsController < ApplicationController
private
def find_project
@attachment = Attachment.find(params[:id])
# Show 404 if the filename in the url is wrong
raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
@project = @attachment.project
permission = @attachment.container.is_a?(Version) ? :view_files : "view_#{@attachment.container.class.name.underscore.pluralize}".to_sym
allowed = User.current.allowed_to?(permission, @project)

View File

@ -47,6 +47,17 @@ module ApplicationHelper
link_to "#{issue.tracker.name} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue}, options
end
# Generates a link to an attachment.
# Options:
# * :text - Link text (default to attachment filename)
# * :download - Force download (default: false)
def link_to_attachment(attachment, options={})
text = options.delete(:text) || attachment.filename
action = options.delete(:download) ? 'download' : 'show'
link_to(h(text), {:controller => 'attachments', :action => action, :id => attachment, :filename => attachment.filename }, options)
end
def toggle_link(name, id, options={})
onclick = "Element.toggle('#{id}'); "
onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")

View File

@ -95,9 +95,9 @@ module IssuesHelper
label = content_tag('strong', label)
old_value = content_tag("i", h(old_value)) if detail.old_value
old_value = content_tag("strike", old_value) if detail.old_value and (!detail.value or detail.value.empty?)
if detail.property == 'attachment' && !value.blank? && Attachment.find_by_id(detail.prop_key)
if detail.property == 'attachment' && !value.blank? && a = Attachment.find_by_id(detail.prop_key)
# Link to the attachment if it has not been removed
value = link_to(value, :controller => 'attachments', :action => 'show', :id => detail.prop_key)
value = link_to_attachment(a)
else
value = content_tag("i", h(value)) if value
end

View File

@ -26,7 +26,7 @@ class Attachment < ActiveRecord::Base
validates_length_of :disk_filename, :maximum => 255
acts_as_event :title => :filename,
:url => Proc.new {|o| {:controller => 'attachments', :action => 'download', :id => o.id}}
:url => Proc.new {|o| {:controller => 'attachments', :action => 'download', :id => o.id, :filename => o.filename}}
cattr_accessor :storage_path
@@storage_path = "#{RAILS_ROOT}/files"

View File

@ -1,6 +1,6 @@
<div class="attachments">
<% for attachment in attachments %>
<p><%= link_to attachment.filename, {:controller => 'attachments', :action => 'show', :id => attachment }, :class => 'icon icon-attachment' -%>
<p><%= link_to_attachment attachment, :class => 'icon icon-attachment' -%>
<%= h(" - #{attachment.description}") unless attachment.description.blank? %>
<span class="size">(<%= number_to_human_size attachment.filesize %>)</span>
<% if options[:delete_url] %>

View File

@ -3,7 +3,7 @@
<div class="attachments">
<p><%= h("#{@attachment.description} - ") unless @attachment.description.blank? %>
<span class="author"><%= @attachment.author %>, <%= format_time(@attachment.created_on) %></span></p>
<p><%= link_to l(:button_download), {:controller => 'attachments', :action => 'download', :id => @attachment } -%>
<p><%= link_to_attachment @attachment, :text => l(:button_download), :download => true -%>
<span class="size">(<%= number_to_human_size @attachment.filesize %>)</span></p>
</div>

View File

@ -3,7 +3,7 @@
<div class="attachments">
<p><%= h("#{@attachment.description} - ") unless @attachment.description.blank? %>
<span class="author"><%= @attachment.author %>, <%= format_time(@attachment.created_on) %></span></p>
<p><%= link_to l(:button_download), {:controller => 'attachments', :action => 'download', :id => @attachment } -%>
<p><%= link_to_attachment @attachment, :text => l(:button_download), :download => true -%>
<span class="size">(<%= number_to_human_size @attachment.filesize %>)</span></p>
</div>

View File

@ -23,8 +23,7 @@
<% for file in version.attachments %>
<tr class="<%= cycle("odd", "even") %>">
<td></td>
<td><%= link_to(h(file.filename), {:controller => 'attachments', :action => 'download', :id => file},
:title => file.description) %></td>
<td><%= link_to_attachment file, :download => true, :title => file.description %></td>
<td align="center"><%= format_time(file.created_on) %></td>
<td align="center"><%= number_to_human_size(file.filesize) %></td>
<td align="center"><%= file.downloads %></td>

View File

@ -32,8 +32,10 @@ ActionController::Routing::Routes.draw do |map|
omap.repositories_revision 'repositories/revision/:id/:rev', :action => 'revision'
end
map.connect 'attachments/:id', :controller => 'attachments', :action => 'show'
map.connect 'attachments/:id', :controller => 'attachments', :action => 'show', :id => /\d+/
map.connect 'attachments/:id/:filename', :controller => 'attachments', :action => 'show', :id => /\d+/, :filename => /.*/
map.connect 'attachments/download/:id/:filename', :controller => 'attachments', :action => 'download', :id => /\d+/, :filename => /.*/
# Allow downloading Web Service WSDL as a file with an extension
# instead of a file named 'wsdl'
map.connect ':controller/service.wsdl', :action => 'wsdl'

View File

@ -33,6 +33,21 @@ class AttachmentsControllerTest < Test::Unit::TestCase
User.current = nil
end
def test_routing
assert_routing('/attachments/1', :controller => 'attachments', :action => 'show', :id => '1')
assert_routing('/attachments/1/filename.ext', :controller => 'attachments', :action => 'show', :id => '1', :filename => 'filename.ext')
assert_routing('/attachments/download/1', :controller => 'attachments', :action => 'download', :id => '1')
assert_routing('/attachments/download/1/filename.ext', :controller => 'attachments', :action => 'download', :id => '1', :filename => 'filename.ext')
end
def test_recognizes
assert_recognizes({:controller => 'attachments', :action => 'show', :id => '1'}, '/attachments/1')
assert_recognizes({:controller => 'attachments', :action => 'show', :id => '1'}, '/attachments/show/1')
assert_recognizes({:controller => 'attachments', :action => 'show', :id => '1', :filename => 'filename.ext'}, '/attachments/1/filename.ext')
assert_recognizes({:controller => 'attachments', :action => 'download', :id => '1'}, '/attachments/download/1')
assert_recognizes({:controller => 'attachments', :action => 'download', :id => '1', :filename => 'filename.ext'},'/attachments/download/1/filename.ext')
end
def test_show_diff
get :show, :id => 5
assert_response :success