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
# If the entry is a dir, show the browser
(show; return) if @entry.is_dir?
@content = @repository.cat(@path, @rev)
(show_error_not_found; return) unless @content
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_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
send_type = Redmine::MimeType.of(@path)
send_opt[:type] = send_type.to_s if send_type
send_data @content, send_opt
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)
f = nil
begin
f = @repository.save_entry_to_temp_file(@path, @rev)
if params[:format] == 'raw' ||
f.size > Setting.file_max_size_displayed.to_i.kilobyte ||
is_binary_data?(f, @path)
# Force the download
send_type = Redmine::MimeType.of(@path)
options = {
:filename => filename_for_content_disposition(@path.split('/').last),
:disposition => 'attachment'
}
options[:type] = send_type.to_s if send_type
send_file(f.path, options)
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
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
if ent.respond_to?("is_binary_data?") && ent.is_binary_data? # Ruby 1.8.x and <1.9.2
return false
elsif ent.respond_to?(:force_encoding) && (ent.dup.force_encoding("UTF-8") != ent.dup.force_encoding("BINARY") ) # Ruby 1.9.2
# TODO: need to handle edge cases of non-binary content that isn't UTF-8
return false
end
true
def is_binary_data?(file, path)
return false if Redmine::MimeType.is_type?('text', path)
# First block of the file is examined for odd
# characters such as strange control codes or char-
# acters with the high bit set. If too many strange
# characters (>30%) are found, it's a binary file,
# otherwise it's a text file. Also, any file con-
# taining null in the first block is considered a
# binary file.
blk = file.read(Setting.file_max_size_displayed.to_i.kilobyte)
return blk.size == 0 ||
blk.count("^ -~", "^\r\n") / blk.size > 0.3 ||
blk.count("\x00") > 0
end
private :is_entry_text_data?
private :is_binary_data?
def annotate
@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