From 254537014145cbd2bf6f564c68a3cee9e4c16e66 Mon Sep 17 00:00:00 2001 From: liyong Date: Tue, 8 Feb 2022 00:25:20 +0800 Subject: [PATCH] move upload to a single file --- scripts/combine.lua | 35 ++--------------------------------- scripts/convert_audio.lua | 37 ++----------------------------------- scripts/upyun_upload.lua | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 68 deletions(-) create mode 100644 scripts/upyun_upload.lua diff --git a/scripts/combine.lua b/scripts/combine.lua index 9a0b925..220493a 100644 --- a/scripts/combine.lua +++ b/scripts/combine.lua @@ -2,40 +2,9 @@ package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua'; local uuid = require "resty.jit-uuid" local cjson = require "cjson" -local strip_path = require("strip_path") -local Upyun = require('upyun') +local upyun_upload = require("upyun_upload") uuid.seed() -local function upload(filepath) - 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 - local filename = strip_path.strip_path(filepath) - - local savePath = bucket .. "/" .. directory .. "/" .. filename - - local info, err = upyun:upload_file(savePath, nil, nil) - if not info then - ngx.status = 502 - ngx.log(ngx.ERR, "failed to upload file : " .. err) - return - else - ngx.status = 200 - ngx.say(ngx.var.upyun_cdn .. directory .. "/" .. filename) - end -end - local file_dir = ngx.var.tmp_file_dir ngx.req.read_body() local req_body = cjson.decode(ngx.req.get_body_data()) @@ -71,7 +40,7 @@ if result and code == 0 then ngx.log(ngx.INFO, "Upload combined image " .. combined .. " to upyun .") - upload(combined) + upyun_upload.upload(combined) else ngx.status = 500 diff --git a/scripts/convert_audio.lua b/scripts/convert_audio.lua index 7062910..3932d27 100644 --- a/scripts/convert_audio.lua +++ b/scripts/convert_audio.lua @@ -3,10 +3,7 @@ package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua'; local cjson = require "cjson" local uuid = require "resty.jit-uuid" local http = require "resty.http" -local strip_path = require("strip_path") -local Upyun = require('upyun') - - +local upyun_upload = require("upyun_upload") local TaskTypes = { NOOP = 'NOOP', @@ -94,36 +91,6 @@ local function get_wx_media(media_id) return saved_audio; end -local function upload(filepath) - 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 - local filename = strip_path.strip_path(filepath) - - local savePath = bucket .. "/" .. directory .. "/" .. filename - - local info, err = upyun:upload_file(savePath, nil, nil) - if not info then - ngx.status = 502 - ngx.log(ngx.ERR, "failed to upload file : " .. err) - return - else - ngx.status = 200 - ngx.say(ngx.var.upyun_cdn .. directory .. "/" .. filename) - end -end - local function convert() local file_dir = ngx.var.tmp_file_dir @@ -144,7 +111,7 @@ local function convert() local result, _, code = os.execute(cmd) if result and code == 0 then ngx.log(ngx.INFO, "result -> ", result); - upload(converted_audio_file) + upyun_upload.upload(converted_audio_file) else ngx.status = 500 ngx.log(ngx.ERR, "AUDIO CONVERT *FAILED*") diff --git a/scripts/upyun_upload.lua b/scripts/upyun_upload.lua new file mode 100644 index 0000000..8c7ea82 --- /dev/null +++ b/scripts/upyun_upload.lua @@ -0,0 +1,39 @@ +-- 指定模块引用目录,否则无法加载同目录下的其他文件 +package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua'; + +local upyun_upload = {} + +local strip_path = require("strip_path") +local Upyun = require('upyun') + +function upyun_upload.upload(filepath) + 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 + local filename = strip_path.strip_path(filepath) + + local savePath = bucket .. "/" .. directory .. "/" .. filename + + local info, err = upyun:upload_file(savePath, nil, nil) + if not info then + ngx.status = 502 + ngx.log(ngx.ERR, "failed to upload file : " .. err) + return + else + ngx.status = 200 + ngx.say(ngx.var.upyun_cdn .. directory .. "/" .. filename) + end +end + +return upyun_upload \ No newline at end of file