From 444ce3764b686ff3b34792e81eac38440fc97801 Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 11 Sep 2024 16:30:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E7=A6=85=E9=81=93=E6=8F=8F=E8=BF=B0=E9=87=8C=E7=9A=84=E5=9B=BE?= =?UTF-8?q?=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/import_issues_from_chandao.rake | 45 +++++++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/tasks/import_issues_from_chandao.rake b/lib/tasks/import_issues_from_chandao.rake index b4b799fc0..d53c10baf 100644 --- a/lib/tasks/import_issues_from_chandao.rake +++ b/lib/tasks/import_issues_from_chandao.rake @@ -23,6 +23,45 @@ namespace :import_from_chandao do return nil end + def trans_content_img(content, user) + respace_content_arr = content.to_s.scan(/]*?src=[“.*?“][^>]*?\/?>/).map{|s|[s,s.match(/(\d+\.\w+)/)[0].split(".")[0],s.match(/(\d+\.\w+)/)[0].split(".")[1]]} + respace_content_arr.each do |img| + remote_image_url = "#{EduSetting.get("chandao_server_url")}/file-read-#{img[1]}.json" + tmp_local_image_path = "#{Rails.root}/#{img[1..2].join(".")}" + uri = URI(remote_image_url) + size = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| + response = http.get(uri.path) + File.open(tmp_local_image_path, 'wb') do |file| + file.write(response.body) + end + end + digest = "#{Digest::MD5.file(tmp_local_image_path).hexdigest}_#{(Time.now.to_f * 1000).to_i}.#{img[2]}" + month_folder = "#{Time.now.year}/#{Time.now.month.to_s.rjust(2, '0')}" + save_path = "#{Rails.root}#{EduSetting.get("attachment_folder")}#{month_folder}" + unless Dir.exists?(save_path) + FileUtils.mkdir_p(save_path) ##不成功这里会抛异常 + end + + local_image_path = File.join(save_path, digest) + FileUtils.mv(tmp_local_image_path, local_image_path) + attachment = Attachment.new + attachment.filename = img[1..2].join(".") + attachment.disk_filename = local_image_path[save_path.size+1, local_image_path.size] + attachment.filesize = size + attachment.content_type = img[2] + attachment.digest = digest + attachment.author_id = user.id + attachment.disk_directory = month_folder + attachment.cloud_url = remote_image_url + attachment.uuid = SecureRandom.uuid + attachment.save + + att_url = "#{Rails.application.config_for(:configuration)['platform_url']}/api/attachments/#{attachment.uuid}" + content.gsub!(img[0], "![](#{att_url})") + end + return content + end + desc "bug数据" # 执行示例 bundle exec rake "import_from_chandao:bugs[企业内部工时管理系统.csv, 3, ceshi_org]" # RAILS_ENV=production bundle exec rake "import_from_chandao:bugs[企业内部工时管理系统.csv, 3, ceshi_org]" @@ -52,7 +91,7 @@ namespace :import_from_chandao do issue.tracker_id = Tracker.first.id issue.priority_id = randd_field_hash['优先级'].to_i issue.subject = randd_field_hash['Bug标题'] - issue.description = randd_field_hash['重现步骤'] + issue.description = trans_content_img(randd_field_hash['重现步骤'].to_s, author) issue.created_on = randd_field_hash['创建日期'].to_time rescue nil issue.updated_on = randd_field_hash['修改日期'].to_time rescue issue.created_on issue.due_date = randd_field_hash['截止日期'].to_time rescue nil @@ -107,7 +146,7 @@ namespace :import_from_chandao do issue.tracker_id = Tracker.first.id issue.priority_id = randd_field_hash['优先级'].to_i issue.subject = randd_field_hash['任务名称'] - issue.description = randd_field_hash['任务描述'] + issue.description = trans_content_img(randd_field_hash['任务描述'].to_s, author) issue.created_on = randd_field_hash['创建日期'].to_time rescue nil issue.updated_on = randd_field_hash['最后修改日期'].to_time rescue issue.created_on issue.time_scale = randd_field_hash['最初预计'].to_i @@ -158,7 +197,7 @@ namespace :import_from_chandao do issue.tracker_id = Tracker.first.id issue.priority_id = randd_field_hash['优先级'].to_i issue.subject = randd_field_hash['需求名称'] - issue.description = randd_field_hash['需求描述'] + issue.description = trans_content_img(randd_field_hash['需求描述'].to_s, author) issue.created_on = randd_field_hash['创建日期'].to_time issue.updated_on = randd_field_hash['最后修改日期'].to_time rescue randd_field_hash['创建日期'].to_time issue.time_scale = randd_field_hash['预计工时'].to_i