split audio convert and download
This commit is contained in:
+6
-95
@@ -1,5 +1,4 @@
|
|||||||
local cjson = require "cjson"
|
local cjson = require "cjson"
|
||||||
local http = require "resty.http"
|
|
||||||
local uuid = require "resty.jit-uuid"
|
local uuid = require "resty.jit-uuid"
|
||||||
|
|
||||||
local TaskTypes = {
|
local TaskTypes = {
|
||||||
@@ -7,100 +6,12 @@ local TaskTypes = {
|
|||||||
AUDIO_CONVERT = "AUDIO_CONVERT"
|
AUDIO_CONVERT = "AUDIO_CONVERT"
|
||||||
}
|
}
|
||||||
|
|
||||||
local httpc = http.new()
|
|
||||||
local media_id = ngx.req.get_uri_args().media_id
|
|
||||||
|
|
||||||
local function get_access_token()
|
|
||||||
|
|
||||||
local htyuc = ngx.var.htyuc
|
|
||||||
local host = ngx.var.host
|
|
||||||
|
|
||||||
ngx.log(ngx.INFO, "HTYUC -> ", htyuc)
|
|
||||||
ngx.log(ngx.INFO, "HOST -> ", host)
|
|
||||||
|
|
||||||
ngx.log(ngx.INFO, "URL -> ", ngx.var.uri)
|
|
||||||
|
|
||||||
local sudoerToken = ngx.req.get_headers().HtySudoerToken
|
|
||||||
local htyhostHeader = ngx.req.get_headers().HtyHost
|
|
||||||
|
|
||||||
|
|
||||||
local remote_url = string.format("%s/api/v1/uc/wx/get_access_token", "https://test-music-room.moicen.com")
|
|
||||||
ngx.log(ngx.INFO, 'remote_url -> ', remote_url)
|
|
||||||
ngx.log(ngx.INFO, 'HtyHostHeader -> ', htyhostHeader)
|
|
||||||
ngx.log(ngx.INFO, 'HtySudoerToken -> ', sudoerToken)
|
|
||||||
|
|
||||||
local res, err = httpc:request_uri(
|
|
||||||
remote_url,
|
|
||||||
{
|
|
||||||
ssl_verify = false, -- 设置参数 ssl_verify 为false 不校验ssl证书
|
|
||||||
method = "GET",
|
|
||||||
headers = {
|
|
||||||
["HtyHost"] = htyhostHeader,
|
|
||||||
["HtySudoerToken"] = sudoerToken,
|
|
||||||
},
|
|
||||||
body = body_text,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if res == nil then
|
|
||||||
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *HTYUC*", err)
|
|
||||||
end
|
|
||||||
|
|
||||||
if 200 ~= res.status then
|
|
||||||
ngx.log(ngx.ERR, "GET ACCESS TOKEN *FAILED*", err)
|
|
||||||
ngx.say(err)
|
|
||||||
ngx.exit(res.status)
|
|
||||||
end
|
|
||||||
ngx.log(ngx.ERR, res.body)
|
|
||||||
return cjson.decode(res.body).d
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function get_wx_media()
|
|
||||||
local access_token = get_access_token()
|
|
||||||
print("access token: " .. access_token)
|
|
||||||
local wx_media_url = string.format("https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s", access_token, media_id)
|
|
||||||
print("wx_media_url: " .. wx_media_url)
|
|
||||||
local res, err = httpc:request_uri(wx_media_url, {
|
|
||||||
method = 'GET',
|
|
||||||
ssl_verify = ssl_verify or false, -- 设置参数 ssl_verify 为false 不校验ssl证书
|
|
||||||
})
|
|
||||||
|
|
||||||
if res == nil then
|
|
||||||
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *Weixin*", err)
|
|
||||||
ngx.say(err)
|
|
||||||
ngx.exit(500)
|
|
||||||
end
|
|
||||||
|
|
||||||
if 200 ~= res.status then
|
|
||||||
ngx.log(ngx.ERR, 'GET WECHET MEDIA *FAILED*', err)
|
|
||||||
ngx.say(err)
|
|
||||||
ngx.exit(res.status)
|
|
||||||
end
|
|
||||||
|
|
||||||
local filename = ngx.re.match(res.headers['Content-disposition'], [[filename="(\w+\.(amr|speex))"]], "jo")[1]
|
|
||||||
local file_dir = ngx.var.tmp_file_dir
|
local file_dir = ngx.var.tmp_file_dir
|
||||||
local saved_audio = file_dir .. "/" .. filename;
|
ngx.req.read_body()
|
||||||
print('saved audio file...' .. saved_audio);
|
local req_body = cjson.decode(ngx.req.get_body_data())
|
||||||
local file, err = io.open(saved_audio, 'w')
|
ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data())
|
||||||
if file == nil then
|
local data = req_body["data"]["audio"]
|
||||||
print("Can not open file..." .. err)
|
local converted = file_dir .. "/" .. uuid.generate_v4() .. ".mp3";
|
||||||
else
|
|
||||||
file:write(res.body)
|
|
||||||
file:close()
|
|
||||||
end
|
|
||||||
|
|
||||||
ngx.say(res.body)
|
-- todo: convert audio file to mp3
|
||||||
end
|
|
||||||
|
|
||||||
local function convert()
|
|
||||||
get_wx_media()
|
|
||||||
|
|
||||||
|
|
||||||
local task_server = ngx.var.task_server
|
|
||||||
ngx.log(ngx.INFO, "TASK_SERVER -> ", task_server)
|
|
||||||
|
|
||||||
-- todo: convert file
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
convert()
|
|
||||||
|
|||||||
@@ -0,0 +1,129 @@
|
|||||||
|
local cjson = require "cjson"
|
||||||
|
local http = require "resty.http"
|
||||||
|
|
||||||
|
local TaskTypes = {
|
||||||
|
NOOP = 'NOOP',
|
||||||
|
AUDIO_CONVERT = "AUDIO_CONVERT"
|
||||||
|
}
|
||||||
|
|
||||||
|
local httpc = http.new()
|
||||||
|
local media_id = ngx.req.get_uri_args().media_id
|
||||||
|
local authHeader = ngx.req.get_headers().Authorization
|
||||||
|
local sudoerToken = ngx.req.get_headers().HtySudoerToken
|
||||||
|
local htyhostHeader = ngx.req.get_headers().HtyHost
|
||||||
|
local function get_access_token()
|
||||||
|
|
||||||
|
local htyuc = ngx.var.htyuc
|
||||||
|
local host = ngx.var.host
|
||||||
|
|
||||||
|
ngx.log(ngx.INFO, "HTYUC -> ", htyuc)
|
||||||
|
ngx.log(ngx.INFO, "HOST -> ", host)
|
||||||
|
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")
|
||||||
|
ngx.log(ngx.INFO, 'remote_url -> ', remote_url)
|
||||||
|
ngx.log(ngx.INFO, 'HtyHostHeader -> ', htyhostHeader)
|
||||||
|
ngx.log(ngx.INFO, 'HtySudoerToken -> ', sudoerToken)
|
||||||
|
|
||||||
|
local res, err = httpc:request_uri(
|
||||||
|
remote_url,
|
||||||
|
{
|
||||||
|
ssl_verify = false, -- 设置参数 ssl_verify 为false 不校验ssl证书
|
||||||
|
method = "GET",
|
||||||
|
headers = {
|
||||||
|
["HtyHost"] = htyhostHeader,
|
||||||
|
["HtySudoerToken"] = sudoerToken,
|
||||||
|
},
|
||||||
|
body = body_text,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
if res == nil then
|
||||||
|
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *HTYUC*", err)
|
||||||
|
end
|
||||||
|
|
||||||
|
if 200 ~= res.status then
|
||||||
|
ngx.log(ngx.ERR, "GET ACCESS TOKEN *FAILED*", err)
|
||||||
|
ngx.say(err)
|
||||||
|
ngx.exit(res.status)
|
||||||
|
end
|
||||||
|
ngx.log(ngx.ERR, res.body)
|
||||||
|
return cjson.decode(res.body).d
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function get_wx_media()
|
||||||
|
local access_token = get_access_token()
|
||||||
|
print("access token: " .. access_token)
|
||||||
|
local wx_media_url = string.format("https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s", access_token, media_id)
|
||||||
|
print("wx_media_url: " .. wx_media_url)
|
||||||
|
local res, err = httpc:request_uri(wx_media_url, {
|
||||||
|
method = 'GET',
|
||||||
|
ssl_verify = ssl_verify or false, -- 设置参数 ssl_verify 为false 不校验ssl证书
|
||||||
|
})
|
||||||
|
|
||||||
|
if res == nil then
|
||||||
|
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *Weixin*", err)
|
||||||
|
ngx.say(err)
|
||||||
|
ngx.exit(500)
|
||||||
|
end
|
||||||
|
|
||||||
|
if 200 ~= res.status then
|
||||||
|
ngx.log(ngx.ERR, 'GET WECHET MEDIA *FAILED*', err)
|
||||||
|
ngx.say(err)
|
||||||
|
ngx.exit(res.status)
|
||||||
|
end
|
||||||
|
|
||||||
|
local filename = ngx.re.match(res.headers['Content-disposition'], [[filename="(\w+\.(amr|speex))"]], "jo")[1]
|
||||||
|
local file_dir = ngx.var.tmp_file_dir
|
||||||
|
local saved_audio = file_dir .. "/" .. filename;
|
||||||
|
print('saved audio file...' .. saved_audio);
|
||||||
|
local file, err = io.open(saved_audio, 'w')
|
||||||
|
if file == nil then
|
||||||
|
print("Can not open file..." .. err)
|
||||||
|
else
|
||||||
|
file:write(res.body)
|
||||||
|
file:close()
|
||||||
|
end
|
||||||
|
|
||||||
|
return saved_audio;
|
||||||
|
end
|
||||||
|
|
||||||
|
local function create_task(audio_file)
|
||||||
|
local task_server = ngx.var.task_server
|
||||||
|
local remote_url = string.format("%s/api/v1/ts/create_task", task_server)
|
||||||
|
ngx.log(ngx.INFO, 'remote_url -> ', remote_url)
|
||||||
|
ngx.log(ngx.INFO, 'Authorization -> ', authHeader)
|
||||||
|
ngx.log(ngx.INFO, 'HtySudoerToken -> ', sudoerToken)
|
||||||
|
|
||||||
|
local body_text = cjson.encode({ task_type = TaskTypes.AUDIO_CONVERT, data = { audio_file } })
|
||||||
|
|
||||||
|
ngx.log(ngx.INFO, 'AUDIO_CONVERT *body_text* ->', body_text)
|
||||||
|
local res, err = httpc:request_uri(
|
||||||
|
remote_url,
|
||||||
|
{
|
||||||
|
ssl_verify = false, -- 设置参数 ssl_verify 为false 不校验ssl证书
|
||||||
|
method = "POST",
|
||||||
|
headers = {
|
||||||
|
["Content-Type"] = "application/json",
|
||||||
|
["Authorization"] = authHeader,
|
||||||
|
["HtySudoerToken"] = sudoerToken,
|
||||||
|
},
|
||||||
|
body = body_text,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
if res == nil then
|
||||||
|
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *TASK_SERVER*", err)
|
||||||
|
end
|
||||||
|
|
||||||
|
if 201 ~= res.status then
|
||||||
|
ngx.log(ngx.ERR, "TASK CREATE *FAILED*", err)
|
||||||
|
ngx.say(err)
|
||||||
|
ngx.exit(res.status)
|
||||||
|
end
|
||||||
|
ngx.say(res.body)
|
||||||
|
end
|
||||||
|
|
||||||
|
create_task(get_wx_media())
|
||||||
|
|
||||||
|
|
||||||
@@ -35,6 +35,10 @@ server {
|
|||||||
location /api/ngx/image/combine {
|
location /api/ngx/image/combine {
|
||||||
content_by_lua_file $resty_loc/resty_funcs/combine.lua;
|
content_by_lua_file $resty_loc/resty_funcs/combine.lua;
|
||||||
}
|
}
|
||||||
|
#Audio file download
|
||||||
|
location /api/ngx/audio/download {
|
||||||
|
content_by_lua_file $resty_loc/resty_funcs/audio_download.lua;
|
||||||
|
}
|
||||||
#Audio file convert
|
#Audio file convert
|
||||||
location /api/ngx/audio/convert {
|
location /api/ngx/audio/convert {
|
||||||
content_by_lua_file $resty_loc/resty_funcs/audio_convert.lua;
|
content_by_lua_file $resty_loc/resty_funcs/audio_convert.lua;
|
||||||
|
|||||||
@@ -72,6 +72,10 @@ server {
|
|||||||
location /api/ngx/image/combine {
|
location /api/ngx/image/combine {
|
||||||
content_by_lua_file $resty_loc/resty_funcs/combine.lua;
|
content_by_lua_file $resty_loc/resty_funcs/combine.lua;
|
||||||
}
|
}
|
||||||
|
#Audio file download
|
||||||
|
location /api/ngx/audio/download {
|
||||||
|
content_by_lua_file $resty_loc/resty_funcs/audio_download.lua;
|
||||||
|
}
|
||||||
#Audio file convert
|
#Audio file convert
|
||||||
location /api/ngx/audio/convert {
|
location /api/ngx/audio/convert {
|
||||||
content_by_lua_file $resty_loc/resty_funcs/audio_convert.lua;
|
content_by_lua_file $resty_loc/resty_funcs/audio_convert.lua;
|
||||||
|
|||||||
Reference in New Issue
Block a user