Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to remove concatenated media names #6737

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion applications/cdr/src/cdr_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
,register_views/0
]).
-export([save_cdr/2]).
-export([check_media_names/1]).

-include("cdr.hrl").

Expand Down Expand Up @@ -43,7 +44,13 @@ save_cdr(AccountMOD, Doc) ->

-spec do_save_cdr(kz_term:api_binary(), kz_json:object()) -> 'ok' | {'error', 'max_save_retries'}.
do_save_cdr(AccountMODb, Doc) ->
case kazoo_modb:save_doc(AccountMODb, Doc, [{'max_retries', 3}]) of
MediaNames = check_media_names(kz_json:get_value([<<"custom_channel_vars">>, <<"media_names">>], Doc)),
MediaRecordings = check_media_names(kz_json:get_value([<<"custom_channel_vars">>, <<"media_recordings">>], Doc)),

NewDoc = kz_json:set_values([{[<<"custom_channel_vars">>, <<"media_names">>], MediaNames}
,{[<<"custom_channel_vars">>, <<"media_recordings">>], MediaRecordings}], Doc),

case kazoo_modb:save_doc(AccountMODb, NewDoc, [{'max_retres', 3}]) of
{'ok', _}-> 'ok';
{'error', 'conflict'} -> 'ok';
{'error', _E} ->
Expand All @@ -54,3 +61,18 @@ do_save_cdr(AccountMODb, Doc) ->
-spec register_views() -> 'ok'.
register_views() ->
kz_datamgr:register_views_from_folder('cdr').

-spec check_media_names(list()|kz_term:api_binary()| 'undefined') -> list() | 'undefined'.
check_media_names([HeadMN | TailMN]) ->
re:split(HeadMN, ",") ++ check_last_media_name(TailMN);
check_media_names('undefined') ->
'undefined';
check_media_names(MediaNames) ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this crash is MediaNames is 'undefined' ?

re:split(MediaNames, ",").


-spec check_last_media_name(list())->list().
check_last_media_name([FirstMediaName])->
re:split(FirstMediaName, ",");
check_last_media_name([FirstMediaName | LasttMediaName])->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[FirstMediaName | LastMediaName] means that LastMediaName will be a list of binaries. Then you re:split on it below which is likely to crash.

Where are the tests showing this functionality working as expected?

re:split(FirstMediaName, ",") ++ check_media_names(re:split(LasttMediaName, ",")).
33 changes: 33 additions & 0 deletions applications/cdr/test/cdr_util_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-module(cdr_util_tests).
-include_lib("eunit/include/eunit.hrl").

-spec check_media_names_test() -> 'ok'.
check_media_names_test()->

A = <<"a9b8c9cf7546827d9ec5ad505770fc86.mp3">>,

B = <<"202109-d85184f8cfc5592f944bc323617c16ac">>,

C = [<<"8e9f742001e09ff12abf5ce8bfcb1ff9.mp3">>,<<"3c37d4a18634030662e68523bf74da96.mp3,092b94f757eaceadafbc1ae0dd858057.mp3,51a945565136ed62dce5caf9b90d3630.mp3,e038de9919babfbe28f49fe1d1673405.mp3,9530f5fbdb1efe1f0796506785c6ec7c.mp3">>],

D = [<<"8e9f742001e09ff12abf5ce8bfcb1ff9.mp3">>,
<<"3c37d4a18634030662e68523bf74da96.mp3">>,
<<"092b94f757eaceadafbc1ae0dd858057.mp3">>,
<<"51a945565136ed62dce5caf9b90d3630.mp3">>,
<<"e038de9919babfbe28f49fe1d1673405.mp3">>,
<<"9530f5fbdb1efe1f0796506785c6ec7c.mp3">>],

E = [<<"202109-f13d49151c46627fb19c3f69b8580c5c">>,<<"202109-2643e9204a52bf529a03a6909d740416,202109-105f6012c16cf1b5af018423ab1dbee8,202109-4cf5e781b82838e750b7ea5fd064e67f,202109-f42c80fd47f98f424520cca147641ecf,202109-103b202b96a2f963f59bc99521af98ff">>],

F = [<<"202109-f13d49151c46627fb19c3f69b8580c5c">>,
<<"202109-2643e9204a52bf529a03a6909d740416">>,
<<"202109-105f6012c16cf1b5af018423ab1dbee8">>,
<<"202109-4cf5e781b82838e750b7ea5fd064e67f">>,
<<"202109-f42c80fd47f98f424520cca147641ecf">>,
<<"202109-103b202b96a2f963f59bc99521af98ff">>],

?assertEqual('undefined',cdr_util:check_media_names('undefined')),
?assertMatch([A], cdr_util:check_media_names(A)),
?assertMatch([B], cdr_util:check_media_names(B)),
?assertMatch(D,cdr_util:check_media_names(C)),
?assertMatch(F,cdr_util:check_media_names(E)).
17 changes: 17 additions & 0 deletions applications/ecallmgr/src/ecallmgr_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,10 @@ get_fs_kv(<<"Hold-Media">>, Media, UUID) ->
list_to_binary(["hold_music=", MediaPath]);
get_fs_kv(?CCV(Key), Val, UUID) ->
get_fs_kv(Key, Val, UUID);
get_fs_kv(<<"Media-Names">>, [FileName1 | []], _) ->
list_to_binary([get_fs_key(<<"Media-Names">>), "=", maybe_sanitize_fs_value(<<"Media-Names">>, FileName1)]);
get_fs_kv(<<"Media-Recordings">>, [FileName1 | []], _) ->
list_to_binary([get_fs_key(<<"Media-Recordings">>), "=", maybe_sanitize_fs_value(<<"Media-Recordings">>, FileName1)]);
get_fs_kv(Key, Val, _) ->
list_to_binary([get_fs_key(Key), "=", maybe_sanitize_fs_value(Key, Val)]).

Expand Down Expand Up @@ -731,10 +735,23 @@ maybe_sanitize_fs_value(<<"Export-Variables">>, Val) ->
kz_binary:join(Val, <<",">>);
maybe_sanitize_fs_value(Key, Val) when not is_binary(Key) ->
maybe_sanitize_fs_value(kz_term:to_binary(Key), Val);
maybe_sanitize_fs_value(<<"Media-Names">>, Val) when not is_binary(Val) ->
maybe_sanitize_fs_value(<<"Media-Names">>, checking_list_media_name(Val));
maybe_sanitize_fs_value(<<"Media-Recordings">>, Val) when not is_binary(Val) ->
maybe_sanitize_fs_value(<<"Media-Recordings">>, checking_list_media_name(Val));
maybe_sanitize_fs_value(Key, Val) when not is_binary(Val) ->
maybe_sanitize_fs_value(Key, kz_term:to_binary(Val));
maybe_sanitize_fs_value(_, Val) -> Val.

-spec checking_list_media_name(list()) -> binary().
checking_list_media_name([])->
<<>>;
checking_list_media_name([MediaName1 | []]) ->
<<MediaName1/binary>>;
checking_list_media_name([MediaName1 | MediaName2])->
Next = checking_list_media_name(MediaName2),
<<MediaName1/binary, ",", Next/binary>>.

%%------------------------------------------------------------------------------
%% @doc takes endpoints (/sofia/foo/bar), and optionally a caller id name/num
%% and create the dial string ([origination_caller_id_name=Name
Expand Down