Merge branch 'develop' of 10.0.47.245:/home/trustie2 into develop

exceptionHandle
Wen 11 years ago
commit 4e567afd0a

@ -1,68 +1,115 @@
class ZipdownController < ApplicationController class ZipdownController < ApplicationController
def assort SAVE_FOLDER = "#{Rails.root}/files"
obj_class = params[:obj_class] OUTPUT_FOLDER = "#{Rails.root}/tmp/archiveZip"
obj_id = params[:obj_id]
obj = obj_class.constantize.find(obj_id) def assort
case obj.class.to_s.to_sym obj_class = params[:obj_class]
when :Bid obj_id = params[:obj_id]
zip obj obj = obj_class.constantize.find(obj_id)
else zipfile = nil
logger.error "[ZipDown#assort] ===> #{obj.class.to_s.to_sym} unKown !!" case obj.class.to_s.to_sym
end when :Bid
zipfile = zip_bid obj
rescue NameError, ActiveRecord::RecordNotFound => e else
logger.error "[ZipDown] ===> #{e}" logger.error "[ZipDown#assort] ===> #{obj.class.to_s.to_sym} unKown !!"
@error = e end
end send_file zipfile, :filename => obj.name, :type => detect_content_type(zipfile) if zipfile
private #rescue NameError, ActiveRecord::RecordNotFound => e
#logger.error "[ZipDown] ===> #{e}"
def zip bid #@error = e
# Todo: User Access Controll end
homeworks_attach_path = []
bid.homeworks.each do |homeattach| private
homeattach.attachments.each do |attach|
length = attach.storage_path.length def zip_bid(bid)
homeworks_attach_path << attach.diskfile.to_s.slice((length+1)..-1) # Todo: User Access Controll
end
end homeattaches = bid.homeworks
@paths = homeworks_attach_path # 得到每一个人所有文件打包的zip文件
zipfile = ziping homeworks_attach_path # 并将每一个人的zip打包为一个并返回路径
send_file zipfile, :filename => bid.name, user_zip_paths = homeattaches.map do |homeattach|
:type => detect_content_type(zipfile) zip_homework_by_user homeattach
rescue Errno::ENOENT => e end
logger.error "[Errno::ENOENT] ===> #{e}" zipping "#{Time.now.to_i}_#{bid.name}.zip", user_zip_paths, OUTPUT_FOLDER
@error = e
end #@paths = homeworks_attach_path
#zipfile = ziping homeworks_attach_path
def ziping files_path #send_file zipfile, :filename => bid.name,
ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE') # :type => detect_content_type(zipfile)
folder = "#{Rails.root}/files" #rescue Errno::ENOENT => e
input_filename = files_path # logger.error "[Errno::ENOENT] ===> #{e}"
zipfile_name = "#{Rails.root}/tmp/archiveZip/archive_#{Time.now.to_i}.zip" # @error = e
end
Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name))
def zip_homework_by_user(homeattach)
homeworks_attach_path = []
# 需要将所有homework.attachments遍历加入zip
# 并且返回zip路径
user_attaches_paths = homeattach.attachments.each do |attach|
#length = attach.storage_path.length
homeworks_attach_path << attach.diskfile#.to_s.slice((length+1)..-1)
end
zipping "#{Time.now.to_i}_#{homeattach.user.name.to_s}.zip", homeworks_attach_path, OUTPUT_FOLDER, true
end
def zipping(zip_name_refer, files_paths, output_path, is_attachment=false)
# 输入待打包的文件列表已经打包文件定位到ouput_path
ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE')
input_filename = files_paths
rename_zipfile = zip_name_refer ||= "archive_#{Time.now.to_i}.zip"
zipfile_name = "#{output_path}/#{rename_zipfile}"
Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name))
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| input_filename.each do |filename|
zipfile.add(ic.iconv(filename_to_real(File.basename(filename))), folder + '/' + filename) rename_file = Time.now.to_i.to_s+ ic.iconv( (File.basename(filename)) ).to_s
rename_file = ic.iconv( filename_to_real( File.basename(filename))).to_s if is_attachment
zipfile.add(rename_file, filename)
end end
zipfile.get_output_stream("ReadMe"){ |os| zipfile.get_output_stream('ReadMe') do |os|
os.write "Homeworks" os.write 'Homeworks'
} end
end end
zipfile_name zipfile_name
rescue Errno=> e rescue Errno => e
logger.error "[zipdown#zipping] ===> #{e}" logger.error "[zipdown#zipping] ===> #{e}"
@error = e @error = e
end end
def detect_content_type(name) #def ziping files_path
content_type = Redmine::MimeType.of(name) # ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE')
content_type.to_s # folder = SaveFolder
end # input_filename = files_path
# zipfile_name = "#{OutputFolder}/archive_#{Time.now.to_i}.zip"
def filename_to_real name #
attach = Attachment.find_by_disk_filename(name) # Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name))
attach.filename # Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
end # input_filename.each do |filename|
# zipfile.add(ic.iconv(filename_to_real(File.basename(filename))), folder + '/' + filename)
# end
# zipfile.get_output_stream("ReadMe") { |os|
# os.write "Homeworks"
# }
# end
# zipfile_name
#rescue Errno => e
# logger.error "[zipdown#zipping] ===> #{e}"
# @error = e
#end
def detect_content_type(name)
content_type = Redmine::MimeType.of(name)
content_type.to_s
end
def filename_to_real(name)
attach = Attachment.find_by_disk_filename(name)
attach.filename
end
end end

@ -17,14 +17,14 @@
</style> </style>
<% @courses.each do |course| %> <% @courses.each do |course| %>
<div class="courses_list"> <div class="courses_list">
<%= course.name %> <%= course.name %>
<% course.homeworks.each do |homework| %> <% course.homeworks.each do |homework| %>
<% homeworks_attach_path = [] %> <% homeworks_attach_path = [] %>
<div class="homeworks"> <div class="homeworks">
<%= link_to homework.name, respond_path(homework) %>(<%=homework.homeworks.count %>)<%#Bid%> <%= link_to homework.name, respond_path(homework) %>(<%=homework.homeworks.count %>)<%#Bid%>
<div class="attach_item"> <div class="attach_item">
<%= link_to "package", test_zip_path(:homework_id => homework.id)%><br/> <%= link_to "package", zipdown_assort_path(obj_class: homework.class, obj_id: homework.id) %><br/>
<% homework.homeworks.each do |homeattach|%><%#homework.class == Bid %> <% homework.homeworks.each do |homeattach|%><%#homework.class == Bid %>
<% homeattach.attachments.each do |attach|%> <% homeattach.attachments.each do |attach|%>
<%= link_to_attachment attach, author: true, :download => true %> (<%=attach.author%>) <%= link_to_attachment attach, author: true, :download => true %> (<%=attach.author%>)

Loading…
Cancel
Save