Skip to content

Commit

Permalink
[4.3] PISTON-1170: Update content type on media attachments (#6674)
Browse files Browse the repository at this point in the history
The `kzm_message:forward_message/4` function branches, depending on
whether the user has recorded an additional message to be prepended to
the voicemail message being forwarded. Prepending the additional
message requires the two media files to be merged into a temporary
file. This is then written to the database as an attachment.

Prior to this fix, the code assumed that the merged media file has an
`mp3` format, which is not necessarily true. In addition, it did not
pass any content-type option to the `kz_datamgr:put_attachment/5`
function which actually writes the attachment. As a result, the
attachment metadata in the database always indicated a content-type of
`application/octet-stream`.

Calls to `kz_media_util:join_media_files/2` now ensure that the merged
output format is specified explicitly, rather than relying on default
behaviour. The same format is also mapped to the corresponding MIME
type which is passed to `kz_datamgr:put_attachment/5` to ensure the
attachment metadata is correct.
  • Loading branch information
Roger Neate authored and jamesaimonetti committed Dec 16, 2020
1 parent 8c35973 commit 44c449e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions core/kazoo_media/include/kz_media.hrl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-ifndef(KZ_MEDIA_HRL).

-define(CHUNKSIZE, 24576).
-define(NORMALIZATION_FORMAT, kapps_config:get_ne_binary(<<"crossbar.media">>, <<"normalization_format">>, <<"mp3">>)).

-record(media_file, {stream_url = <<>> :: binary()
,contents = <<>> :: binary()
Expand Down
2 changes: 0 additions & 2 deletions core/kazoo_media/src/kz_media_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
-define(NORMALIZE_SOURCE_ARGS, kapps_config:get_binary(?CONFIG_CAT, <<"normalize_source_args">>, <<>>)).
-define(NORMALIZE_DEST_ARGS, kapps_config:get_binary(?CONFIG_CAT, <<"normalize_destination_args">>, <<"-r 8000">>)).

-define(NORMALIZATION_FORMAT, kapps_config:get_ne_binary(<<"crossbar.media">>, <<"normalization_format">>, <<"mp3">>)).

-define(USE_ACCOUNT_OVERRIDES, kapps_config:get_is_true(?CONFIG_CAT, <<"support_account_overrides">>, 'true')).

-define(DEFAULT_MAX_RECORDING_LIMIT, 3*?SECONDS_IN_HOUR).
Expand Down
22 changes: 13 additions & 9 deletions core/kazoo_voicemail/src/kvm_message.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
]).

-include("kz_voicemail.hrl").
-include("kazoo_media/include/kz_media.hrl").

-export_type([vm_folder/0]).

Expand Down Expand Up @@ -785,32 +786,35 @@ prepend_forward_message(Call, ForwardId, Metadata, _SrcBoxId, Props) ->
{'ok', _} = kz_media_util:synthesize_tone(OrigSampleRate, <<"440">>, <<"0.5">>, TonePath),

lager:debug("joining prepend to original message"),
case kz_media_util:join_media_files([TmpPath, TonePath, OrigPath], [{sample_rate, OrigSampleRate}]) of
JoinFormat = ?NORMALIZATION_FORMAT,
JoinOptions = [{'sample_rate', OrigSampleRate}, {'to_format', JoinFormat}],
case kz_media_util:join_media_files([TmpPath, TonePath, OrigPath], JoinOptions) of
{'ok', FileContents} ->
JoinFilename = <<(kz_binary:rand_hex(16))/binary, ".mp3">>,
JoinFilename = <<(kz_binary:rand_hex(16))/binary, ".", JoinFormat/binary>>,
FileProps = [{'content_type', kz_mime:from_extension(JoinFormat)}],
_ = [kz_util:delete_file(F) || F <- [TmpPath, OrigPath, TonePath]],
%%TODO: update forwarded doc with length and media_filename
try_put_fwd_attachment(AccountId, ForwardId, JoinFilename, FileContents, 3);
try_put_fwd_attachment(AccountId, ForwardId, JoinFilename, FileContents, FileProps, 3);
{'error', _} ->
_ = [kz_util:delete_file(F) || F <- [TmpPath, OrigPath, TonePath]],
lager:warning("failed to join forward message media files"),
{'error', 'join_failed'}

end.

-spec try_put_fwd_attachment(kz_term:ne_binary(), kz_term:ne_binary(), kz_term:ne_binary(), iodata(), 1..3) -> db_ret().
try_put_fwd_attachment(AccountId, ForwardId, _JoinFilename, _FileContents, 0) ->
-spec try_put_fwd_attachment(kz_term:ne_binary(), kz_term:ne_binary(), kz_term:ne_binary(), iodata(), kz_term:proplist(), 1..3) -> db_ret().
try_put_fwd_attachment(AccountId, ForwardId, _JoinFilename, _FileContents, _FileProps, 0) ->
lager:error("max retries to save prepend forward voicemail attachment ~s in db ~s"
,[ForwardId, kvm_util:get_db(AccountId, ForwardId)]
),
{'error', 'max_save_retries'};
try_put_fwd_attachment(AccountId, ForwardId, JoinFilename, FileContents, Loop) ->
case kz_datamgr:put_attachment(kvm_util:get_db(AccountId, ForwardId), ForwardId, JoinFilename, FileContents) of
try_put_fwd_attachment(AccountId, ForwardId, JoinFilename, FileContents, FileProps, Loop) ->
case kz_datamgr:put_attachment(kvm_util:get_db(AccountId, ForwardId), ForwardId, JoinFilename, FileContents, FileProps) of
{'ok', _}=OK -> OK;
{'error', 'conflict'} ->
try_put_fwd_attachment(AccountId, ForwardId, JoinFilename, FileContents, Loop - 1);
try_put_fwd_attachment(AccountId, ForwardId, JoinFilename, FileContents, FileProps, Loop - 1);
{'error', 'timeout'} ->
try_put_fwd_attachment(AccountId, ForwardId, JoinFilename, FileContents, Loop - 1);
try_put_fwd_attachment(AccountId, ForwardId, JoinFilename, FileContents, FileProps, Loop - 1);
{'error', _Reason}=Error ->
lager:error("failed to save prepend forward voicemail message ~s in db ~s : ~p"
,[ForwardId, kvm_util:get_db(AccountId, ForwardId), _Reason]),
Expand Down

0 comments on commit 44c449e

Please sign in to comment.