38 lines
1.3 KiB
Lua
38 lines
1.3 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 upyun_upload = require("upyun_upload")
|
|
local get_wx_media = require("wx_download")
|
|
|
|
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();
|