From ecd5a7b2fb30a0d32b17ff91e048c8f14c8acc92 Mon Sep 17 00:00:00 2001 From: sw <939547590@qq.com> Date: Tue, 11 Nov 2014 11:16:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BD=9C=E4=B8=9A=E6=89=93?= =?UTF-8?q?=E5=8C=85=E4=B8=8B=E8=BD=BD=E6=97=A0=E6=B3=95=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/zipdown_controller.rb | 58 +++++++++------------------ 1 file changed, 18 insertions(+), 40 deletions(-) diff --git a/app/controllers/zipdown_controller.rb b/app/controllers/zipdown_controller.rb index 18ed80a34..b2195adc3 100644 --- a/app/controllers/zipdown_controller.rb +++ b/app/controllers/zipdown_controller.rb @@ -19,17 +19,13 @@ class ZipdownController < ApplicationController 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 !!" + if params[:obj_class] == "Bid" + bid = Bid.find params[:obj_id] + zipfile = zip_bid bid + else + logger.error "[ZipDown#assort] ===> #{params[:obj_class]} unKown !!" end - send_file zipfile, :filename => obj.name+".zip", :type => detect_content_type(zipfile) if zipfile + send_file zipfile, :filename => bid.name + ".zip", :type => detect_content_type(zipfile) if zipfile rescue Exception => e render file: 'public/no_file_found.html' @@ -40,61 +36,43 @@ class ZipdownController < ApplicationController homework = HomeworkAttach.find params[:homework] if User.current.admin? || User.current.member_of_course?(homework.bid.courses.first) if homework != nil - if homework.attachments.count > 0 + unless homework.attachments.empty? zipfile = zip_homework_by_user homework send_file zipfile, :filename => homework.name+".zip", :type => detect_content_type(zipfile) if(zipfile) else - render file: 'public/no_file_found.html' , :layout => 'course_base' + render file: 'public/no_file_found.html' end else - render file: 'public/file_not_found.html' , :layout => 'course_base' + render file: 'public/file_not_found.html' end else - render_403 :message => :notice_not_authorized ,:layout => "course_base" + render_403 end rescue => e - render file: 'public/file_not_found.html' , :layout => 'course_base' + render file: 'public/file_not_found.html' end private - def zip_user_bid(bid,user_id) - # Todo: User Access Controll - - homeattaches = bid.homeworks.where("user_id = ?",user_id) - # 得到每一个人所有文件打包的zip文件 - # 并将每一个人的zip打包为一个并返回路径 - user_zip_paths = homeattaches.map do |homeattach| - zip_homework_by_user homeattach - end - user_zip_paths - end - def zip_bid(bid) # Todo: User Access Controll - - homeattaches = bid.homeworks - #记录所有作业是不是有附件,有一个附件就改为true - #has_file = false - # 得到每一个人所有文件打包的zip文件 - # 并将每一个人的zip打包为一个并返回路径 - user_zip_paths = homeattaches.map do |homeattach| - if homeattach.attachments.count > 0 - zip_homework_by_user homeattach - #has_file = true unless has_file + bid_homework_path = [] + bid.homeworks.each do |homeattach| + unless homeattach.attachments.empty? + bid_homework_path << zip_homework_by_user(homeattach) end end - zipping "#{Time.now.to_i}_#{bid.name}.zip", user_zip_paths, OUTPUT_FOLDER + zipping "#{Time.now.to_i}_#{bid.name}.zip", bid_homework_path, OUTPUT_FOLDER end def zip_homework_by_user(homeattach) homeworks_attach_path = [] # 需要将所有homework.attachments遍历加入zip # 并且返回zip路径 - user_attaches_paths = homeattach.attachments.each do |attach| + homeattach.attachments.each do |attach| homeworks_attach_path << attach.diskfile#.to_s.slice((length+1)..-1) end - zipping("#{homeattach.user.name.to_s}_#{Time.now.to_i}.zip", homeworks_attach_path, OUTPUT_FOLDER, true) + zipping("#{Time.now.to_i}.zip", homeworks_attach_path, OUTPUT_FOLDER, true) end