From 5ee1c8e2f2f1a3dd5dfcada1d4b6db226dd99502 Mon Sep 17 00:00:00 2001 From: yanxd Date: Thu, 17 Apr 2014 19:19:42 +0800 Subject: [PATCH] refactory zip --- app/controllers/zipdown_controller.rb | 161 ++++++++++++++++---------- app/views/test/courselist.html.erb | 4 +- 2 files changed, 104 insertions(+), 61 deletions(-) diff --git a/app/controllers/zipdown_controller.rb b/app/controllers/zipdown_controller.rb index 12f1976e0..dd855bc74 100644 --- a/app/controllers/zipdown_controller.rb +++ b/app/controllers/zipdown_controller.rb @@ -1,68 +1,111 @@ class ZipdownController < ApplicationController - def assort - obj_class = params[:obj_class] - obj_id = params[:obj_id] - obj = obj_class.constantize.find(obj_id) - case obj.class.to_s.to_sym - when :Bid - zip obj - else - logger.error "[ZipDown#assort] ===> #{obj.class.to_s.to_sym} unKown !!" - end + SAVE_FOLDER = "#{Rails.root}/files" + OUTPUT_FOLDER = "#{Rails.root}/tmp/archiveZip" - rescue NameError, ActiveRecord::RecordNotFound => e - logger.error "[ZipDown] ===> #{e}" - @error = e - end + def assort + obj_class = params[:obj_class] + obj_id = params[:obj_id] + obj = obj_class.constantize.find(obj_id) + zipfile = nil + case obj.class.to_s.to_sym + when :Bid + zipfile = zip_bid obj + else + logger.error "[ZipDown#assort] ===> #{obj.class.to_s.to_sym} unKown !!" + end + send_file zipfile, :filename => obj.name, :type => detect_content_type(zipfile) if zipfile - private + rescue NameError, ActiveRecord::RecordNotFound => e + logger.error "[ZipDown] ===> #{e}" + @error = e + end - def zip bid - # Todo: User Access Controll - homeworks_attach_path = [] - bid.homeworks.each do |homeattach| - homeattach.attachments.each do |attach| - length = attach.storage_path.length - homeworks_attach_path << attach.diskfile.to_s.slice((length+1)..-1) - end - end - @paths = homeworks_attach_path - zipfile = ziping homeworks_attach_path - send_file zipfile, :filename => bid.name, - :type => detect_content_type(zipfile) - rescue Errno::ENOENT => e - logger.error "[Errno::ENOENT] ===> #{e}" - @error = e - end + private - def ziping files_path - ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE') - folder = "#{Rails.root}/files" - input_filename = files_path - zipfile_name = "#{Rails.root}/tmp/archiveZip/archive_#{Time.now.to_i}.zip" + def zip_bid(bid) + # Todo: User Access Controll - Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name)) - Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| - 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 + homeattaches = bid.homeworks + # 得到每一个人所有文件打包的zip文件 + # 并将每一个人的zip打包为一个并返回路径 + user_zip_paths = homeattaches.map do |homeattach| + zip_homework_by_user homeattach + end + zipping bid.name, user_zip_paths, OUTPUT_FOLDER - def detect_content_type(name) - content_type = Redmine::MimeType.of(name) - content_type.to_s - end + #@paths = homeworks_attach_path + #zipfile = ziping homeworks_attach_path + #send_file zipfile, :filename => bid.name, + # :type => detect_content_type(zipfile) + #rescue Errno::ENOENT => e + # logger.error "[Errno::ENOENT] ===> #{e}" + # @error = e + end + + 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 homeattach.user.name, user_attaches_paths, OUTPUT_FOLDER + end + + + def zipping(zip_name_refer, files_paths, output_path) + # 输入待打包的文件列表,已经打包文件定位到ouput_path + ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE') + #folder = SAVE_FOLDER + input_filename = files_paths + zipfile_name = "#{output_path}/archive_#{Time.now.to_i}.zip" + + Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name)) + + Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| + input_filename.each do |filename| + zipfile.add(ic.iconv(filename_to_real(zip_name_refer+"#{Time.now.to_s}") ), filename) + end + zipfile.get_output_stream('ReadMe') do |os| + os.write 'Homeworks' + end + end + zipfile_name + rescue Errno => e + logger.error "[zipdown#zipping] ===> #{e}" + @error = e + end + + #def ziping files_path + # ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE') + # folder = SaveFolder + # input_filename = files_path + # zipfile_name = "#{OutputFolder}/archive_#{Time.now.to_i}.zip" + # + # Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name)) + # Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| + # 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 - def filename_to_real name - attach = Attachment.find_by_disk_filename(name) - attach.filename - end end diff --git a/app/views/test/courselist.html.erb b/app/views/test/courselist.html.erb index 61cd69e13..959d6a4be 100644 --- a/app/views/test/courselist.html.erb +++ b/app/views/test/courselist.html.erb @@ -17,14 +17,14 @@ <% @courses.each do |course| %>
- <%= course.name %> + <%= course.name %> <% course.homeworks.each do |homework| %> <% homeworks_attach_path = [] %>
<%= link_to homework.name, respond_path(homework) %>(<%=homework.homeworks.count %>)<%#Bid%>
- <%= link_to "package", test_zip_path(:homework_id => homework.id)%>
+ <%= link_to "package", zipdown_assort_path(obj_class: homework.class, obj_id: homework.id) %>
<% homework.homeworks.each do |homeattach|%><%#homework.class == Bid %> <% homeattach.attachments.each do |attach|%> <%= link_to_attachment attach, author: true, :download => true %> (<%=attach.author%>)