|
|
|
# 执行示例 bundle exec rake zip_pack:shixun_pack args=123,2323
|
|
|
|
namespace :zip_pack do
|
|
|
|
desc "手工打包作品"
|
|
|
|
OUTPUT_FOLDER = "#{Rails.root}/files/archiveZip"
|
|
|
|
|
|
|
|
task :shixun_pack => :environment do
|
|
|
|
|
|
|
|
if ENV['args']
|
|
|
|
homework_ids = ENV['args'].split(",").map(&:to_i)
|
|
|
|
homeworks = HomeworkCommon.where(id: homework_ids)
|
|
|
|
homeworks.includes(:score_student_works).each do |homework|
|
|
|
|
out_file_name = "#{Time.now.strftime("%Y%m%d%H%M%S").to_s}-#{homework.course_id}-#{homework.name}.zip"
|
|
|
|
out_file_name.gsub!(" ", "-")
|
|
|
|
out_file_name.gsub!("/", "_")
|
|
|
|
|
|
|
|
zipfile_name = "#{OUTPUT_FOLDER}/#{out_file_name}"
|
|
|
|
Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name))
|
|
|
|
|
|
|
|
student_works = homework.score_student_works
|
|
|
|
|
|
|
|
if student_works.size > 0
|
|
|
|
pdfs = []
|
|
|
|
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zip|
|
|
|
|
student_works.find_each.map do |student_work|
|
|
|
|
export = ExportShixunReportService.new(homework, student_work)
|
|
|
|
pdf = export.to_pdf
|
|
|
|
pdfs << pdf
|
|
|
|
begin
|
|
|
|
zip.add(export.filename, pdf.path)
|
|
|
|
puts "out: #{export.filename}_#{pdf.path}"
|
|
|
|
rescue => ex
|
|
|
|
Rails.logger.error(ex.message)
|
|
|
|
|
|
|
|
zip.get_output_stream('FILE_NOTICE.txt'){|os| os.write("文件重复:#{export.filename}") }
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
zipfile = zipfile_name
|
|
|
|
else
|
|
|
|
zipfile = {:message => "no file"}
|
|
|
|
end
|
|
|
|
puts "out: #{zipfile}"
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
task :homework_attach_pack => :environment do
|
|
|
|
include ExportHelper
|
|
|
|
if ENV['args']
|
|
|
|
homework_id = ENV['args']
|
|
|
|
homework = HomeworkCommon.find homework_id
|
|
|
|
zip_works = homework.student_works.where("work_status > 0")
|
|
|
|
if zip_works.size > 0
|
|
|
|
zipfile = zip_homework_common homework, zip_works
|
|
|
|
else
|
|
|
|
zipfile = {:message => "no file"}
|
|
|
|
end
|
|
|
|
puts "out: #{zipfile}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def filename_for_content_disposition(name)
|
|
|
|
request.env['HTTP_USER_AGENT'] =~ %r{MSIE|Trident|Edge} ? ERB::Util.url_encode(name) : name
|
|
|
|
end
|
|
|
|
end
|