125 lines
3.9 KiB
Lua
125 lines
3.9 KiB
Lua
-- 指定模块引用目录,否则无法加载同目录下的其他文件
|
|
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
|
|
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
|
|
|
|
local cjson = require "cjson"
|
|
local uuid = require "resty.jit-uuid"
|
|
local http = require "resty.http"
|
|
local upyun_upload = require("upyun_upload")
|
|
|
|
local TaskTypes = {
|
|
NOOP = 'NOOP',
|
|
CONVERT_AUDIO_FILE = "CONVERT_AUDIO_FILE"
|
|
}
|
|
|
|
local httpc = http.new()
|
|
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, "Request URL -> ", ngx.var.uri)
|
|
ngx.log(ngx.INFO, "HTYUC -> ", htyuc)
|
|
ngx.log(ngx.INFO, "Host -> ", host)
|
|
ngx.log(ngx.INFO, 'HtyHostHeader -> ', htyhostHeader)
|
|
ngx.log(ngx.INFO, 'HtySudoerToken -> ', sudoerToken)
|
|
|
|
local remote_url = string.format("%s/api/v1/uc/wx_get_access_token", htyuc)
|
|
ngx.log(ngx.INFO, 'remote_url -> ', remote_url)
|
|
|
|
local res, err = httpc:request_uri(
|
|
remote_url,
|
|
{
|
|
ssl_verify = false, -- 设置参数 ssl_verify 为false 不校验ssl证书
|
|
method = "GET",
|
|
headers = {
|
|
["HtyHost"] = "wx.moicen.com",
|
|
["HtySudoerToken"] = sudoerToken,
|
|
}
|
|
}
|
|
)
|
|
|
|
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)
|
|
else
|
|
ngx.log(ngx.ERR, res.body)
|
|
return cjson.decode(res.body).d
|
|
end
|
|
end
|
|
|
|
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)
|
|
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="(.+\.(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 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 input_audio_file = get_wx_media(req_body["payload"]["media_id"]);
|
|
local converted_audio_file = file_dir .. "/" .. uuid.generate_v4() .. ".mp3";
|
|
|
|
ngx.log(ngx.INFO, "Input audio file -> ", input_audio_file);
|
|
ngx.log(ngx.INFO, "Converted audio file -> ", converted_audio_file);
|
|
|
|
local cmd = "/usr/local/bin/file_convert" .. " " .. input_audio_file .. " " .. converted_audio_file
|
|
|
|
ngx.log(ngx.INFO, 'Audio convert command -> ', cmd)
|
|
|
|
local result, _, code = os.execute(cmd)
|
|
if result and code == 0 then
|
|
ngx.log(ngx.INFO, "result -> ", result);
|
|
upyun_upload.upload(converted_audio_file)
|
|
else
|
|
ngx.status = 500
|
|
ngx.log(ngx.ERR, "AUDIO CONVERT *FAILED*")
|
|
end
|
|
end
|
|
|
|
convert();
|