Allow use odbc to store files (record/voicemail) in database. (#1535)

* Change. Allow use odbc to store files (record/voicemail) in database.

* Fix. Generate correct default config.
This commit is contained in:
Alexey Melnichuk
2016-09-04 22:05:47 +03:00
committed by FusionPBX
parent 119a007acd
commit 09f2d8fa43
16 changed files with 603 additions and 655 deletions

View File

@@ -1,70 +1,50 @@
--get the argv values
script_name = argv[0];
file_name = table.concat(argv, " ");
local script_name = argv[0];
local file_name = table.concat(argv, " ");
freeswitch.consoleLog("notice", "[streamfile] file_name: " .. file_name .. "\n");
--include config.lua
require "resources.functions.config";
--connect to the database
require "resources.functions.database_handle";
dbh = database_handle('system');
--load libraries
local Database = require "resources.functions.database";
local Settings = require "resources.functions.lazy_settings";
local file = require "resources.functions.file";
local log = require "resources.functions.log".streamfile;
--get the variables
domain_name = session:getVariable("domain_name");
domain_uuid = session:getVariable("domain_uuid");
local domain_name = session:getVariable("domain_name");
local domain_uuid = session:getVariable("domain_uuid");
--get the sounds dir, language, dialect and voice
sounds_dir = session:getVariable("sounds_dir");
default_language = session:getVariable("default_language");
default_dialect = session:getVariable("default_dialect");
default_voice = session:getVariable("default_voice");
if (not default_language) then default_language = 'en'; end
if (not default_dialect) then default_dialect = 'us'; end
if (not default_voice) then default_voice = 'callie'; end
--settings
require "resources.functions.settings";
settings = settings(domain_uuid);
storage_type = "";
storage_path = "";
if (settings['recordings'] ~= nil) then
if (settings['recordings']['storage_type'] ~= nil) then
if (settings['recordings']['storage_type']['text'] ~= nil) then
storage_type = settings['recordings']['storage_type']['text'];
end
end
if (settings['recordings']['storage_path'] ~= nil) then
if (settings['recordings']['storage_path']['text'] ~= nil) then
storage_path = settings['recordings']['storage_path']['text'];
storage_path = storage_path:gsub("${domain_name}", domain_name);
storage_path = storage_path:gsub("${voicemail_id}", voicemail_id);
storage_path = storage_path:gsub("${voicemail_dir}", voicemail_dir);
end
end
end
if (not temp_dir) or (#temp_dir == 0) then
if (settings['server'] ~= nil) then
if (settings['server']['temp'] ~= nil) then
if (settings['server']['temp']['dir'] ~= nil) then
temp_dir = settings['server']['temp']['dir'];
end
end
end
end
local sounds_dir = session:getVariable("sounds_dir");
local default_language = session:getVariable("default_language") or 'en';
local default_dialect = session:getVariable("default_dialect") or 'us';
local default_voice = session:getVariable("default_voice") or 'callie';
--set the recordings directory
recordings_dir = recordings_dir .. "/"..domain_name;
local recordings_dir = recordings_dir .. "/" .. domain_name;
--check if a file exists
require "resources.functions.file_exists";
--parse file name
local file_name_only = file_name:match("([^/]+)$");
--settings
local dbh = Database.new('system');
local settings = Settings.new(dbh, domain_name, domain_uuid);
local storage_type = settings:get('recordings', 'storage_type', 'text') or '';
if (not temp_dir) or (#temp_dir == 0) then
temp_dir = settings:get('server', 'temp', 'dir') or '/tmp';
end
dbh:release()
--define the on_dtmf call back function
-- luacheck: globals on_dtmf, ignore s arg
function on_dtmf(s, type, obj, arg)
if (type == "dtmf") then
session:setVariable("dtmf_digits", obj['digit']);
freeswitch.console_log("info", "[streamfile] dtmf digit: " .. obj['digit'] .. ", duration: " .. obj['duration'] .. "\n");
log.info("dtmf digit: " .. obj['digit'] .. ", duration: " .. obj['duration']);
if (obj['digit'] == "*") then
return("false"); --return to previous
elseif (obj['digit'] == "0") then
@@ -87,50 +67,43 @@
end
end
--parse file name
file_name_only = file_name:match("([^/]+)$");
--if base64, get from db, create temp file
if (storage_type == "base64") then
if (not file_exists(recordings_dir.."/"..file_name_only)) then
sql = [[SELECT * FROM v_recordings
WHERE domain_uuid = ']] .. domain_uuid ..[['
AND recording_filename = ']].. file_name_only.. [[' ]];
local full_path = recordings_dir .. "/" .. file_name_only
if not file.exists(full_path) then
local sql = "SELECT recording_base64 FROM v_recordings "
.. "WHERE domain_uuid = '" .. domain_uuid .."'"
.. "AND recording_filename = '".. file_name_only.. "' ";
if (debug["sql"]) then
freeswitch.consoleLog("notice", "[ivr_menu] SQL: " .. sql .. "\n");
log.notice("SQL: " .. sql);
end
local dbh = Database.new('system', 'base64/read');
local recording_base64 = dbh:first_value(sql);
dbh:release();
if recording_base64 and #recording_base64 > 32 then
file_name = full_path;
file.write_base64(file_name, recording_base64);
end
status = dbh:query(sql, function(row)
--add functions
require "resources.functions.base64";
--add the path to filename
file_name = recordings_dir.."/"..file_name_only;
--save the recording to the file system
if (string.len(row["recording_base64"]) > 32) then
local file = io.open(file_name, "w");
file:write(base64.decode(row["recording_base64"]));
file:close();
end
end);
else
file_name = recordings_dir.."/"..file_name_only;
file_name = full_path;
end
end
--adjust file path
if (not file_exists(file_name)) then
if (file_exists(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..file_name_only)) then
file_name = sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..file_name_only;
elseif (file_exists(recordings_dir.."/"..file_name_only)) then
file_name = recordings_dir.."/"..file_name_only;
end
if not file.exists(file_name) then
file_name = file.exists(sounds_dir.."/"..default_language.."/"..default_dialect.."/"..default_voice.."/"..file_name_only)
or file.exists(recordings_dir.."/"..file_name_only)
or file_name
end
--stream file if exists
if (session:ready()) then
session:answer();
slept = session:getVariable("slept");
local slept = session:getVariable("slept");
if (slept == nil or slept == "false") then
freeswitch.consoleLog("notice", "[ivr_menu] sleeping (1s)\n");
log.notice("sleeping (1s)");
session:sleep(1000);
if (slept == "false") then
session:setVariable("slept", "true");
@@ -142,8 +115,8 @@
end
--if base64, remove temp file (increases responsiveness when files remain local)
if (storage_type == "base64") then
if (file_exists(file_name)) then
--os.remove(file_name);
end
end
-- if (storage_type == "base64") then
-- if (file.exists(file_name)) then
-- file.remove(file_name);
-- end
-- end