音频转换及上传upyun

This commit is contained in:
moicen
2022-01-28 02:25:18 +08:00
committed by 木逸辰
parent 862aff55da
commit 6f02e88c37
6 changed files with 54 additions and 51 deletions
+2
View File
@@ -22,6 +22,8 @@ nameserver 183.60.83.19
[weli@moicen ~]$
```
使用上述nameserver的时候,请求upyun会提示no route to host错误,使用8.8.8.8偶尔也会出现
put `upload.lua` into:
```bash
+5
View File
@@ -14,6 +14,7 @@ server {
set $htyuc "http://127.0.0.1:3000"; #htyuc host
set $resty_loc "/usr/local/openresty";
set $convert "/usr/bin/convert";
set $upyun_cdn "https://upyun.alchemy-studio.cn/";
location / {
try_files $uri $uri/ /index.html;
@@ -54,6 +55,10 @@ server {
content_by_lua_file $resty_loc/nginx/scripts/convert_audio.lua;
}
location /api/ngx/convert/test {
content_by_lua_file $resty_loc/nginx/scripts/test.lua;
}
location /api/v1/ws/ {
proxy_pass http://127.0.0.1:3001/api/v1/ws/;
}
+1 -2
View File
@@ -19,8 +19,7 @@ server {
set $upyun_password "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN";
set $upyun_bucket "huiwing";
set $upyun_directory "music-room";
set $upyun_cdn "https://upyun.alchemy-studio.cn/music-room/";
set $upyun_api "https://v2.api.upyun.com";
set $upyun_cdn "https://upyun.alchemy-studio.cn/";
location / {
try_files $uri $uri/ /index.html;
+35 -40
View File
@@ -1,7 +1,11 @@
-- 指定模块引用目录,否则无法加载同目录下的其他文件
package.path = package.path .. ';/usr/local/openresty/nginx/scripts/?.lua';
local cjson = require "cjson"
local uuid = require "resty.jit-uuid"
local http = require "resty.http"
local upyun_token = require('upyun_token')
local strip_path = require("strip_path")
local Upyun = require('upyun')
local TaskTypes = {
@@ -24,7 +28,7 @@ local function get_access_token()
ngx.log(ngx.INFO, "URL -> ", ngx.var.uri)
local remote_url = string.format("%s/api/v1/uc/wx/get_access_token", "https://test-music-room.moicen.com")
local remote_url = string.format("%s/api/v1/uc/wx/get_access_token", htyuc)
ngx.log(ngx.INFO, 'remote_url -> ', remote_url)
ngx.log(ngx.INFO, 'HtyHostHeader -> ', htyhostHeader)
ngx.log(ngx.INFO, 'HtySudoerToken -> ', sudoerToken)
@@ -35,10 +39,9 @@ local function get_access_token()
ssl_verify = false, -- 设置参数 ssl_verify 为false 不校验ssl证书
method = "GET",
headers = {
["HtyHost"] = htyhostHeader,
["HtyHost"] = "wx.moicen.com",
["HtySudoerToken"] = sudoerToken,
},
body = body_text,
}
}
)
if res == nil then
@@ -76,7 +79,7 @@ local function get_wx_media(media_id)
ngx.exit(res.status)
end
local filename = ngx.re.match(res.headers['Content-disposition'], [[filename="(\w+\.(amr|speex))"]], "jo")[1]
local filename = ngx.re.match(res.headers['Content-disposition'], [[filename="(.+\.(amr|speex))"]], "jo")[1]
local file_dir = ngx.var.tmp_file_dir
local saved_audio = file_dir .. "/" .. filename;
print('saved audio file...' .. saved_audio);
@@ -91,43 +94,37 @@ local function get_wx_media(media_id)
return saved_audio;
end
local function upload(filename)
local upyun_api = ngx.var.upyun_api
local token = upyun_token(filename)
local file, err = io.open(saved_audio, 'w')
local form = {
["Authorization"] = token.token,
["file"] = file,
["policy"] = token.policy
}
local res, err = httpc:request_uri(
upyun_api,
{
ssl_verify = false, -- 设置参数 ssl_verify 为false 不校验ssl证书
method = "POST",
body = form,
}
)
if res == nil then
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *Upyun*", err)
ngx.say(err)
ngx.exit(500)
local function upload(filepath)
local upyun, err = Upyun:new({
user = ngx.var.upyun_operator,
password = ngx.var.upyun_password,
localFilePath = filepath
})
if not upyun then
ngx.status = 500
ngx.log(ngx.ERR, "failed to initialize upyun: " .. err)
return
end
if 200 ~= res.status then
ngx.log(ngx.ERR, 'GET WECHET MEDIA *FAILED*', err)
ngx.say(err)
ngx.exit(res.status)
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 image file : " .. err)
return
else
ngx.status = 200
ngx.say(ngx.var.upyun_cdn .. directory .. "/" .. filename)
end
end
local function convert()
-- mock for test
local res = cjson.encode({
r = true, d = "https://upyun.alchemy-studio.cn/daily-music/audios/Song-of-joy.mp3"
})
ngx.say(res)
ngx.exit(200)
local file_dir = ngx.var.tmp_file_dir
ngx.req.read_body()
@@ -147,13 +144,11 @@ local function convert()
local result, _, code = os.execute(cmd)
if result and code == 0 then
ngx.log(ngx.INFO, "result -> ", result);
upload(result)
upload(converted_audio_file)
else
ngx.status = 500
ngx.log(ngx.ERR, "AUDIO CONVERT *FAILED*")
end
ngx.log(ngx.INFO, "Output audio file -> ", result);
end
convert();
+5 -3
View File
@@ -1,16 +1,18 @@
package.path = package.path .. ';/usr/local/openresty/nginx/scripts/?.lua';
local yun = require("upyun")
local cjson = require "cjson"
local function upyun_upload_file()
local config = {
user = "moicen", --授权操作员名称
passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN", --操作员密码
localFilePath = "/Users/liyong/Code/Lua/resty_functions/test_files_dir/r2d2.jpeg"
localFilePath = "/file_upload/abc.mp3"
}
local upyun = yun:new(config)
local savePath = "huiwing/test/r2d2.jpeg"
local savePath = "huiwing/test/abc.mp3"
local gmkerl = nil
local options = nil
@@ -19,7 +21,7 @@ local function upyun_upload_file()
ngx.say("failed to upload image file : " .. err)
return
else
ngx.say(info)
ngx.say(cjson.encode(info))
end
end
+6 -6
View File
@@ -284,8 +284,8 @@ local function _request(sock, method, path, headers, body, extra)
-- Build and send request header
print("Debug upyun module request header method : ", method)
print("Debug upyun module request header path : ", path)
print("Debug upyun module request header headers : ", headers)
print("Debug upyun module request header extra : ", extra)
--print("Debug upyun module request header headers : ", headers)
--print("Debug upyun module request header extra : ", extra)
local header = _req_header(method, path, headers, extra)
local bytes, err = sock:send(header)
@@ -636,21 +636,21 @@ function _M.upload_file(self, path, gmkerl, option)
if not ret then
return nil, err
end
print("Upyun module upload file request ret : ", ret)
--print("Upyun module upload file request ret : ", ret)
print("Upyun module upload file parse response headers : ", ret)
--print("Upyun module upload file parse response headers : ", ret)
ret, err = _parse_upyun_headers(ret.headers, [[^x-upyun-([\w-]+)$]])
if not ret then
return nil, err
end
print("Upyun module upload file parse response headers ret : ", ret)
--print("Upyun module upload file parse response headers ret : ", ret)
-- write the original author back as header.Authorization
-- may be changed in the _upyun_request()
headers.Authorization = author
headers["Content-Length"] = "0"
print("Upyun module upload file ret : ", ret)
--print("Upyun module upload file ret : ", ret)
return ret
end