From a45038a1e7398207a640507d031bd8a11d424343 Mon Sep 17 00:00:00 2001 From: jingquan huang Date: Tue, 2 Jul 2019 15:03:30 +0800 Subject: [PATCH] =?UTF-8?q?rake=20=E4=BB=BB=E5=8A=A1=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/homework_git_file.rake | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lib/tasks/homework_git_file.rake diff --git a/lib/tasks/homework_git_file.rake b/lib/tasks/homework_git_file.rake new file mode 100644 index 00000000..f8a5253a --- /dev/null +++ b/lib/tasks/homework_git_file.rake @@ -0,0 +1,43 @@ +namespace :homework do + def tran_base64_decode64 str + if str.blank? + str + else + s_size = str.size % 4 + if s_size != 0 + str += "=" * (4 - s_size) + end + Base64.decode64(str.tr("-_", "+/")).force_encoding("utf-8") + end + end + + task :gitfiledown => :environment do + begin + puts ENV['args'] + system("tmp/homework_file") if File.exist?("tmp/homework_file") + homework_common = HomeworkCommon.find(ENV['args']) + student_works = homework_common.student_works + student_works.each do |sw| + username = sw.user.try(:show_real_name) + myshixun = sw.try(:myshixun) + puts myshixun.try(:gpid) + if myshixun.try(:gpid) + content = g.files(myshixun.gpid, "PL/PL.cpp", "master").try(:content) + tran_content = tran_base64_decode64(content) + path = "tmp/homework_file/#{username}" + FileUtils.mkdir_p(path) unless File.exist?(path) + system("cd #{path}; touch PL.cpp") + aFile = File.new("#{path}/PL.cpp", "r+") + if aFile + aFile.syswrite(tran_content) + else + puts "Unable to open file!" + end + end + end + rescue Exception => e + Rails.logger.error(e.message) + end + end +end +