Skip to content

Instantly share code, notes, and snippets.

@bfaist
Created February 29, 2016 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfaist/93e8de6a4846f0497c06 to your computer and use it in GitHub Desktop.
Save bfaist/93e8de6a4846f0497c06 to your computer and use it in GitHub Desktop.
iOS Audio File and POST to Mojolicious Web Service
package MyApp::Controller::Audio;
use Mojo::Base 'Mojolicious::Controller';
sub save {
my $self = shift;
# get contents of the audio upload in memory
my $upload_audio_asset = $self->tx->req->content->asset;
my $save_audio_dir = '/path/to/audio/files';
# build save audio path
my $upload_audio_file = "$save_audio_dir/audio_upload.wav";
# move to final location
$upload_audio_asset->move_to($upload_audio_file);
$self->render(json => { status => 'OK', audio_filename => $upload_audio_file });
}
// load settings.plist file
NSString* path = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
NSDictionary* settings = [[NSDictionary alloc] initWithContentsOfFile:path];
NSString* upload_audio_url = [settings objectForKey:@"upload_audio_url"];
NSURL *upload_url = [NSURL URLWithString:upload_audio_url];
// setup POST request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:upload_url];
[request setHTTPMethod:@"POST"];
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
[request setTimeoutInterval:120.0];
[request setValue:@"audio/x-wav;codec=pcm;bit=16;rate=16000" forHTTPHeaderField:@"Content-Type"];
// new session with defaults and use main queue
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config
delegate:nil
delegateQueue:[NSOperationQueue mainQueue]];
// POST audio file
NSURLSessionUploadTask *uploadTask =
[session uploadTaskWithRequest:request
fromFile:self.soundFileURL
completionHandler:^(NSData *data, NSURLResponse *response,NSError *error) {
// Handle response here
if(error) {
} else {
}
}];
[uploadTask resume];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment