Commit 2aee0f09 authored by Enrique García Cota's avatar Enrique García Cota

updated repo controller, too

parent dd596d6d
...@@ -118,41 +118,52 @@ class RepositoriesController < ApplicationController ...@@ -118,41 +118,52 @@ class RepositoriesController < ApplicationController
# If the entry is a dir, show the browser # If the entry is a dir, show the browser
(show; return) if @entry.is_dir? (show; return) if @entry.is_dir?
@content = @repository.cat(@path, @rev) f = nil
(show_error_not_found; return) unless @content begin
if 'raw' == params[:format] || f = @repository.save_entry_to_temp_file(@path, @rev)
(@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) ||
! is_entry_text_data?(@content, @path) if params[:format] == 'raw' ||
# Force the download f.size > Setting.file_max_size_displayed.to_i.kilobyte ||
send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) } is_binary_data?(f, @path)
send_type = Redmine::MimeType.of(@path) # Force the download
send_opt[:type] = send_type.to_s if send_type send_type = Redmine::MimeType.of(@path)
send_data @content, send_opt options = {
else :filename => filename_for_content_disposition(@path.split('/').last),
# Prevent empty lines when displaying a file with Windows style eol :disposition => 'attachment'
# TODO: UTF-16 }
# Is this needs? AttachmentsController reads file simply. options[:type] = send_type.to_s if send_type
@content.gsub!("\r\n", "\n") send_file(f.path, options)
@changeset = @repository.find_changeset_by_name(@rev) else
@content = f.read
@changeset = @repository.find_changeset_by_name(@rev)
end
rescue Errno::ENOENT
show_error_not_found
return
ensure
f.close if f
end end
end end
def is_entry_text_data?(ent, path) def is_binary_data?(file, path)
# UTF-16 contains "\x00".
# It is very strict that file contains less than 30% of ascii symbols return false if Redmine::MimeType.is_type?('text', path)
# in non Western Europe.
return true if Redmine::MimeType.is_type?('text', path) # First block of the file is examined for odd
# Ruby 1.8.6 has a bug of integer divisions. # characters such as strange control codes or char-
# http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F # acters with the high bit set. If too many strange
if ent.respond_to?("is_binary_data?") && ent.is_binary_data? # Ruby 1.8.x and <1.9.2 # characters (>30%) are found, it's a binary file,
return false # otherwise it's a text file. Also, any file con-
elsif ent.respond_to?(:force_encoding) && (ent.dup.force_encoding("UTF-8") != ent.dup.force_encoding("BINARY") ) # Ruby 1.9.2 # taining null in the first block is considered a
# TODO: need to handle edge cases of non-binary content that isn't UTF-8 # binary file.
return false
end blk = file.read(Setting.file_max_size_displayed.to_i.kilobyte)
true return blk.size == 0 ||
blk.count("^ -~", "^\r\n") / blk.size > 0.3 ||
blk.count("\x00") > 0
end end
private :is_entry_text_data? private :is_binary_data?
def annotate def annotate
@entry = @repository.entry(@path, @rev) @entry = @repository.entry(@path, @rev)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment