add upyun upload rety
This commit is contained in:
@@ -4,10 +4,16 @@ package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
|
|||||||
|
|
||||||
local upyun_upload = {}
|
local upyun_upload = {}
|
||||||
|
|
||||||
|
local json = require("cjson")
|
||||||
|
|
||||||
local strip_path = require("lib.strip_path")
|
local strip_path = require("lib.strip_path")
|
||||||
local Upyun = require('lib.upyun')
|
local Upyun = require('lib.upyun')
|
||||||
|
|
||||||
function upyun_upload.upload(filepath, filename)
|
function upyun_upload.upload(filepath, filename, retry)
|
||||||
|
|
||||||
|
if retry > 3 then
|
||||||
|
ngx.log(ngx.ERR, "failed to upload file : reach max retries")
|
||||||
|
else
|
||||||
local upyun, err = Upyun:new({
|
local upyun, err = Upyun:new({
|
||||||
user = ngx.var.upyun_operator,
|
user = ngx.var.upyun_operator,
|
||||||
passwd = ngx.var.upyun_password,
|
passwd = ngx.var.upyun_password,
|
||||||
@@ -38,9 +44,20 @@ function upyun_upload.upload(filepath, filename)
|
|||||||
|
|
||||||
local info, err = upyun:upload_file(savePath, nil, options)
|
local info, err = upyun:upload_file(savePath, nil, options)
|
||||||
if not info then
|
if not info then
|
||||||
|
|
||||||
|
local error_table = json.decode(err)
|
||||||
|
ngx.log(ngx.ERR, '[' .. error_table["code"] .. ']')
|
||||||
|
|
||||||
|
if (error_table["code"] == 40000006)
|
||||||
|
then
|
||||||
ngx.status = 400
|
ngx.status = 400
|
||||||
ngx.log(ngx.ERR, "failed to upload file : " .. err)
|
ngx.log(ngx.ERR, "Retry upload file : " .. '[' .. err .. ']')
|
||||||
return
|
upyun_upload.upload(filepath, filename, retry+1)
|
||||||
|
else
|
||||||
|
ngx.status = 400
|
||||||
|
ngx.log(ngx.ERR, "failed to upload file : " .. '[' .. err .. ']')
|
||||||
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
ngx.status = 200
|
ngx.status = 200
|
||||||
local fullpath = ngx.var.upyun_domain .. "/" .. directory .. "/" .. filename
|
local fullpath = ngx.var.upyun_domain .. "/" .. directory .. "/" .. filename
|
||||||
@@ -49,4 +66,6 @@ function upyun_upload.upload(filepath, filename)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
return upyun_upload
|
return upyun_upload
|
||||||
@@ -21,4 +21,4 @@ local fullpath = file_dir .. "/" .. file_to_upload .. ".png";
|
|||||||
|
|
||||||
ngx.log(ngx.INFO, 'UPLOAD COMBINED IMAGE -> fullpath -> ', fullpath)
|
ngx.log(ngx.INFO, 'UPLOAD COMBINED IMAGE -> fullpath -> ', fullpath)
|
||||||
|
|
||||||
upyun_upload.upload(fullpath, nil)
|
upyun_upload.upload(fullpath, nil, 0)
|
||||||
Reference in New Issue
Block a user