Add upyun remove
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
-- 指定模块引用目录,否则无法加载同目录下的其他文件
|
||||
--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.log(ngx.ERR, "failed to remove file : reach max retries")
|
||||
else
|
||||
|
||||
-- 使用 os.tmpname 获取临时文件名
|
||||
local tempFileName = "upyun_remove_temp_file"
|
||||
-- 拼接路径
|
||||
local tempFilePath = "/tmp/" .. tempFileName
|
||||
-- 使用 io.open 创建临时文件
|
||||
local tempFile = io.open(tempFilePath, "w")
|
||||
-- 写入内容到临时文件
|
||||
tempFile:write("upyun new config local temp file")
|
||||
-- 关闭文件
|
||||
tempFile:close()
|
||||
-- 使用 os.execute 执行 chmod 命令
|
||||
os.execute("chmod 777 " .. tempFilePath)
|
||||
--print("Upyun new config local temp file path : ", tempFilePath)
|
||||
|
||||
|
||||
local upyun, err = Upyun:new({
|
||||
user = ngx.var.upyun_operator,
|
||||
passwd = ngx.var.upyun_password,
|
||||
--localFilePath = filepath -- TODO : Add a fake file for create new upyun entry
|
||||
--localFilePath = "/Users/liyong/Code/AlchemyStudio/resty_functions/test_files_dir"
|
||||
localFilePath = tempFilePath
|
||||
})
|
||||
|
||||
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)
|
||||
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.INFO, "Upyun Remove File Error: " .. err)
|
||||
ngx.log(ngx.ERR, '[' .. error_table["code"] .. ']')
|
||||
|
||||
if (error_table["code"] == 40000006)
|
||||
then
|
||||
ngx.status = 400
|
||||
ngx.log(ngx.ERR, "Retry remove file : " .. '[' .. err .. ']')
|
||||
upyun_remove.remove(filepath, filename, retry+1)
|
||||
else
|
||||
ngx.status = 400
|
||||
ngx.log(ngx.ERR, "failed to remove file : " .. '[' .. err .. ']')
|
||||
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
|
||||
Reference in New Issue
Block a user