From 92181e916c560d7963a7d432741bf1d2a9977b20 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Mon, 6 Jan 2020 11:54:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=9E=E8=AE=AD=E6=8A=A5=E5=91=8A?= =?UTF-8?q?=E7=9A=84=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/zip_pack.rake | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 lib/tasks/zip_pack.rake diff --git a/lib/tasks/zip_pack.rake b/lib/tasks/zip_pack.rake new file mode 100644 index 000000000..7eb757f08 --- /dev/null +++ b/lib/tasks/zip_pack.rake @@ -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 \ No newline at end of file From 445e7daf5f338057da429d8d68195b8478b38a7e Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Mon, 6 Jan 2020 13:48:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=89=B9=E9=87=8F=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E5=AE=9E=E8=AE=AD=E6=8A=A5=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/export_shixun_report_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/export_shixun_report_service.rb b/app/services/export_shixun_report_service.rb index 4263ef4f1..b75f031c0 100644 --- a/app/services/export_shixun_report_service.rb +++ b/app/services/export_shixun_report_service.rb @@ -10,7 +10,7 @@ class ExportShixunReportService end def filename - @_filename ||= "#{work.user&.student_id}_#{work.user.real_name}_#{homework.name}_#{Time.now.strftime('%Y%m%d_%H%M%S')}.pdf".gsub(' ', '-').gsub('/', '_') + @_filename ||= "#{homework.course&.user_group_name(work.user_id)}_#{work.user&.student_id}_#{work.user.real_name}_#{homework.name}_#{Time.now.strftime('%Y%m%d_%H%M%S')}.pdf".gsub(' ', '-').gsub('/', '_') end def prepare_binding