move upload to a single file

This commit is contained in:
liyong
2022-02-08 00:25:20 +08:00
committed by XiaoLi
parent 9355277bc0
commit 2545370141
3 changed files with 43 additions and 68 deletions
+2 -33
View File
@@ -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
+2 -35
View File
@@ -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*")
+39
View File
@@ -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