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 -
- (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];
}

View file

@ -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.
*/

View file

@ -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];