修复:解析禅道描述里的图片

This commit is contained in:
yystopf 2024-09-11 16:30:48 +08:00
parent ba1251be28
commit 444ce3764b
1 changed files with 42 additions and 3 deletions

View File

@ -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(/<img\s+[^>]*?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