forked from Gitlink/forgeplus
46 lines
1.1 KiB
Ruby
46 lines
1.1 KiB
Ruby
# 代码溯源 导出pdf
|
|
require 'open-uri'
|
|
require 'fileutils'
|
|
require 'zip'
|
|
|
|
class Trace::PdfReportService < Trace::ClientService
|
|
|
|
attr_accessor :token, :task_id
|
|
|
|
def initialize(token, task_id)
|
|
@token = token
|
|
@task_id = task_id
|
|
end
|
|
|
|
def call
|
|
content = open("#{domain}#{base_url}#{url}?task_id=#{task_id}", "Authorization" => token)
|
|
if content.is_a?(Tempfile)
|
|
check_file_path
|
|
IO.copy_stream(content, "#{save_path}/report.zip")
|
|
Zip::File.open("#{save_path}/report.zip") do |zip_file|
|
|
zip_file.each do |f|
|
|
name = f.name.force_encoding('utf-8')
|
|
next unless name == '/report.pdf'
|
|
fpath = File.join(save_path, name)
|
|
zip_file.extract(f, fpath)
|
|
end
|
|
end
|
|
return {code: 200, download_url: "/trace_task_results/#{task_id}/report.pdf"}
|
|
else
|
|
return {code: 404}
|
|
end
|
|
end
|
|
|
|
private
|
|
def check_file_path
|
|
FileUtils.mkdir_p save_path
|
|
end
|
|
|
|
def save_path
|
|
"public/trace_task_results/#{task_id}"
|
|
end
|
|
|
|
def url
|
|
"/user/pdfreport".freeze
|
|
end
|
|
end |