add: download pdf from trace server

This commit is contained in:
yystopf 2022-05-12 17:52:19 +08:00
parent 4dccdfde97
commit 3d9fe5bba5
3 changed files with 25 additions and 14 deletions

1
.gitignore vendored
View File

@ -85,3 +85,4 @@ Dockerfile
dump.rdb dump.rdb
.tags* .tags*
ceshi_user.xlsx ceshi_user.xlsx
public/trace_task_results

View File

@ -56,13 +56,12 @@ class Traces::ProjectsController < Traces::BaseController
def task_pdf def task_pdf
return render_error("task_id错误") if params[:task_id].blank? return render_error("task_id错误") if params[:task_id].blank?
code, data, error = Trace::PdfReportService.call(current_user.trace_token, params[:task_id]) result = Trace::PdfReportService.call(current_user.trace_token, params[:task_id])
domain = Trace.trace_config[:domain] if result.is_a?(Hash) && result[:code] == 200
base_url = Trace.trace_config[:base_url] redirect_to result[:download_url]
url = "/user/pdfreport?task_id=#{params[:task_id]}" else
file_path = [domain, base_url, url].join render_error("下载报告失败!")
request.headers["Authorization"] = current_user.trace_token end
redirect_to file_path
rescue Exception => exception rescue Exception => exception
puts exception.message puts exception.message
normal_status(-1, exception.message) normal_status(-1, exception.message)

View File

@ -1,4 +1,7 @@
# 代码溯源 导出pdf # 代码溯源 导出pdf
require 'open-uri'
require 'fileutils'
class Trace::PdfReportService < Trace::ClientService class Trace::PdfReportService < Trace::ClientService
attr_accessor :token, :task_id attr_accessor :token, :task_id
@ -9,15 +12,23 @@ class Trace::PdfReportService < Trace::ClientService
end end
def call def call
result = authed_get(token, url, request_params) content = URI.open("#{domain}#{base_url}#{url}?task_id#{task_id}", "Authorization" => token)
response = render_response(result) if content.is_a?(Tempfile)
check_file_path
IO.copy_stream(content, "#{save_path}/#{task_id}.zip")
return {code: 200, download_url: "/trace_task_results/#{task_id}.zip"}
else
return {code: 404}
end
end end
private private
def request_params def check_file_path
{ FileUtils.mkdir_p save_path
task_id: task_id end
}
def save_path
"public/trace_task_results"
end end
def url def url