From be7696ce5572fc28368bc57fef16847104123c57 Mon Sep 17 00:00:00 2001 From: Marc O'Morain Date: Wed, 7 May 2014 21:58:59 +0100 Subject: [PATCH] Change API to use NSURL rather than NSString --- .gitignore | 1 + AFAmazonS3Client/AFAmazonS3Manager.h | 4 +- AFAmazonS3Client/AFAmazonS3Manager.m | 74 +++++++++++++--------------- 3 files changed, 38 insertions(+), 41 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..496ee2c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store \ No newline at end of file diff --git a/AFAmazonS3Client/AFAmazonS3Manager.h b/AFAmazonS3Client/AFAmazonS3Manager.h index 169f26a..544aada 100644 --- a/AFAmazonS3Client/AFAmazonS3Manager.h +++ b/AFAmazonS3Client/AFAmazonS3Manager.h @@ -171,7 +171,7 @@ @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the response object from the server. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the `NSError` object describing error that occurred. */ -- (void)postObjectWithFile:(NSString *)path +- (void)postObjectWithFile:(NSURL *)path destinationPath:(NSString *)destinationPath parameters:(NSDictionary *)parameters progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress @@ -188,7 +188,7 @@ @param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the response object from the server. @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the `NSError` object describing error that occurred. */ -- (void)putObjectWithFile:(NSString *)path +- (void)putObjectWithFile:(NSURL *)path destinationPath:(NSString *)destinationPath parameters:(NSDictionary *)parameters progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress diff --git a/AFAmazonS3Client/AFAmazonS3Manager.m b/AFAmazonS3Client/AFAmazonS3Manager.m index 1d89477..80938bf 100644 --- a/AFAmazonS3Client/AFAmazonS3Manager.m +++ b/AFAmazonS3Client/AFAmazonS3Manager.m @@ -26,6 +26,7 @@ @interface AFAmazonS3Manager () @property (readwrite, nonatomic, strong) NSURL *baseURL; +@property (readwrite, nonatomic) NSTimeInterval sendTimeout; @end @implementation AFAmazonS3Manager @@ -39,6 +40,7 @@ - (instancetype)initWithBaseURL:(NSURL *)url { self.requestSerializer = [AFAmazonS3RequestSerializer serializer]; self.responseSerializer = [AFXMLParserResponseSerializer serializer]; + self.sendTimeout = 30; return self; } @@ -52,7 +54,6 @@ - (id)initWithAccessKeyID:(NSString *)accessKey } [self.requestSerializer setAccessKeyID:accessKey secret:secret]; - return self; } @@ -184,7 +185,7 @@ - (void)getObjectWithPath:(NSString *)path [self.operationQueue addOperation:requestOperation]; } -- (void)postObjectWithFile:(NSString *)path +- (void)postObjectWithFile:(NSURL *)path destinationPath:(NSString *)destinationPath parameters:(NSDictionary *)parameters progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress @@ -194,7 +195,7 @@ - (void)postObjectWithFile:(NSString *)path [self setObjectWithMethod:@"POST" file:path destinationPath:destinationPath parameters:parameters progress:progress success:success failure:failure]; } -- (void)putObjectWithFile:(NSString *)path +- (void)putObjectWithFile:(NSURL *)path destinationPath:(NSString *)destinationPath parameters:(NSDictionary *)parameters progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress @@ -212,49 +213,44 @@ - (void)deleteObjectWithPath:(NSString *)path } - (void)setObjectWithMethod:(NSString *)method - file:(NSString *)filePath + file:(NSURL *)fileUrl destinationPath:(NSString *)destinationPath parameters:(NSDictionary *)parameters progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure { - NSMutableURLRequest *fileRequest = [NSMutableURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]; - [fileRequest setCachePolicy:NSURLCacheStorageNotAllowed]; - - NSURLResponse *response = nil; - NSError *fileError = nil; - NSData *data = [NSURLConnection sendSynchronousRequest:fileRequest returningResponse:&response error:&fileError]; - - if (data && response) { - NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:method URLString:[[self.baseURL URLByAppendingPathComponent:destinationPath] absoluteString] parameters:parameters constructingBodyWithBlock:^(id formData) { - if (![parameters valueForKey:@"key"]) { - [formData appendPartWithFormData:[[filePath lastPathComponent] dataUsingEncoding:NSUTF8StringEncoding] name:@"key"]; - } - [formData appendPartWithFileData:data name:@"file" fileName:[filePath lastPathComponent] mimeType:[response MIMEType]]; - } error:nil]; - -// NSURL *temporaryFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSUUID UUID] UUIDString]]]; -// request = [self.requestSerializer requestWithMultipartFormRequest:request writingStreamContentsToFile:temporaryFileURL completionHandler:^(NSError *error) { -// if (!error) { -// [request setHTTPBody:[NSData dataWithContentsOfFile:[temporaryFileURL absoluteString]]]; -// } -// }]; - - AFHTTPRequestOperation *requestOperation = [self HTTPRequestOperationWithRequest:request success:^(__unused AFHTTPRequestOperation *operation, id responseObject) { - if (success) { - success(responseObject); - } - } failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) { - if (failure) { - failure(error); - } - }]; - - [requestOperation setUploadProgressBlock:progress]; - - [self.operationQueue addOperation:requestOperation]; + + NSError* requestError = nil; + NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method + URLString:[[self.baseURL URLByAppendingPathComponent:destinationPath] absoluteString] + parameters:parameters + error:&requestError]; + + if (requestError && failure) { + failure(requestError); + return; } + + [request setHTTPBody:[NSData dataWithContentsOfURL:fileUrl]]; + [request setTimeoutInterval:self.sendTimeout]; + + AFHTTPRequestOperation *requestOperation = [self HTTPRequestOperationWithRequest:request success:^(__unused AFHTTPRequestOperation *operation, id responseObject) { + if (success) { + success(responseObject); + } + } failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) { + if (failure) { + NSLog(@"Operation: %@", operation); + NSLog(@"Response String: %@", operation.responseString); + failure(error); + return; + } + }]; + + [requestOperation setUploadProgressBlock:progress]; + + [self.operationQueue addOperation:requestOperation]; } #pragma mark - NSKeyValueObserving