feat(ngx): NOOP IMAGE_FORM_COMPRESS audit after form_upload_to_compress

Made-with: Cursor
This commit is contained in:
2026-04-28 09:52:25 +08:00
parent e053cab89e
commit bb95a39dfa
3 changed files with 100 additions and 45 deletions
+53 -44
View File
@@ -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
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