From 1abf1a0c2a0b28827e2f65e5b82ca7b60849cbcf Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Thu, 31 Mar 2016 15:09:06 +0200 Subject: [PATCH] Upload implemented --- .../xcdebugger/Breakpoints_v2.xcbkptlist | 18 +++++++ FMC Planner 2/DropboxV2ObjC.h | 2 +- FMC Planner 2/DropboxV2ObjC.m | 54 ++++++++++++++++++- FMC Planner 2/ViewControllerScreen.m | 2 +- FMC Planner 2/ViewControllerServiceMenu.m | 7 +-- 5 files changed, 73 insertions(+), 10 deletions(-) diff --git a/FMC Planner 2.xcodeproj/xcuserdata/Kili2.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/FMC Planner 2.xcodeproj/xcuserdata/Kili2.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index fe2b454..13067ff 100644 --- a/FMC Planner 2.xcodeproj/xcuserdata/Kili2.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/FMC Planner 2.xcodeproj/xcuserdata/Kili2.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -2,4 +2,22 @@ + + + + + + diff --git a/FMC Planner 2/DropboxV2ObjC.h b/FMC Planner 2/DropboxV2ObjC.h index 44a347e..8df77de 100644 --- a/FMC Planner 2/DropboxV2ObjC.h +++ b/FMC Planner 2/DropboxV2ObjC.h @@ -28,7 +28,7 @@ #pragma mark - File and directory methods - (void)downloadFromDropbox:(NSArray *)files; -- (void)uploadToDropbox:(NSData *)data; +- (void)uploadToDropbox:(NSArray *)files; - (NSArray *)contentsOfPath:(NSString *)path; #pragma mark - Other methods diff --git a/FMC Planner 2/DropboxV2ObjC.m b/FMC Planner 2/DropboxV2ObjC.m index 8b86da3..e9d3d28 100644 --- a/FMC Planner 2/DropboxV2ObjC.m +++ b/FMC Planner 2/DropboxV2ObjC.m @@ -25,6 +25,7 @@ } #pragma mark - OAuth stuff +// TODO: Alert on error - (BOOL)authorizeUserWithToke:(NSURL *)token completion:(void (^)(void))handler { NSScanner *scan = [NSScanner scannerWithString:token.absoluteString]; @@ -52,6 +53,7 @@ return true; } +// TODO: Alert on error - (void)deauthorizeUser { NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; @@ -96,7 +98,8 @@ * Pass nil as path for root dir * @return Content of directory at path or nil if error */ -#warning make async +// !!!: Cannot be asynchronous +// TODO: Alert on error - (NSArray *)contentsOfPath:(NSString *)path { @@ -143,6 +146,7 @@ } } +// TODO: Alert on error - (void)downloadFromDropbox:(NSArray *)files { NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; @@ -189,9 +193,55 @@ } } +// TODO: Alert on error +- (void)uploadToDropbox:(NSArray *)files +{ + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; + request.URL = + [NSURL URLWithString:@"https://content.dropboxapi.com/2/files/upload"]; + [request addValue:[NSString stringWithFormat:@"Bearer %@", _token] + .precomposedStringWithCanonicalMapping + forHTTPHeaderField:(@"Authorization") + .precomposedStringWithCanonicalMapping]; + [request addValue:(@"application/octet-stream") + .precomposedStringWithCanonicalMapping + forHTTPHeaderField:(@"Content-Type")]; + request.HTTPMethod = @"POST"; + + // File Path + for (NSString *string in files) { + NSData *data = [NSJSONSerialization dataWithJSONObject:@{ + @"path" : [NSString stringWithFormat:@"/%@", string] + .precomposedStringWithCanonicalMapping, + @"mode" : @"overwrite".precomposedStringWithCanonicalMapping, + } + options:0 + error:nil]; + NSString *temp = + [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; + temp = + [temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"]; + + [request addValue:temp.precomposedStringWithCanonicalMapping + forHTTPHeaderField:(@"Dropbox-API-Arg") + .precomposedStringWithCanonicalMapping]; + + request.HTTPBody = [NSData + dataWithContentsOfFile:[SharedDeclerations savePathForFile:string]]; + + [NSURLConnection + sendAsynchronousRequest:request + queue:[[NSOperationQueue alloc] init] + completionHandler:^(NSURLResponse *response, NSData *data, + NSError *error){ + }]; + } +} + #pragma mark - Other operations -#warning make async +// !!!: Cannot be asynchronous +// TODO: Alert on error - (NSDictionary *)getUserInfo { NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; diff --git a/FMC Planner 2/ViewControllerScreen.m b/FMC Planner 2/ViewControllerScreen.m index e2876b3..8757b58 100644 --- a/FMC Planner 2/ViewControllerScreen.m +++ b/FMC Planner 2/ViewControllerScreen.m @@ -110,7 +110,7 @@ } else if ([((UILabel *)tapReg.view) .restorationIdentifier isEqualToString:@"LSKR5"]) { -#warning EXPORT TO PMDG GOES HERE + // TODO: Export to PMDG route format } else if ([((UILabel *)tapReg.view) .restorationIdentifier isEqualToString:@"LSKR6"]) { diff --git a/FMC Planner 2/ViewControllerServiceMenu.m b/FMC Planner 2/ViewControllerServiceMenu.m index 30bc250..c0a0506 100644 --- a/FMC Planner 2/ViewControllerServiceMenu.m +++ b/FMC Planner 2/ViewControllerServiceMenu.m @@ -94,12 +94,7 @@ DropboxV2ObjC *dbClient = ((AppDelegate *)[UIApplication sharedApplication].delegate).dbClient; NSLog(@"%@", [dbClient contentsOfPath:nil]); - [dbClient downloadFromDropbox:@[ - @"lol.txt", - @"rofl.txt", - @"tldr.txt", - @"lol Kopie 3.txt" - ]]; + [dbClient uploadToDropbox:@[ @"EDDFEDDM001" ]]; } @end