diff --git a/scripts/form_upload_to_compress.lua b/scripts/form_upload_to_compress.lua index 1f458ed..438eed5 100644 --- a/scripts/form_upload_to_compress.lua +++ b/scripts/form_upload_to_compress.lua @@ -167,10 +167,17 @@ local function read_form_file() handle:close() local upyun_upload = require("lib.upyun_upload") + local ts_compress_audit = require("lib.ts_compress_audit") ngx.log(ngx.INFO, 'UPLOAD COMPRESSED IMAGE -> fullpath -> ', output_file[i]) - upyun_upload.upload(output_file[i], nil) + local public_url = upyun_upload.upload_return_url(output_file[i], nil) + if not public_url then + return + end + ts_compress_audit.create(httpc, authHeader, sudoerToken, public_url) + ngx.status = 200 + ngx.say(public_url) end break diff --git a/scripts/lib/ts_compress_audit.lua b/scripts/lib/ts_compress_audit.lua new file mode 100644 index 0000000..c42c469 --- /dev/null +++ b/scripts/lib/ts_compress_audit.lua @@ -0,0 +1,39 @@ +-- 压缩直传成功后向 Rust TS(3003)登记 NOOP,便于管理端 Tasks 可追溯(非 UPLOAD_PICTURE:后者专指 combine 管线)。 +local cjson = require("cjson") + +local function create(httpc, auth_header, sudoer_token, public_url) + local ts_host = ngx.var.huiwing_htyts_rust or "127.0.0.1:3003" + local remote_url = "http://" .. ts_host .. "/api/v1/ts/create_task" + local hty_host = ngx.req.get_headers()["HtyHost"] or "" + local body_tbl = { + task_type = "NOOP", + payload = { + kind = "IMAGE_FORM_COMPRESS", + url = public_url, + }, + } + local body = cjson.encode(body_tbl) + local res, err = httpc:request_uri(remote_url, { + ssl_verify = false, + method = "POST", + headers = { + ["Content-Type"] = "application/json", + ["Authorization"] = auth_header or "", + ["HtySudoerToken"] = sudoer_token or "", + ["HtyHost"] = hty_host, + }, + body = body, + }) + if not res then + ngx.log(ngx.ERR, "IMAGE_FORM_COMPRESS audit: request failed ", err) + return false + end + if res.status ~= 201 and res.status ~= 200 then + ngx.log(ngx.ERR, "IMAGE_FORM_COMPRESS audit: status=", res.status, " body=", res.body) + return false + end + ngx.log(ngx.INFO, "IMAGE_FORM_COMPRESS audit noop created") + return true +end + +return { create = create } diff --git a/scripts/lib/upyun_upload.lua b/scripts/lib/upyun_upload.lua index 3515665..a49ef0a 100644 --- a/scripts/lib/upyun_upload.lua +++ b/scripts/lib/upyun_upload.lua @@ -6,69 +6,78 @@ local upyun_upload = {} local json = require("cjson") + local strip_path = require("lib.strip_path") local Upyun = require('lib.upyun') -function upyun_upload.upload(filepath, filename, retry) - +--- @return string|nil public_url 成功返回又拍完整 URL;失败返回 nil(已打日志) +function upyun_upload.upload_return_url(filepath, filename, retry) if retry == nil then retry = 1 end if retry > 3 then ngx.log(ngx.ERR, "failed to upload file : reach max retries") - else - local upyun, err = Upyun:new({ - user = ngx.var.upyun_operator, - passwd = ngx.var.upyun_password, - localFilePath = filepath - }) + return nil + end - if not upyun then - ngx.status = 500 - ngx.log(ngx.ERR, "failed to initialize upyun: " .. err) - return - end + local upyun, err = Upyun:new({ + user = ngx.var.upyun_operator, + passwd = ngx.var.upyun_password, + localFilePath = filepath + }) - local bucket = ngx.var.upyun_bucket - local directory = ngx.var.upyun_directory - if not filename then - filename = strip_path.strip_path(filepath) - end + if not upyun then + ngx.status = 500 + ngx.log(ngx.ERR, "failed to initialize upyun: " .. err) + return nil + end - ngx.log(ngx.INFO, 'bucket -> ', bucket) - ngx.log(ngx.INFO, 'directory -> ', directory) + local bucket = ngx.var.upyun_bucket + local directory = ngx.var.upyun_directory + if not filename then + filename = strip_path.strip_path(filepath) + end - local savePath = bucket .. "/" .. directory .. "/" .. filename - ngx.log(ngx.INFO, " savePath " , savePath) + ngx.log(ngx.INFO, 'bucket -> ', bucket) + ngx.log(ngx.INFO, 'directory -> ', directory) - local options = { - md5 = true - } + local savePath = bucket .. "/" .. directory .. "/" .. filename + ngx.log(ngx.INFO, " savePath " , savePath) - local info, err = upyun:upload_file(savePath, nil, options) - if not info then - local error_table = json.decode(err) - ngx.log(ngx.INFO, "Upyun Upload File Error: " .. err) - ngx.log(ngx.ERR, '[' .. error_table["code"] .. ']') + local options = { + md5 = true + } - if (error_table["code"] == 40000006) - then - ngx.status = 400 - ngx.log(ngx.ERR, "Retry upload file : " .. '[' .. err .. ']') - upyun_upload.upload(filepath, filename, retry+1) - else - ngx.status = 400 - ngx.log(ngx.ERR, "failed to upload file : " .. '[' .. err .. ']') - end + local info, upl_err = upyun:upload_file(savePath, nil, options) + if not info then + local error_table = json.decode(upl_err) + ngx.log(ngx.INFO, "Upyun Upload File Error: " .. upl_err) + ngx.log(ngx.ERR, '[' .. error_table["code"] .. ']') + + if (error_table["code"] == 40000006) + then + ngx.status = 400 + ngx.log(ngx.ERR, "Retry upload file : " .. '[' .. upl_err .. ']') + return upyun_upload.upload_return_url(filepath, filename, retry + 1) else - ngx.status = 200 - local fullpath = ngx.var.upyun_domain .. "/" .. directory .. "/" .. filename - ngx.log(ngx.INFO, "SUCCESS UPLOAD -> UPYUN URL -> ", fullpath) - ngx.say(fullpath) + ngx.status = 400 + ngx.log(ngx.ERR, "failed to upload file : " .. '[' .. upl_err .. ']') + return nil end end + ngx.status = 200 + local fullpath = ngx.var.upyun_domain .. "/" .. directory .. "/" .. filename + ngx.log(ngx.INFO, "SUCCESS UPLOAD -> UPYUN URL -> ", fullpath) + return fullpath end -return upyun_upload \ No newline at end of file +function upyun_upload.upload(filepath, filename, retry) + local url = upyun_upload.upload_return_url(filepath, filename, retry) + if url then + ngx.say(url) + end +end + +return upyun_upload