From 95878ab1d3b941494394e54cb7c2450f6a2e684a Mon Sep 17 00:00:00 2001 From: XiaoLi Date: Sun, 23 Jul 2023 23:09:13 +0800 Subject: [PATCH] add upyun upload rety --- scripts/lib/upyun_upload.lua | 93 +++++++++++++++++++------------ scripts/upload_combined_image.lua | 2 +- 2 files changed, 57 insertions(+), 38 deletions(-) diff --git a/scripts/lib/upyun_upload.lua b/scripts/lib/upyun_upload.lua index 23476ac..be806ea 100644 --- a/scripts/lib/upyun_upload.lua +++ b/scripts/lib/upyun_upload.lua @@ -4,49 +4,68 @@ package.path = package.path .. ';/?.lua'; 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) - local upyun, err = Upyun:new({ - user = ngx.var.upyun_operator, - passwd = ngx.var.upyun_password, - localFilePath = filepath - }) +function upyun_upload.upload(filepath, filename, retry) - 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 savePath = bucket .. "/" .. directory .. "/" .. filename - ngx.log(ngx.INFO, " savePath " , savePath) - - local options = { - md5 = true - } - - local info, err = upyun:upload_file(savePath, nil, options) - if not info then - ngx.status = 400 - ngx.log(ngx.ERR, "failed to upload file : " .. err) - return + if retry > 3 then + ngx.log(ngx.ERR, "failed to upload file : reach max retries") else - ngx.status = 200 - local fullpath = ngx.var.upyun_domain .. "/" .. directory .. "/" .. filename - ngx.log(ngx.INFO, "SUCCESS UPLOAD -> UPYUN URL -> ", fullpath) - ngx.say(fullpath) + 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 + 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, err = upyun:upload_file(savePath, nil, options) + 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.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 + + else + ngx.status = 200 + local fullpath = ngx.var.upyun_domain .. "/" .. directory .. "/" .. filename + ngx.log(ngx.INFO, "SUCCESS UPLOAD -> UPYUN URL -> ", fullpath) + ngx.say(fullpath) + end end + end return upyun_upload \ No newline at end of file diff --git a/scripts/upload_combined_image.lua b/scripts/upload_combined_image.lua index 7579f40..5a1a4ef 100644 --- a/scripts/upload_combined_image.lua +++ b/scripts/upload_combined_image.lua @@ -21,4 +21,4 @@ local fullpath = file_dir .. "/" .. file_to_upload .. ".png"; ngx.log(ngx.INFO, 'UPLOAD COMBINED IMAGE -> fullpath -> ', fullpath) -upyun_upload.upload(fullpath, nil) \ No newline at end of file +upyun_upload.upload(fullpath, nil, 0) \ No newline at end of file