]> source.dussan.org Git - redmine.git/commitdiff
scm: fix non ascii text files displaying (#6256).
authorToshi MARUYAMA <marutosijp2@yahoo.co.jp>
Tue, 22 Mar 2011 15:31:17 +0000 (15:31 +0000)
committerToshi MARUYAMA <marutosijp2@yahoo.co.jp>
Tue, 22 Mar 2011 15:31:17 +0000 (15:31 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@5204 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/repositories_controller.rb

index bdb59dfe7f8cca4cb8e0acc06cd2377815f588e0..a4719423eafe945e23d1d680b6789c54b25379a0 100644 (file)
@@ -123,16 +123,31 @@ class RepositoriesController < ApplicationController
 
     @content = @repository.cat(@path, @rev)
     (show_error_not_found; return) unless @content
-    if 'raw' == params[:format] || @content.is_binary_data? ||
-         (@entry.size && @entry.size > Setting.file_max_size_displayed.to_i.kilobyte)
+    if 'raw' == params[:format] ||
+         (@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) ||
+         ! is_entry_text_data?(@content, @path)
       # Force the download
       send_data @content, :filename => filename_for_content_disposition(@path.split('/').last)
     else
       # Prevent empty lines when displaying a file with Windows style eol
+      # TODO: UTF-16
+      # Is this needs? AttachmentsController reads file simply.
       @content.gsub!("\r\n", "\n")
       @changeset = @repository.find_changeset_by_name(@rev)
-   end
+    end
+  end
+
+  def is_entry_text_data?(ent, path)
+    # UTF-16 contains "\x00".
+    # It is very strict that file contains less than 30% of ascii symbols 
+    # in non Western Europe.
+    return true if Redmine::MimeType.is_type?('text', path)
+    # Ruby 1.8.6 has a bug of integer divisions.
+    # http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F
+    return false if ent.is_binary_data?
+    true
   end
+  private :is_entry_text_data?
 
   def annotate
     @entry = @repository.entry(@path, @rev)