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)
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(); ")
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
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"
<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] %>
<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>
<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>
<% 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>
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'
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