修复:处理禅道图片函数
This commit is contained in:
parent
4fbbe88ca9
commit
0d1ca7bc49
|
@ -76,6 +76,42 @@ class Attachment < ApplicationRecord
|
||||||
|
|
||||||
DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z)
|
DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z)
|
||||||
|
|
||||||
|
def self.build_from_remote_url(user, name, url, container=nil)
|
||||||
|
ext = name.split('.')[-1]
|
||||||
|
tmp_path = "#{Rails.root}/#{name}"
|
||||||
|
uri = URI(url)
|
||||||
|
size = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
||||||
|
response = http.get(uri.path)
|
||||||
|
File.open(tmp_path, 'wb') do |file|
|
||||||
|
file.write(response.body)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
digest = "#{Digest::MD5.file(tmp_path).hexdigest}_#{(Time.now.to_f * 1000).to_i}.#{ext}"
|
||||||
|
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
|
||||||
|
path = File.join(save_path, digest)
|
||||||
|
FileUtils.mv(tmp_path, path)
|
||||||
|
attachment = Attachment.new
|
||||||
|
attachment.filename = name
|
||||||
|
attachment.disk_filename = path[save_path.size+1, path.size]
|
||||||
|
attachment.filesize = size
|
||||||
|
attachment.content_type = 'application/octet-stream'
|
||||||
|
attachment.digest = digest.split('.')[0]
|
||||||
|
attachment.author_id = user.id
|
||||||
|
attachment.disk_directory = month_folder
|
||||||
|
attachment.cloud_url = url
|
||||||
|
attachment.uuid = SecureRandom.uuid
|
||||||
|
attachment.container = container
|
||||||
|
attachment.save!
|
||||||
|
|
||||||
|
return attachment
|
||||||
|
rescue
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
def diskfile
|
def diskfile
|
||||||
File.join(File.join(Rails.root, "files"), disk_directory.to_s, disk_filename.to_s)
|
File.join(File.join(Rails.root, "files"), disk_directory.to_s, disk_filename.to_s)
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,38 +23,23 @@ namespace :import_from_chandao do
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_chandao_json(api_url)
|
||||||
|
url = URI("#{EduSetting.get("chandao_server_url")}#{api_url}")
|
||||||
|
http = Net::HTTP.new(url.host, url.port);
|
||||||
|
request = Net::HTTP::Post.new(url)
|
||||||
|
request["Cookie"] = "zentaosid=#{EduSetting.get("chandao_sid")}"
|
||||||
|
response = http.request(request)
|
||||||
|
response.read_body
|
||||||
|
return JSON.parse(JSON.parse(response.read_body)['data'])
|
||||||
|
rescue
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
|
||||||
def trans_content_img(content, user)
|
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 = 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|
|
respace_content_arr.each do |img|
|
||||||
remote_image_url = "#{EduSetting.get("chandao_server_url")}/file-read-#{img[1]}.json"
|
remote_image_url = "#{EduSetting.get("chandao_server_url")}/file-read-#{img[1]}.json"
|
||||||
tmp_local_image_path = "#{Rails.root}/#{img[1..2].join(".")}"
|
attachment = Attachment.build_from_remote_url(user, img[1..2].join("."), remote_image_url)
|
||||||
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}"
|
att_url = "#{Rails.application.config_for(:configuration)['platform_url']}/api/attachments/#{attachment.uuid}"
|
||||||
content.gsub!(img[0], "")
|
content.gsub!(img[0], "")
|
||||||
|
@ -89,7 +74,7 @@ namespace :import_from_chandao do
|
||||||
issue.project_issues_index = randd_field_hash['Bug编号'].to_i
|
issue.project_issues_index = randd_field_hash['Bug编号'].to_i
|
||||||
issue.status_id = trans_status(randd_field_hash['Bug状态']) || IssueStatus.first.id
|
issue.status_id = trans_status(randd_field_hash['Bug状态']) || IssueStatus.first.id
|
||||||
issue.tracker_id = Tracker.first.id
|
issue.tracker_id = Tracker.first.id
|
||||||
issue.priority_id = randd_field_hash['优先级'].to_i
|
issue.priority_id = randd_field_hash['优先级'].split('P')[-1].to_i rescue 1
|
||||||
issue.subject = randd_field_hash['Bug标题']
|
issue.subject = randd_field_hash['Bug标题']
|
||||||
issue.description = trans_content_img(randd_field_hash['重现步骤'].to_s, author)
|
issue.description = trans_content_img(randd_field_hash['重现步骤'].to_s, author)
|
||||||
issue.created_on = randd_field_hash['创建日期'].to_time rescue nil
|
issue.created_on = randd_field_hash['创建日期'].to_time rescue nil
|
||||||
|
|
Loading…
Reference in New Issue