2014-09-15 10:58:14 +08:00
|
|
|
|
require 'zip'
|
|
|
|
|
class ZipdownController < ApplicationController
|
|
|
|
|
#查找项目(课程)
|
|
|
|
|
before_filter :find_project_by_bid_id, :only => [:assort]
|
|
|
|
|
#检查权限
|
|
|
|
|
#勿删 before_filter :authorize, :only => [:assort,:download_user_homework]
|
|
|
|
|
SAVE_FOLDER = "#{Rails.root}/files"
|
|
|
|
|
OUTPUT_FOLDER = "#{Rails.root}/tmp/archiveZip"
|
|
|
|
|
|
|
|
|
|
#通过作业Id找到项目(课程)
|
|
|
|
|
def find_project_by_bid_id
|
|
|
|
|
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
|
|
|
|
|
@project = obj.courses[0]
|
|
|
|
|
end
|
|
|
|
|
end
|
2014-10-11 14:51:28 +08:00
|
|
|
|
|
2014-09-15 10:58:14 +08:00
|
|
|
|
def assort
|
2014-11-11 11:16:37 +08:00
|
|
|
|
if params[:obj_class] == "Bid"
|
|
|
|
|
bid = Bid.find params[:obj_id]
|
2014-11-24 15:56:23 +08:00
|
|
|
|
file_count = 0
|
|
|
|
|
bid.homeworks.map { |homework| file_count += homework.attachments.count}
|
|
|
|
|
if file_count > 0
|
|
|
|
|
zipfile = zip_bid bid
|
|
|
|
|
else
|
|
|
|
|
render file: 'public/no_file_found.html'
|
|
|
|
|
end
|
2014-11-11 11:16:37 +08:00
|
|
|
|
else
|
|
|
|
|
logger.error "[ZipDown#assort] ===> #{params[:obj_class]} unKown !!"
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
2014-11-11 11:16:37 +08:00
|
|
|
|
send_file zipfile, :filename => bid.name + ".zip", :type => detect_content_type(zipfile) if zipfile
|
2014-09-15 10:58:14 +08:00
|
|
|
|
|
2014-10-11 16:00:37 +08:00
|
|
|
|
rescue Exception => e
|
2014-11-10 10:21:16 +08:00
|
|
|
|
render file: 'public/no_file_found.html'
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#下载某一学生的作业的所有文件
|
|
|
|
|
def download_user_homework
|
|
|
|
|
homework = HomeworkAttach.find params[:homework]
|
|
|
|
|
if User.current.admin? || User.current.member_of_course?(homework.bid.courses.first)
|
|
|
|
|
if homework != nil
|
2014-11-11 11:16:37 +08:00
|
|
|
|
unless homework.attachments.empty?
|
2014-09-15 10:58:14 +08:00
|
|
|
|
zipfile = zip_homework_by_user homework
|
2014-12-12 09:46:12 +08:00
|
|
|
|
send_file zipfile, :filename => ((homework.user.user_extensions.nil? || homework.user.user_extensions.student_id.nil?) ? "" : homework.user.user_extensions.student_id) +
|
2014-12-10 17:04:22 +08:00
|
|
|
|
"_" + (homework.user.lastname.nil? ? "" : homework.user.lastname) + (homework.user.firstname.nil? ? "" : homework.user.firstname) +
|
|
|
|
|
"_" + homework.name + ".zip", :type => detect_content_type(zipfile) if(zipfile)
|
2014-09-15 10:58:14 +08:00
|
|
|
|
else
|
2014-11-11 11:16:37 +08:00
|
|
|
|
render file: 'public/no_file_found.html'
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
|
|
|
|
else
|
2014-11-11 11:16:37 +08:00
|
|
|
|
render file: 'public/file_not_found.html'
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
|
|
|
|
else
|
2014-11-11 11:16:37 +08:00
|
|
|
|
render_403
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
|
|
|
|
rescue => e
|
2014-11-11 11:16:37 +08:00
|
|
|
|
render file: 'public/file_not_found.html'
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def zip_bid(bid)
|
|
|
|
|
# Todo: User Access Controll
|
2014-11-11 11:16:37 +08:00
|
|
|
|
bid_homework_path = []
|
|
|
|
|
bid.homeworks.each do |homeattach|
|
|
|
|
|
unless homeattach.attachments.empty?
|
|
|
|
|
bid_homework_path << zip_homework_by_user(homeattach)
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
2014-11-11 11:16:37 +08:00
|
|
|
|
zipping "#{Time.now.to_i}_#{bid.name}.zip", bid_homework_path, OUTPUT_FOLDER
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def zip_homework_by_user(homeattach)
|
2014-10-30 09:33:30 +08:00
|
|
|
|
homeworks_attach_path = []
|
2015-01-09 15:59:42 +08:00
|
|
|
|
not_exist_file = []
|
2014-10-30 09:33:30 +08:00
|
|
|
|
# 需要将所有homework.attachments遍历加入zip
|
|
|
|
|
# 并且返回zip路径
|
2014-11-11 11:16:37 +08:00
|
|
|
|
homeattach.attachments.each do |attach|
|
2015-01-09 15:59:42 +08:00
|
|
|
|
if File.exist?(attach.diskfile)
|
|
|
|
|
homeworks_attach_path << attach.diskfile
|
|
|
|
|
else
|
|
|
|
|
not_exist_file << attach.filename
|
|
|
|
|
end
|
2014-10-30 09:33:30 +08:00
|
|
|
|
end
|
2015-01-09 15:59:42 +08:00
|
|
|
|
zipping("#{homeattach.user.lastname}#{homeattach.user.firstname}_#{((homeattach.user.user_extensions.nil? || homeattach.user.user_extensions.student_id.nil?) ? "" : homeattach.user.user_extensions.student_id)}_#{Time.now.to_i.to_s}.zip", homeworks_attach_path, OUTPUT_FOLDER, true, not_exist_file)
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
2015-01-09 15:59:42 +08:00
|
|
|
|
def zipping(zip_name_refer, files_paths, output_path, is_attachment=false, not_exist_file=[])
|
2014-09-15 10:58:14 +08:00
|
|
|
|
# 输入待打包的文件列表,已经打包文件定位到ouput_path
|
|
|
|
|
ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE')
|
|
|
|
|
input_filename = files_paths
|
|
|
|
|
|
2014-11-11 14:41:07 +08:00
|
|
|
|
rename_zipfile = zip_name_refer ||= "#{Time.now.to_i.to_s}.zip"
|
2014-09-15 10:58:14 +08:00
|
|
|
|
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|
|
2014-10-30 09:33:30 +08:00
|
|
|
|
input_filename.each do |filename|
|
2014-11-11 11:39:57 +08:00
|
|
|
|
rename_file = ic.iconv( (File.basename(filename)) ).to_s
|
2014-10-30 09:33:30 +08:00
|
|
|
|
rename_file = ic.iconv( filename_to_real( File.basename(filename))).to_s if is_attachment
|
2014-09-15 10:58:14 +08:00
|
|
|
|
|
2014-10-30 09:33:30 +08:00
|
|
|
|
zipfile.add(rename_file, filename)
|
|
|
|
|
end
|
2015-01-09 15:59:42 +08:00
|
|
|
|
unless not_exist_file.empty?
|
|
|
|
|
zipfile.get_output_stream('FILE_LOST.txt') do |os|
|
|
|
|
|
os.write l(:label_file_lost) + not_exist_file.join(',').to_s
|
|
|
|
|
end
|
|
|
|
|
end
|
2014-09-15 10:58:14 +08:00
|
|
|
|
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
|
2015-01-09 15:59:42 +08:00
|
|
|
|
end
|