diff --git a/audio_convert.lua b/audio_convert.lua deleted file mode 100644 index 94b84f8..0000000 --- a/audio_convert.lua +++ /dev/null @@ -1,17 +0,0 @@ -local cjson = require "cjson" -local uuid = require "resty.jit-uuid" - -local TaskTypes = { - NOOP = 'NOOP', - AUDIO_CONVERT = "AUDIO_CONVERT" -} - - -local file_dir = ngx.var.tmp_file_dir -ngx.req.read_body() -local req_body = cjson.decode(ngx.req.get_body_data()) -ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data()) -local data = req_body["data"]["audio"] -local converted = file_dir .. "/" .. uuid.generate_v4() .. ".mp3"; - --- todo: convert audio file to mp3 diff --git a/audio_download.lua b/covnert_audio.lua similarity index 65% rename from audio_download.lua rename to covnert_audio.lua index 8601a97..6da38b8 100644 --- a/audio_download.lua +++ b/covnert_audio.lua @@ -1,16 +1,17 @@ local cjson = require "cjson" +local uuid = require "resty.jit-uuid" local http = require "resty.http" local TaskTypes = { NOOP = 'NOOP', - AUDIO_CONVERT = "AUDIO_CONVERT" + CONVERT_AUDIO_FILE = "CONVERT_AUDIO_FILE" } 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 @@ -51,8 +52,7 @@ local function get_access_token() return cjson.decode(res.body).d end - -local function get_wx_media() +local function get_wx_media(media_id) 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) @@ -89,41 +89,20 @@ local function get_wx_media() 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 +local function convert() + local file_dir = ngx.var.tmp_file_dir + ngx.req.read_body() + local req_body = cjson.decode(ngx.req.get_body_data()) + ngx.log(ngx.INFO, 'REQ_BODY -> ', ngx.req.get_body_data()) - 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) + + local audio_file = get_wx_media(req_body["data"]["media_id"]); + local converted = file_dir .. "/" .. uuid.generate_v4() .. ".mp3"; + + -- todo convert file end -create_task(get_wx_media()) - +convert(); diff --git a/music-room-dev.conf b/music-room-dev.conf index 8a7b224..31a0016 100644 --- a/music-room-dev.conf +++ b/music-room-dev.conf @@ -36,12 +36,12 @@ server { 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; + location /api/ngx/audio/upload { + content_by_lua_file $resty_loc/resty_funcs/upload_audio.lua; } #Audio file 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/covnert_audio.lua; } #Static file server location /file_upload { diff --git a/music-room-test.conf b/music-room-test.conf index 16510c9..72cb910 100644 --- a/music-room-test.conf +++ b/music-room-test.conf @@ -73,12 +73,12 @@ server { 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; + location /api/ngx/audio/upload { + content_by_lua_file $resty_loc/resty_funcs/upload_audio.lua; } #Audio file 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/covnert_audio.lua; } #Static file server location /file_upload { diff --git a/upload_audio.lua b/upload_audio.lua new file mode 100644 index 0000000..bb46506 --- /dev/null +++ b/upload_audio.lua @@ -0,0 +1,51 @@ +local cjson = require "cjson" +local http = require "resty.http" + +local TaskTypes = { + NOOP = 'NOOP', + CONVERT_AUDIO_FILE = "CONVERT_AUDIO_FILE" +} + +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 function create_task() + 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 = { media_id } }) + + 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() + +