From d77c1d2829f985c418442940c623ec6ec5d5457b Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 8 Jun 2008 18:26:39 +0000 Subject: [PATCH] Unified diff viewer for attached files with .patch or .diff extension (#1403). git-svn-id: http://redmine.rubyforge.org/svn/trunk@1516 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/attachments_controller.rb | 13 ++++++++++--- app/helpers/attachments_helper.rb | 4 ++++ app/helpers/issues_helper.rb | 2 +- app/models/attachment.rb | 4 ++++ app/views/attachments/_links.rhtml | 2 +- app/views/attachments/diff.rhtml | 15 +++++++++++++++ config/routes.rb | 2 ++ 7 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 app/views/attachments/diff.rhtml diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 4e87e5442..cfc15669f 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -19,19 +19,26 @@ class AttachmentsController < ApplicationController layout 'base' before_filter :find_project, :check_project_privacy + def show + if @attachment.is_diff? + @diff = File.new(@attachment.diskfile, "rb").read + render :action => 'diff' + else + download + end + end + def download # images are sent inline send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), :type => @attachment.content_type, :disposition => (@attachment.image? ? 'inline' : 'attachment') - rescue - # in case the disk file was deleted - render_404 end private def find_project @attachment = Attachment.find(params[:id]) + render_404 and return false unless File.readable?(@attachment.diskfile) @project = @attachment.project rescue render_404 diff --git a/app/helpers/attachments_helper.rb b/app/helpers/attachments_helper.rb index 989cd3e66..ebf417bab 100644 --- a/app/helpers/attachments_helper.rb +++ b/app/helpers/attachments_helper.rb @@ -22,4 +22,8 @@ module AttachmentsHelper render :partial => 'attachments/links', :locals => {:attachments => attachments, :options => options} end end + + def to_utf8(str) + str + end end diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index 6013f1ec8..915a80b2a 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -91,7 +91,7 @@ module IssuesHelper 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) # Link to the attachment if it has not been removed - value = link_to(value, :controller => 'attachments', :action => 'download', :id => detail.prop_key) + value = link_to(value, :controller => 'attachments', :action => 'show', :id => detail.prop_key) else value = content_tag("i", h(value)) if value end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 45bbd5428..95de4837a 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -88,6 +88,10 @@ class Attachment < ActiveRecord::Base self.filename =~ /\.(jpe?g|gif|png)$/i end + def is_diff? + self.filename =~ /\.(patch|diff)$/i + end + private def sanitize_filename(value) # get only the filename, not the whole path diff --git a/app/views/attachments/_links.rhtml b/app/views/attachments/_links.rhtml index 4d485548b..9e3ac747c 100644 --- a/app/views/attachments/_links.rhtml +++ b/app/views/attachments/_links.rhtml @@ -1,6 +1,6 @@
<% for attachment in attachments %> -

<%= link_to attachment.filename, {:controller => 'attachments', :action => 'download', :id => attachment }, :class => 'icon icon-attachment' -%> +

<%= link_to attachment.filename, {:controller => 'attachments', :action => 'show', :id => attachment }, :class => 'icon icon-attachment' -%> <%= h(" - #{attachment.description}") unless attachment.description.blank? %> (<%= number_to_human_size attachment.filesize %>) <% if options[:delete_url] %> diff --git a/app/views/attachments/diff.rhtml b/app/views/attachments/diff.rhtml new file mode 100644 index 000000000..4a9f5c9bd --- /dev/null +++ b/app/views/attachments/diff.rhtml @@ -0,0 +1,15 @@ +

<%=h @attachment.filename %>

+ +
+

<%= h("#{@attachment.description} - ") unless @attachment.description.blank? %> + <%= @attachment.author %>, <%= format_time(@attachment.created_on) %>

+

<%= link_to l(:button_download), {:controller => 'attachments', :action => 'download', :id => @attachment } -%> + (<%= number_to_human_size @attachment.filesize %>)

+ +
+  +<%= render :partial => 'common/diff', :locals => {:diff => @diff, :diff_type => @diff_type} %> + +<% content_for :header_tags do -%> + <%= stylesheet_link_tag "scm" -%> +<% end -%> diff --git a/config/routes.rb b/config/routes.rb index 0edb71a06..3cb18d1bd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -31,6 +31,8 @@ ActionController::Routing::Routes.draw do |map| omap.repositories_entry 'repositories/annotate/:id/*path', :action => 'annotate' end + map.connect 'attachments/:id', :controller => 'attachments', :action => 'show' + # Allow downloading Web Service WSDL as a file with an extension # instead of a file named 'wsdl' map.connect ':controller/service.wsdl', :action => 'wsdl' -- 2.39.5