]> source.dussan.org Git - redmine.git/commitdiff
Unified diff viewer for attached files with .patch or .diff extension (#1403).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 8 Jun 2008 18:26:39 +0000 (18:26 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 8 Jun 2008 18:26:39 +0000 (18:26 +0000)
git-svn-id: http://redmine.rubyforge.org/svn/trunk@1516 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/attachments_controller.rb
app/helpers/attachments_helper.rb
app/helpers/issues_helper.rb
app/models/attachment.rb
app/views/attachments/_links.rhtml
app/views/attachments/diff.rhtml [new file with mode: 0644]
config/routes.rb

index 4e87e54422d3eee80e606fa6726360e9c8e2a285..cfc15669ffa566c24bc383e2b76f78c34e993799 100644 (file)
@@ -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
index 989cd3e66b14b608c1f77f829a9e4096e8bf52ee..ebf417baba70158e5417ebed13afe29f2153d182 100644 (file)
@@ -22,4 +22,8 @@ module AttachmentsHelper
       render :partial => 'attachments/links', :locals => {:attachments => attachments, :options => options}
     end
   end
+  
+  def to_utf8(str)
+    str
+  end
 end
index 6013f1ec8bb5d74b8b8c5eca882cda214d0214ae..915a80b2a67febd50d93af02ebe8d21a6d95f695 100644 (file)
@@ -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
index 45bbd5428b996749499d73be940480e2f9f970b6..95de4837a96b87f79c1113df63770ab0221242df 100644 (file)
@@ -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
index 4d485548b57bc91cc17335857e9c660325f9305d..9e3ac747c7ed0e759c3d0c78295cea0414ff19ec 100644 (file)
@@ -1,6 +1,6 @@
 <div class="attachments">
 <% for attachment in attachments %>
-<p><%= link_to attachment.filename, {:controller => 'attachments', :action => 'download', :id => attachment }, :class => 'icon icon-attachment' -%>
+<p><%= link_to attachment.filename, {:controller => 'attachments', :action => 'show', :id => 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] %>
diff --git a/app/views/attachments/diff.rhtml b/app/views/attachments/diff.rhtml
new file mode 100644 (file)
index 0000000..4a9f5c9
--- /dev/null
@@ -0,0 +1,15 @@
+<h2><%=h @attachment.filename %></h2>
+
+<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 } -%>
+   <span class="size">(<%= number_to_human_size @attachment.filesize %>)</span></p>
+
+</div>
+&nbsp;
+<%= render :partial => 'common/diff', :locals => {:diff => @diff, :diff_type => @diff_type} %>
+
+<% content_for :header_tags do -%>
+    <%= stylesheet_link_tag "scm" -%>
+<% end -%>
index 0edb71a06af938da189e1ea1c8cf1dbdbc206a5d..3cb18d1bdadec9dd7f1a2252a84bcdba9e23b35c 100644 (file)
@@ -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'