2022-02-08 00:25:20 +08:00
|
|
|
-- 指定模块引用目录,否则无法加载同目录下的其他文件
|
2022-02-08 13:57:15 +08:00
|
|
|
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
|
2022-02-08 14:14:57 +08:00
|
|
|
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
|
2022-02-08 00:25:20 +08:00
|
|
|
|
|
|
|
|
local upyun_upload = {}
|
|
|
|
|
|
2023-07-23 23:09:13 +08:00
|
|
|
local json = require("cjson")
|
|
|
|
|
|
2026-04-28 09:52:25 +08:00
|
|
|
|
2023-05-08 00:45:53 +08:00
|
|
|
local strip_path = require("lib.strip_path")
|
|
|
|
|
local Upyun = require('lib.upyun')
|
2022-02-08 00:25:20 +08:00
|
|
|
|
2026-04-28 09:52:25 +08:00
|
|
|
--- @return string|nil public_url 成功返回又拍完整 URL;失败返回 nil(已打日志)
|
|
|
|
|
function upyun_upload.upload_return_url(filepath, filename, retry)
|
2023-08-09 19:15:18 +08:00
|
|
|
if retry == nil then
|
|
|
|
|
retry = 1
|
|
|
|
|
end
|
|
|
|
|
|
2023-07-23 23:09:13 +08:00
|
|
|
if retry > 3 then
|
|
|
|
|
ngx.log(ngx.ERR, "failed to upload file : reach max retries")
|
2026-04-28 09:52:25 +08:00
|
|
|
return nil
|
|
|
|
|
end
|
2023-01-17 19:39:08 +08:00
|
|
|
|
2026-04-28 09:52:25 +08:00
|
|
|
local upyun, err = Upyun:new({
|
|
|
|
|
user = ngx.var.upyun_operator,
|
|
|
|
|
passwd = ngx.var.upyun_password,
|
|
|
|
|
localFilePath = filepath
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if not upyun then
|
|
|
|
|
ngx.status = 500
|
|
|
|
|
ngx.log(ngx.ERR, "failed to initialize upyun: " .. err)
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local bucket = ngx.var.upyun_bucket
|
|
|
|
|
local directory = ngx.var.upyun_directory
|
|
|
|
|
if not filename then
|
|
|
|
|
filename = strip_path.strip_path(filepath)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
ngx.log(ngx.INFO, 'bucket -> ', bucket)
|
|
|
|
|
ngx.log(ngx.INFO, 'directory -> ', directory)
|
|
|
|
|
|
|
|
|
|
local savePath = bucket .. "/" .. directory .. "/" .. filename
|
|
|
|
|
ngx.log(ngx.INFO, " savePath " , savePath)
|
|
|
|
|
|
|
|
|
|
local options = {
|
|
|
|
|
md5 = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"] .. ']')
|
2022-02-08 00:25:20 +08:00
|
|
|
|
2026-04-28 09:52:25 +08:00
|
|
|
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)
|
2023-07-23 23:09:13 +08:00
|
|
|
else
|
2026-04-28 09:52:25 +08:00
|
|
|
ngx.status = 400
|
|
|
|
|
ngx.log(ngx.ERR, "failed to upload file : " .. '[' .. upl_err .. ']')
|
|
|
|
|
return nil
|
2023-07-23 23:09:13 +08:00
|
|
|
end
|
2022-02-08 00:25:20 +08:00
|
|
|
end
|
2023-07-23 23:09:13 +08:00
|
|
|
|
2026-04-28 09:52:25 +08:00
|
|
|
ngx.status = 200
|
|
|
|
|
local fullpath = ngx.var.upyun_domain .. "/" .. directory .. "/" .. filename
|
|
|
|
|
ngx.log(ngx.INFO, "SUCCESS UPLOAD -> UPYUN URL -> ", fullpath)
|
|
|
|
|
return fullpath
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function upyun_upload.upload(filepath, filename, retry)
|
|
|
|
|
local url = upyun_upload.upload_return_url(filepath, filename, retry)
|
|
|
|
|
if url then
|
|
|
|
|
ngx.say(url)
|
|
|
|
|
end
|
2022-02-08 00:25:20 +08:00
|
|
|
end
|
|
|
|
|
|
2026-04-28 09:52:25 +08:00
|
|
|
return upyun_upload
|