实训报告的导出

dev_static
cxt 5 years ago
parent e266d3a46a
commit 92181e916c

@ -0,0 +1,50 @@
# 执行示例 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
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)
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
def filename_for_content_disposition(name)
request.env['HTTP_USER_AGENT'] =~ %r{MSIE|Trident|Edge} ? ERB::Util.url_encode(name) : name
end
end
Loading…
Cancel
Save