create and set temporary path

This commit is contained in:
Peter Steinberger 2012-04-06 03:00:33 -07:00
parent 2413d95c3f
commit 24564772df
3 changed files with 35 additions and 6 deletions

View file

@ -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 - #pragma mark -
- (void)setDestination:(NSString *)path allowOverwrite:(BOOL)allowOverwrite { - (void)setDestination:(NSString *)path allowOverwrite:(BOOL)allowOverwrite {
[self willChangeValueForKey:@"isReady"];
self.destination = path; self.destination = path;
self.allowOverwrite = allowOverwrite; self.allowOverwrite = allowOverwrite;
[self didChangeValueForKey:@"isReady"];
} }
#pragma mark - NSOperation #pragma mark - NSOperation
@ -70,8 +78,8 @@
- (void)start { - (void)start {
if ([self isReady]) { if ([self isReady]) {
// TODO Create temporary path NSString *temporaryPath = [self temporaryPath];
self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.destination append:NO]; self.outputStream = [NSOutputStream outputStreamToFileAtPath:temporaryPath append:NO];
[super start]; [super start];
} }

View file

@ -28,6 +28,8 @@
*/ */
extern NSSet * AFContentTypesFromHTTPHeader(NSString *string); 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. `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.
*/ */

View file

@ -90,6 +90,24 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
return string; 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 - #pragma mark -
@interface AFHTTPRequestOperation () @interface AFHTTPRequestOperation ()
@ -154,7 +172,7 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
} else { } else {
offset = [[self.outputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey] length]; offset = [[self.outputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey] length];
} }
NSMutableURLRequest *mutableURLRequest = [[self.request mutableCopy] autorelease]; NSMutableURLRequest *mutableURLRequest = [[self.request mutableCopy] autorelease];
if ([[self.response allHeaderFields] valueForKey:@"ETag"]) { if ([[self.response allHeaderFields] valueForKey:@"ETag"]) {
[mutableURLRequest setValue:[[self.response allHeaderFields] valueForKey:@"ETag"] forHTTPHeaderField:@"If-Range"]; [mutableURLRequest setValue:[[self.response allHeaderFields] valueForKey:@"ETag"] forHTTPHeaderField:@"If-Range"];
@ -178,7 +196,7 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
if (_successCallbackQueue) { if (_successCallbackQueue) {
dispatch_release(_successCallbackQueue); dispatch_release(_successCallbackQueue);
} }
if (successCallbackQueue) { if (successCallbackQueue) {
dispatch_retain(successCallbackQueue); dispatch_retain(successCallbackQueue);
_successCallbackQueue = successCallbackQueue; _successCallbackQueue = successCallbackQueue;
@ -264,6 +282,7 @@ didReceiveResponse:(NSURLResponse *)response
{ {
self.response = (NSHTTPURLResponse *)response; self.response = (NSHTTPURLResponse *)response;
// 206 = Partial Content.
if ([self.response statusCode] != 206) { if ([self.response statusCode] != 206) {
if ([self.outputStream propertyForKey:NSStreamFileCurrentOffsetKey]) { if ([self.outputStream propertyForKey:NSStreamFileCurrentOffsetKey]) {
[self.outputStream setProperty:[NSNumber numberWithInteger:0] forKey:NSStreamFileCurrentOffsetKey]; [self.outputStream setProperty:[NSNumber numberWithInteger:0] forKey:NSStreamFileCurrentOffsetKey];