下载功能优化, 功能已实现
This commit is contained in:
parent
bdf419438e
commit
15accd7d9f
2
Gemfile
2
Gemfile
|
@ -58,8 +58,6 @@ group :test do
|
||||||
#end
|
#end
|
||||||
end
|
end
|
||||||
|
|
||||||
# gem 'rspec-rails' , '2.13.1'
|
|
||||||
# gem 'guard-rspec','2.5.0'
|
|
||||||
# Gems used only for assets and not required
|
# Gems used only for assets and not required
|
||||||
# in production environments by default.
|
# in production environments by default.
|
||||||
group :assets do
|
group :assets do
|
||||||
|
|
|
@ -182,6 +182,8 @@ class AttachmentsController < ApplicationController
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
logger.debug "upload ..... "
|
||||||
@attachment = Attachment.new(:file => request.raw_post)
|
@attachment = Attachment.new(:file => request.raw_post)
|
||||||
@attachment.author = User.current
|
@attachment.author = User.current
|
||||||
if !params[:project].nil?
|
if !params[:project].nil?
|
||||||
|
@ -190,6 +192,8 @@ class AttachmentsController < ApplicationController
|
||||||
end
|
end
|
||||||
@attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
|
@attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
|
||||||
saved = @attachment.save
|
saved = @attachment.save
|
||||||
|
logger.debug "upload save ..... "
|
||||||
|
logger.debug @attchement
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.js
|
format.js
|
||||||
|
|
|
@ -7,6 +7,14 @@ class ZipdownController < ApplicationController
|
||||||
SAVE_FOLDER = "#{Rails.root}/files"
|
SAVE_FOLDER = "#{Rails.root}/files"
|
||||||
OUTPUT_FOLDER = "#{Rails.root}/tmp/archiveZip"
|
OUTPUT_FOLDER = "#{Rails.root}/tmp/archiveZip"
|
||||||
|
|
||||||
|
#统一下载功能
|
||||||
|
def download
|
||||||
|
begin
|
||||||
|
send_file "#{OUTPUT_FOLDER}/#{params[:file]}", :filename => params[:filename], :type => detect_content_type(params[:file])
|
||||||
|
rescue => e
|
||||||
|
render file: 'public/no_file_found.html'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def assort
|
def assort
|
||||||
if params[:obj_class] == "Bid"
|
if params[:obj_class] == "Bid"
|
||||||
|
@ -21,8 +29,14 @@ class ZipdownController < ApplicationController
|
||||||
else
|
else
|
||||||
logger.error "[ZipDown#assort] ===> #{params[:obj_class]} unKown !!"
|
logger.error "[ZipDown#assort] ===> #{params[:obj_class]} unKown !!"
|
||||||
end
|
end
|
||||||
send_file zipfile, :filename => bid.name + ".zip", :type => detect_content_type(zipfile) if zipfile
|
|
||||||
|
|
||||||
|
if zipfile
|
||||||
|
if zipfile.length > 1
|
||||||
|
@mut_down_files = zipfile.map{|x| File.basename(x)}
|
||||||
|
else
|
||||||
|
send_file zipfile, :filename => bid.name + ".zip", :type => detect_content_type(zipfile)
|
||||||
|
end
|
||||||
|
end
|
||||||
#rescue Exception => e
|
#rescue Exception => e
|
||||||
# render file: 'public/no_file_found.html'
|
# render file: 'public/no_file_found.html'
|
||||||
end
|
end
|
||||||
|
@ -71,7 +85,14 @@ class ZipdownController < ApplicationController
|
||||||
bid_homework_path << zip_homework_by_user(homeattach)
|
bid_homework_path << zip_homework_by_user(homeattach)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
zipping "#{Time.now.to_i}_#{bid.name}.zip", bid_homework_path, OUTPUT_FOLDER
|
|
||||||
|
zips = split_pack_files(bid_homework_path, Setting.pack_attachment_max_size.to_i)
|
||||||
|
x = 0
|
||||||
|
|
||||||
|
zips.map { |o|
|
||||||
|
x += 1
|
||||||
|
zipping "#{Time.now.to_i}_#{bid.name}_#{x}.zip", o, OUTPUT_FOLDER
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def zip_homework_by_user(homeattach)
|
def zip_homework_by_user(homeattach)
|
||||||
|
@ -93,15 +114,21 @@ class ZipdownController < ApplicationController
|
||||||
def zipping(zip_name_refer, files_paths, output_path, is_attachment=false, not_exist_file=[])
|
def zipping(zip_name_refer, files_paths, output_path, is_attachment=false, not_exist_file=[])
|
||||||
# 输入待打包的文件列表,已经打包文件定位到ouput_path
|
# 输入待打包的文件列表,已经打包文件定位到ouput_path
|
||||||
ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE')
|
ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE')
|
||||||
input_filename = files_paths
|
|
||||||
|
|
||||||
rename_zipfile = zip_name_refer ||= "#{Time.now.to_i.to_s}.zip"
|
rename_zipfile = zip_name_refer ||= "#{Time.now.to_i.to_s}.zip"
|
||||||
zipfile_name = "#{output_path}/#{rename_zipfile}"
|
zipfile_name = "#{output_path}/#{rename_zipfile}"
|
||||||
|
|
||||||
Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name))
|
Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name))
|
||||||
|
|
||||||
|
unless is_attachment
|
||||||
|
#都是zip合并,没必要再费力压缩了
|
||||||
|
Zip.default_compression = Zlib::NO_COMPRESSION
|
||||||
|
else
|
||||||
|
Zip.default_compression = Zlib::DEFAULT_COMPRESSION
|
||||||
|
end
|
||||||
|
|
||||||
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
|
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
|
||||||
input_filename.each do |filename|
|
files_paths.each do |filename|
|
||||||
flag = true
|
flag = true
|
||||||
index = 1
|
index = 1
|
||||||
rename_file = ic.iconv( (File.basename(filename)) ).to_s
|
rename_file = ic.iconv( (File.basename(filename)) ).to_s
|
||||||
|
@ -128,6 +155,34 @@ class ZipdownController < ApplicationController
|
||||||
# logger.error "[zipdown#zipping] ===> #{e}"
|
# logger.error "[zipdown#zipping] ===> #{e}"
|
||||||
# @error = e
|
# @error = e
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 合理分配文件打包
|
||||||
|
# 如果小于 pack_attachment_max_size, 则返回单个文件
|
||||||
|
# 反之则切分为多个文件组返回
|
||||||
|
def split_pack_files(files, pack_attachment_max_size)
|
||||||
|
max_size = 0
|
||||||
|
last_files = []
|
||||||
|
ret_files = []
|
||||||
|
files.each do |f|
|
||||||
|
if (max_size += File.size(f)) > pack_attachment_max_size
|
||||||
|
max_size = 0
|
||||||
|
if last_files.empty? #如果单个文件超过大小,也将此文件作为一组
|
||||||
|
ret_files << [f]
|
||||||
|
last_files.clear
|
||||||
|
else
|
||||||
|
ret_files << last_files
|
||||||
|
last_files.clear
|
||||||
|
redo
|
||||||
|
end
|
||||||
|
else
|
||||||
|
last_files << f
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ret_files << last_files unless last_files.empty?
|
||||||
|
ret_files
|
||||||
|
end
|
||||||
|
|
||||||
def detect_content_type(name)
|
def detect_content_type(name)
|
||||||
content_type = Redmine::MimeType.of(name)
|
content_type = Redmine::MimeType.of(name)
|
||||||
content_type.to_s
|
content_type.to_s
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
<%= wikitoolbar_for 'settings_welcome_text' %>
|
<%= wikitoolbar_for 'settings_welcome_text' %>
|
||||||
|
|
||||||
<p style="padding-left: 180px;"><%= setting_text_field :attachment_max_size, :size => 6 %> <%= l(:"number.human.storage_units.units.kb") %></p>
|
<p style="padding-left: 180px;"><%= setting_text_field :attachment_max_size, :size => 6 %> <%= l(:"number.human.storage_units.units.kb") %></p>
|
||||||
|
<p style="padding-left: 180px;"><%= setting_text_field :pack_attachment_max_size, :size => 6 %> <%= l(:"number.human.storage_units.units.kb") %></p>
|
||||||
|
|
||||||
<p style="padding-left: 180px;"><%= setting_text_field :per_page_options, :size => 20 %>
|
<p style="padding-left: 180px;"><%= setting_text_field :per_page_options, :size => 20 %>
|
||||||
<em class="info"><%= l(:text_comma_separated) %></em></p>
|
<em class="info"><%= l(:text_comma_separated) %></em></p>
|
||||||
|
|
|
@ -1,2 +1,11 @@
|
||||||
|
<% unless @mut_down_files %>
|
||||||
<h1>Download Status:</h1>
|
<h1>Download Status:</h1>
|
||||||
<%= @error.class %>
|
<%= @error.class %>
|
||||||
|
<% else %>
|
||||||
|
<h1>下载文件包太大,分成<%= @mut_down_files.count %>个下载包</h1>
|
||||||
|
<ul>
|
||||||
|
<% @mut_down_files.each_with_index do |file,i| %>
|
||||||
|
<li><%= link_to "第#{i+1}个文件包", zipdown_download_path(file: File.basename(file)) %></li>
|
||||||
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
<% end %>
|
|
@ -177,6 +177,7 @@ en:
|
||||||
setting_login_required: Authentication required
|
setting_login_required: Authentication required
|
||||||
setting_self_registration: Self-registration
|
setting_self_registration: Self-registration
|
||||||
setting_attachment_max_size: Maximum attachment size
|
setting_attachment_max_size: Maximum attachment size
|
||||||
|
setting_pack_attachment_max_size: Maximum pack attachment size
|
||||||
setting_issues_export_limit: Issues export limit
|
setting_issues_export_limit: Issues export limit
|
||||||
setting_mail_from: Emission email address
|
setting_mail_from: Emission email address
|
||||||
setting_bcc_recipients: Blind carbon copy recipients (bcc)
|
setting_bcc_recipients: Blind carbon copy recipients (bcc)
|
||||||
|
|
|
@ -196,6 +196,7 @@ zh:
|
||||||
setting_login_required: 要求认证
|
setting_login_required: 要求认证
|
||||||
setting_self_registration: 允许自注册
|
setting_self_registration: 允许自注册
|
||||||
setting_attachment_max_size: 附件大小限制
|
setting_attachment_max_size: 附件大小限制
|
||||||
|
setting_pack_attachment_max_size: 附件打包最大限制
|
||||||
setting_issues_export_limit: 问题导出条目的限制
|
setting_issues_export_limit: 问题导出条目的限制
|
||||||
setting_mail_from: 邮件发件人地址
|
setting_mail_from: 邮件发件人地址
|
||||||
setting_bcc_recipients: 使用密件抄送 (bcc)
|
setting_bcc_recipients: 使用密件抄送 (bcc)
|
||||||
|
|
|
@ -105,6 +105,7 @@ RedmineApp::Application.routes.draw do
|
||||||
namespace :zipdown do
|
namespace :zipdown do
|
||||||
match 'assort'
|
match 'assort'
|
||||||
match 'download_user_homework', :as => :download_user_homework
|
match 'download_user_homework', :as => :download_user_homework
|
||||||
|
match 'download'
|
||||||
end
|
end
|
||||||
namespace :test do
|
namespace :test do
|
||||||
match 'courselist'
|
match 'courselist'
|
||||||
|
|
|
@ -46,7 +46,10 @@ session_timeout:
|
||||||
default: 2880
|
default: 2880
|
||||||
attachment_max_size:
|
attachment_max_size:
|
||||||
format: int
|
format: int
|
||||||
default: 5120
|
default: 51200
|
||||||
|
pack_attachment_max_size:
|
||||||
|
format: int
|
||||||
|
default: 51200
|
||||||
issues_export_limit:
|
issues_export_limit:
|
||||||
format: int
|
format: int
|
||||||
default: 500
|
default: 500
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
class AddPackAttachmentMaxSizeToSettings < ActiveRecord::Migration
|
||||||
|
def up
|
||||||
|
Setting.where(name: 'attachment_max_size').update_all(value: 51200)
|
||||||
|
Setting.create(name: 'pack_attachment_max_size', value: 51200 )
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
Setting.delete(name: 'pack_attachment_max_size')
|
||||||
|
Setting.where(name: 'attachment_max_size').update_all(value: 204800)
|
||||||
|
end
|
||||||
|
end
|
|
@ -11,7 +11,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended to check this file into your version control system.
|
# It's strongly recommended to check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(:version => 20150305081132) do
|
ActiveRecord::Schema.define(:version => 20150309090143) do
|
||||||
|
|
||||||
create_table "activities", :force => true do |t|
|
create_table "activities", :force => true do |t|
|
||||||
t.integer "act_id", :null => false
|
t.integer "act_id", :null => false
|
||||||
|
|
Loading…
Reference in New Issue