Last active
October 26, 2016 05:35
-
-
Save elliotrock/70a71819fd7e01da3567 to your computer and use it in GitHub Desktop.
pandaStream authentication upload - objective-c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PandaStreamServices.m | |
// apis added for telestream | |
// | |
// Created by Elliot Rock on 1/03/2015. | |
// Copyright (c) 2015 Pocketry Pty Ltd. All rights reserved. | |
// | |
#import "PandaStreamServices.h" | |
#import "KTZConfig.h" | |
#import <CommonCrypto/CommonHMAC.h> | |
//#import "BMTwitterVideoUpload.h" | |
@interface PandaStreamServices () | |
@property (nonatomic) int retry; | |
@property (nonatomic) NSString *filePath; | |
@property (nonatomic) NSString *fileName; | |
@end | |
@implementation PandaStreamServices | |
-(id) init | |
{ | |
self=[super init]; | |
if(self){ | |
_retry=0; | |
} | |
return self; | |
} | |
-(void) uploadPandaVideoForUrl:(NSString*)filePath forName:(NSString*)fileName | |
{ | |
//[[StateModel stateModel] setIsVideoUploading:YES]; | |
_fileName=fileName; | |
_filePath=filePath; | |
[[[StateModel stateModel] videoUploadProgress] setValue:@(0) forKey:@"progress"]; | |
[[[StateModel stateModel] videoUploadProgress] setValue:@(0) forKey:@"total"]; | |
// Note: these need to match what is on cloud.telestream | |
NSString *profiles=@"hls.ios.cell.240k, hls.ios.cell.464k, hls.ios.cell.664k, hls.ios.wifi.1296k, hls.ios.wifi.3596k, hls.ios.wifi.5128k, ios_cell, hls.audio, h264.hi, preview"; | |
timeStamp=[self getTimestamp]; | |
NSLog(@"timeStamp: %@",timeStamp); | |
//upload.json | |
NSString *string_to_sign = [NSString stringWithFormat:@"POST\n%@\n/videos.json\naccess_key=%@&cloud_id=%@&profiles=%@×tamp=%@", PANDA_API_HOST, PANDA_ACCESS_KEY, PANDA_CLOUD_ID, [self urlEncodeString:profiles], [self urlEncodeString:timeStamp] ]; | |
// sign with the querystring and secret key | |
signature=[self signWithKey:PANDA_SECRET_KEY usingData:string_to_sign]; | |
NSLog(@"signature: %@",signature); | |
NSDictionary *queryParams = @{@"access_key" : PANDA_ACCESS_KEY, | |
@"cloud_id" : PANDA_CLOUD_ID, | |
@"profiles" : profiles, | |
@"signature" : signature, | |
@"timestamp" : timeStamp }; | |
NSLog(@"filePath: %@",_filePath); | |
NSLog(@"fileName: %@",_fileName); | |
NSLog(@"pandaStream queryParams: %@",[queryParams description]); | |
NSData *videoData; | |
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) | |
{ | |
videoData = [NSData dataWithContentsOfFile:filePath]; | |
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"https://api.cloud.telestream.net/videos.json" parameters:queryParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { | |
[formData appendPartWithFileData:videoData name:@"file" fileName:_fileName mimeType:@"video/quicktime"]; | |
} error:nil]; | |
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; | |
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request | |
success:^(AFHTTPRequestOperation *operation, id responseObject) { | |
NSLog(@"PandaStream Success %@", responseObject); | |
[self didCompleteUpload:responseObject]; | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
NSLog(@"PandaStream Failure %@", error.description); | |
[self performSelector:@selector(didFailUploadRetry) withObject:self afterDelay:2 ]; | |
}]; | |
[operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten, | |
long long totalBytesWritten, | |
long long totalBytesExpectedToWrite) { | |
NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite); | |
// yes where every the progress is. | |
//[[[StateModel stateModel] videoUploadProgress] setValue:@(totalBytesWritten) forKey:@"progress"]; | |
//[[[StateModel stateModel] videoUploadProgress] setValue:@(totalBytesExpectedToWrite) forKey:@"total"]; | |
}]; | |
[operation start]; | |
} else { | |
NSLog(@"error: no video file, halt"); | |
} | |
} | |
-(void) sendVideoToTwitter | |
{ | |
} | |
-(void) didCompleteUpload:(id) response | |
{ | |
// capture the video object id [response objectForKey:@"id"] | |
//[[StateModel stateModel] setIsVideoUploading:NO]; | |
//[[[DataModel dataModel] capturedVideo] setObject:[response objectForKey:@"id"] forKey:@"object_uid"]; | |
// | |
//[[DataModel dataModel] addEncodingVideoFromCurrentUser]; | |
[[NSNotificationCenter defaultCenter] postNotificationName:@"removeUploadProgressNotification" object:nil]; | |
[self removeVideoData]; | |
} | |
-(void) removeVideoData | |
{ | |
NSString *lastRecordedVideoFile=[[[[DataModel dataModel] capturedVideo] objectForKey:@"input_url"] description]; | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
if ([fileManager fileExistsAtPath:[lastRecordedVideoFile substringFromIndex:7] ]) { | |
NSError *error; | |
// Attempt to delete the folder containing globalDel.videoPath | |
if ([fileManager removeItemAtPath:[lastRecordedVideoFile substringFromIndex:7] error:&error] != YES) { | |
NSLog(@"Unable to delete recorded file: %@", [error localizedDescription]); | |
} else { | |
NSLog(@"deleted recorded file"); | |
} | |
} | |
if ([fileManager fileExistsAtPath:_filePath ]) { | |
NSError *error; | |
// Attempt to delete the folder containing globalDel.videoPath | |
if ([fileManager removeItemAtPath:[_filePath stringByDeletingLastPathComponent] error:&error] != YES) { | |
NSLog(@"Unable to delete file: %@", [error localizedDescription]); | |
} else { | |
NSLog(@"deleted file"); | |
} | |
} | |
} | |
-(void) didFailUploadRetry | |
{ | |
if(_retry<5) | |
{ | |
_retry++; | |
[self uploadPandaVideoForUrl:_filePath forName:_fileName]; | |
} | |
} | |
-(NSString *)signWithKey:(NSString *)key usingData:(NSString *)data | |
{ | |
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding]; | |
const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding]; | |
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH]; | |
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC); | |
NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)]; | |
NSString *hash = [HMAC base64EncodedStringWithOptions:0]; | |
return hash; | |
} | |
-(NSString*) urlEncodeString:(NSString*) value | |
{ | |
NSString * encodedString = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge_retained CFStringRef)value, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 ); | |
return encodedString; | |
} | |
-(NSString*) getTimestamp | |
{ | |
NSDate* datetime = [NSDate date]; | |
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; | |
//2009-10-15T15:38:42+01:00 | |
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:SS"]; | |
NSString* _timeStamp = [dateFormatter stringFromDate:datetime]; | |
[dateFormatter setDateFormat:@"ZZZ"]; | |
NSString *offset= [NSString stringWithFormat:@"%@:%@",[[dateFormatter stringFromDate:datetime] substringToIndex:3],[[dateFormatter stringFromDate:datetime] substringFromIndex:3]]; | |
NSString * returnStr=[NSString stringWithFormat:@"%@%@",_timeStamp,offset]; | |
return returnStr; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment