37 lines
763 B
Ruby
37 lines
763 B
Ruby
# 代码溯源 导出pdf
|
|
require 'open-uri'
|
|
require 'fileutils'
|
|
|
|
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}/#{task_id}.pdf")
|
|
return {code: 200, download_url: "/trace_task_results/#{task_id}.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"
|
|
end
|
|
|
|
def url
|
|
"/user/pdfreport".freeze
|
|
end
|
|
end |