diff --git a/lib/NGCP/Panel/Controller/API/VoicemailGreetingsItem.pm b/lib/NGCP/Panel/Controller/API/VoicemailGreetingsItem.pm index 07d3176c4e..24da08a9d3 100644 --- a/lib/NGCP/Panel/Controller/API/VoicemailGreetingsItem.pm +++ b/lib/NGCP/Panel/Controller/API/VoicemailGreetingsItem.pm @@ -1,7 +1,12 @@ package NGCP::Panel::Controller::API::VoicemailGreetingsItem; -use Sipwise::Base; use NGCP::Panel::Utils::Generic qw(:all); + +use Sipwise::Base; + +use NGCP::Panel::Utils::Subscriber; +use NGCP::Panel::Utils::Sounds; + use parent qw/NGCP::Panel::Role::EntitiesItem NGCP::Panel::Role::API::VoicemailGreetings/; __PACKAGE__->set_config({ @@ -9,16 +14,17 @@ __PACKAGE__->set_config({ 'ContentType' => ['multipart/form-data'],#, 'Uploads' => {'greetingfile' => ['audio/x-wav', 'application/octet-stream']}, }, + GET => { + #'application/json' is default, if no accept header was recieved. + 'ReturnContentType' => ['application/json', 'audio/x-wav', 'audio/mpeg', 'audio/ogg'],#, + }, + allowed_roles => [qw/admin reseller subscriberadmin subscriber/], }); sub allowed_methods{ return [qw/GET OPTIONS HEAD PUT DELETE/]; } -sub config_allowed_roles { - return [qw/admin reseller subscriberadmin subscriber/]; -} - sub update_item_model{ my($self, $c, $item, $old_resource, $resource, $form, $process_extras) = @_; @@ -37,10 +43,18 @@ sub update_item_model{ } sub get_item_binary_data{ - my($self, $c, $id, $item) = @_; + my($self, $c, $id, $item, $return_type) = @_; #caller waits for: $data_ref,$mime_type,$filename #while we will not strictly check Accepted header, if item can return only one type of the binary data - return \$item->recording, 'audio/x-wav', 'voicemail_'. NGCP::Panel::Utils::Subscriber::get_subscriber_voicemail_type( dir => $item->dir ).'_'.$item->get_column('subscriber_id').'.wav', + my $extension = mime_type_to_extension($return_type); + my $data_ref; + if ($extension ne 'wav') { + $data_ref = NGCP::Panel::Utils::Sounds::transcode_data(\$item->recording, 'WAV', uc($extension)); + } else { + $data_ref = \$item->recording; + } + my $filename = 'voicemail_'. NGCP::Panel::Utils::Subscriber::get_subscriber_voicemail_type( dir => $item->dir ).'_'.$item->get_column('subscriber_id').'.'.$extension; + return $data_ref, $return_type, $filename; } 1; diff --git a/t/api-rest/api-voicemailgreetings.t b/t/api-rest/api-voicemailgreetings.t index b1eed99616..535c8abcdd 100644 --- a/t/api-rest/api-voicemailgreetings.t +++ b/t/api-rest/api-voicemailgreetings.t @@ -9,6 +9,8 @@ use Data::Dumper; use File::Basename; use File::Slurp qw/read_file write_file/; use File::Temp; +use Clone qw/clone/; + #init test_machine my $fake_data = Test::FakeData->new; @@ -36,6 +38,11 @@ $test_machine->methods->{collection}->{allowed} = {map {$_ => 1} qw(GET HEAD OPT $test_machine->methods->{item}->{allowed} = {map {$_ => 1} qw(GET HEAD OPTIONS PUT DELETE)}; +my $uploaded_path = $test_machine->DATA_ITEM_STORE->{greetingfile}->[0]; +my $dir = File::Temp->newdir(undef, CLEANUP => 0); +my $tempdir = $dir->dirname; + + #$test_machine->form_data_item( sub {$_[0]->{json}->{dir} = $dir;} ); $test_machine->form_data_item(); @@ -49,25 +56,23 @@ is($greeting->{content}->{dir}, $test_machine->DATA_ITEM->{json}->{dir}, "Check $res = $test_machine->request_get('/api/voicemailgreetings/?subscriber_id='.$greeting->{content}->{subscriber_id}.'&type='.$greeting->{content}->{dir}); $test_machine->http_code_msg(200, "check subscriber_id and type filters", $res); +my ($expected_downloaded_name,$downloaded_path,$soxi_output); -$res = $test_machine->request_get($greeting->{location}, undef, { - 'Accept' => 'audio/x-wav', -}); -$test_machine->http_code_msg(200, "check download voicemail greeting", $res); - - -my $expected_downloaded_name = "voicemail_".$greeting->{content}->{dir}."_".$greeting->{content}->{subscriber_id}.".wav"; -is($res->filename, $expected_downloaded_name ,"Check downloaded file name: $expected_downloaded_name ."); -ok(length($res->content)>0,"Check length of the downloaded file > 0 :".length($res->content)); - -my $uploaded_path = $test_machine->DATA_ITEM_STORE->{greetingfile}->[0]; - -my $dir = File::Temp->newdir(undef, CLEANUP => 0); -my $tempdir = $dir->dirname; -my $downloaded_path = $tempdir.'/'.$expected_downloaded_name; -write_file( $downloaded_path , {binmode => ':raw'}, $res->content); -my $soxi_output = `soxi -e $downloaded_path`; -$soxi_output=~s/\n//g; +{ + $res = $test_machine->request_get($greeting->{location}, undef, { + 'Accept' => 'audio/x-wav', + }); + $test_machine->http_code_msg(200, "check download voicemail greeting", $res); + + $expected_downloaded_name = "voicemail_".$greeting->{content}->{dir}."_".$greeting->{content}->{subscriber_id}.".wav"; + is($res->filename, $expected_downloaded_name ,"Check downloaded file name: $expected_downloaded_name ."); + ok(length($res->content)>0,"Check length of the downloaded file > 0 :".length($res->content)); + + $downloaded_path = $tempdir.'/'.$expected_downloaded_name; + write_file( $downloaded_path , {binmode => ':raw'}, $res->content); + $soxi_output = `soxi -e $downloaded_path`; + $soxi_output=~s/\n//g; +} if(ok($soxi_output =~/GSM/, "Check that we converted wav to GSM encoding:".$soxi_output)){ my $soxi_output_original = `soxi -e $uploaded_path`; @@ -79,6 +84,7 @@ if(ok($soxi_output =~/GSM/, "Check that we converted wav to GSM encoding:".$soxi 'json' => JSON::to_json($test_machine->DATA_ITEM->{json}), greetingfile => [ $downloaded_path ], ] ); + my $put_content = clone $content; $test_machine->http_code_msg(200, "check download voicemail greeting after put", $res, $content); ok(is_int($content->{id}),"Check id presence after editing."); ok(is_int($content->{subscriber_id}),"Check subscriber_id presence after editing."); @@ -93,6 +99,8 @@ if(ok($soxi_output =~/GSM/, "Check that we converted wav to GSM encoding:".$soxi }); $test_machine->http_code_msg(200, "check download voicemail greeting after put", $res); is(length($res_download->content), length($uploaded_content),"Check length of the downloaded file: ".length($uploaded_content)."<=>".length($res_download->content)); + #we put some values, so let refresh greeting + $greeting->{content} = $put_content; } } { @@ -104,6 +112,29 @@ if(ok($soxi_output =~/GSM/, "Check that we converted wav to GSM encoding:".$soxi } ); $test_machine->http_code_msg(422, "check response code on put empty file", $res_put_empty, $content_put_empty); } +my $audio_types = { + 'wav' => 'audio/x-wav', + 'mp3' => 'audio/mpeg', + 'ogg' => 'audio/ogg', +}; +foreach my $extension (keys %$audio_types) { +#'audio/x-wav', 'audio/mpeg', 'audio/ogg'] + $res = $test_machine->request_get($greeting->{location}, undef, { + #'Accept' => 'audio/x-wav', + 'Accept' => $audio_types->{$extension}, + }); + $test_machine->http_code_msg(200, "check download voicemail greeting", $res); + + $expected_downloaded_name = "voicemail_".$greeting->{content}->{dir}."_".$greeting->{content}->{subscriber_id}.".".$extension; + is($res->filename, $expected_downloaded_name ,"Check downloaded file name: $expected_downloaded_name ."); + ok(length($res->content)>0,"Check length of the downloaded file > 0 :".length($res->content)); + + $downloaded_path = $tempdir.'/'.$expected_downloaded_name; + write_file( $downloaded_path , {binmode => ':raw'}, $res->content); + $soxi_output = `soxi -e $downloaded_path`; + $soxi_output=~s/\n//g; +} + $test_machine->check_bundle(); $test_machine->check_item_delete($greeting->{location});