diff --git a/AFNetworking/AFDownloadRequestOperation.m b/AFNetworking/AFDownloadRequestOperation.m index 9e3888c..cba1714 100644 --- a/AFNetworking/AFDownloadRequestOperation.m +++ b/AFNetworking/AFDownloadRequestOperation.m @@ -53,13 +53,21 @@ } } + +- (NSString *)temporaryPath { + NSString *temporaryPath = nil; + if (self.destination) { + NSString *hashString = [NSString stringWithFormat:@"%d", [self.destination hash]]; + temporaryPath = [AFCreateIncompleteDownloadDirectoryPath() stringByAppendingPathComponent:hashString]; + } + return temporaryPath; +} + #pragma mark - - (void)setDestination:(NSString *)path allowOverwrite:(BOOL)allowOverwrite { - [self willChangeValueForKey:@"isReady"]; self.destination = path; self.allowOverwrite = allowOverwrite; - [self didChangeValueForKey:@"isReady"]; } #pragma mark - NSOperation @@ -70,8 +78,8 @@ - (void)start { if ([self isReady]) { - // TODO Create temporary path - self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.destination append:NO]; + NSString *temporaryPath = [self temporaryPath]; + self.outputStream = [NSOutputStream outputStreamToFileAtPath:temporaryPath append:NO]; [super start]; } diff --git a/AFNetworking/AFHTTPRequestOperation.h b/AFNetworking/AFHTTPRequestOperation.h index 4b6a63e..28c8639 100644 --- a/AFNetworking/AFHTTPRequestOperation.h +++ b/AFNetworking/AFHTTPRequestOperation.h @@ -28,6 +28,8 @@ */ extern NSSet * AFContentTypesFromHTTPHeader(NSString *string); +extern NSString * AFCreateIncompleteDownloadDirectoryPath(void); + /** `AFHTTPRequestOperation` is a subclass of `AFURLConnectionOperation` for requests using the HTTP or HTTPS protocols. It encapsulates the concept of acceptable status codes and content types, which determine the success or failure of a request. */ diff --git a/AFNetworking/AFHTTPRequestOperation.m b/AFNetworking/AFHTTPRequestOperation.m index be53c61..49247a8 100644 --- a/AFNetworking/AFHTTPRequestOperation.m +++ b/AFNetworking/AFHTTPRequestOperation.m @@ -90,6 +90,24 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) { return string; } +NSString * AFCreateIncompleteDownloadDirectoryPath(void) { + static NSString *incompleteDownloadPath; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + NSString *tempDirectory = NSTemporaryDirectory(); + incompleteDownloadPath = [[tempDirectory stringByAppendingPathComponent:kAFNetworkingIncompleteDownloadDirectoryName] retain]; + + NSError *error = nil; + NSFileManager *fileMan = [[NSFileManager alloc] init]; + if(![fileMan createDirectoryAtPath:incompleteDownloadPath withIntermediateDirectories:YES attributes:nil error:&error]) { + NSLog(@"Failed to create incomplete downloads directory at %@", incompleteDownloadPath); + } + [fileMan release]; + }); + + return incompleteDownloadPath; +} + #pragma mark - @interface AFHTTPRequestOperation () @@ -154,7 +172,7 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) { } else { offset = [[self.outputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey] length]; } - + NSMutableURLRequest *mutableURLRequest = [[self.request mutableCopy] autorelease]; if ([[self.response allHeaderFields] valueForKey:@"ETag"]) { [mutableURLRequest setValue:[[self.response allHeaderFields] valueForKey:@"ETag"] forHTTPHeaderField:@"If-Range"]; @@ -178,7 +196,7 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) { if (_successCallbackQueue) { dispatch_release(_successCallbackQueue); } - + if (successCallbackQueue) { dispatch_retain(successCallbackQueue); _successCallbackQueue = successCallbackQueue; @@ -264,6 +282,7 @@ didReceiveResponse:(NSURLResponse *)response { self.response = (NSHTTPURLResponse *)response; + // 206 = Partial Content. if ([self.response statusCode] != 206) { if ([self.outputStream propertyForKey:NSStreamFileCurrentOffsetKey]) { [self.outputStream setProperty:[NSNumber numberWithInteger:0] forKey:NSStreamFileCurrentOffsetKey];