Files
resty_functions/scripts/lib/upyun_remove.lua
T
2024-01-21 03:02:40 +08:00

81 lines
2.5 KiB
Lua

-- 指定模块引用目录,否则无法加载同目录下的其他文件
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
local upyun_remove = {}
local json = require("cjson")
local strip_path = require("lib.strip_path")
local Upyun = require('lib.upyun')
function upyun_remove.remove(filepath, filename, retry)
if retry == nil then
retry = 1
end
if retry > 3 then
ngx.status = 404
local msg = "failed to remove file " .. filepath .. " reach max retries"
ngx.log(ngx.ERR, msg)
ngx.say(msg)
else
local upyun, err = Upyun:new2({
user = ngx.var.upyun_operator,
passwd = ngx.var.upyun_password,
localFilePath = nil
})
if not upyun then
ngx.status = 500
ngx.log(ngx.ERR, "failed to initialize upyun: " .. err)
return
end
local bucket = ngx.var.upyun_bucket
local directory = ngx.var.upyun_directory
if not filename then
--filename = strip_path.strip_path(filepath)
filename = filepath
end
ngx.log(ngx.INFO, 'bucket -> ', bucket)
ngx.log(ngx.INFO, 'directory -> ', directory)
local removePath = bucket .. "/" .. directory .. "/" .. filename
ngx.log(ngx.INFO, " removePath " , removePath)
--local options = {
-- md5 = true
--}
ngx.log(ngx.INFO, ' ========>>>>> ', removePath)
local info, err = upyun:remove_file(removePath)
if not info then
local error_table = json.decode(err)
ngx.log(ngx.ERR, "Upyun Remove File Error: " .. filepath .. " e: " .. err .. " " .. '[' .. error_table["code"] .. ']')
if (error_table["code"] == 40000006)
then
ngx.log(ngx.ERR, "Retry remove file : " .. '[' .. err .. ']')
upyun_remove.remove(filepath, filename, retry+1)
else
ngx.status = 404
local msg = "failed to remove file : " .. filepath .. ' [' .. err .. ']'
ngx.log(ngx.ERR, msg)
ngx.say(msg)
end
else
ngx.status = 200
local fullpath = ngx.var.upyun_domain .. "/" .. directory .. "/" .. filename
ngx.log(ngx.INFO, "SUCCESS REMOVE -> UPYUN URL -> ", fullpath)
ngx.say(fullpath)
end
end
end
return upyun_remove