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

3
.gitignore vendored
View File

@ -84,4 +84,5 @@ redis_data/
Dockerfile
dump.rdb
.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
return render_error("task_id错误") if params[:task_id].blank?
code, data, error = Trace::PdfReportService.call(current_user.trace_token, params[:task_id])
domain = Trace.trace_config[:domain]
base_url = Trace.trace_config[:base_url]
url = "/user/pdfreport?task_id=#{params[:task_id]}"
file_path = [domain, base_url, url].join
request.headers["Authorization"] = current_user.trace_token
redirect_to file_path
result = Trace::PdfReportService.call(current_user.trace_token, params[:task_id])
if result.is_a?(Hash) && result[:code] == 200
redirect_to result[:download_url]
else
render_error("下载报告失败!")
end
rescue Exception => exception
puts exception.message
normal_status(-1, exception.message)

View File

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