Created
March 7, 2013 22:49
-
-
Save rustle/5112576 to your computer and use it in GitHub Desktop.
Example of an ESHTTPOperation subclass for downloading a file directly to disk using NSOutputStream including resuming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// KATGDownloadOperation.h | |
// KATG | |
// | |
// Created by Doug Russell on 3/7/13. | |
// Copyright (c) 2013 Doug Russell. All rights reserved. | |
// | |
#import "ESHTTPOperation.h" | |
@interface KATGDownloadOperation : ESHTTPOperation | |
+ (instancetype)newDownloadOperationWithRemoteURL:(NSURL *)remoteURL fileURL:(NSURL *)fileURL completion:(ESHTTPOperationCompletionBlock)completion; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// KATGDownloadOperation.m | |
// KATG | |
// | |
// Created by Doug Russell on 3/7/13. | |
// Copyright (c) 2013 Doug Russell. All rights reserved. | |
// | |
#import "KATGDownloadOperation.h" | |
@implementation KATGDownloadOperation | |
+ (instancetype)newDownloadOperationWithRemoteURL:(NSURL *)remoteURL fileURL:(NSURL *)fileURL completion:(ESHTTPOperationCompletionBlock)completion | |
{ | |
return [[self alloc] initDownloadOperationWithRemoteURL:remoteURL fileURL:fileURL completion:completion]; | |
} | |
- (BOOL)exists:(NSURL *)fileURL size:(NSUInteger *)size | |
{ | |
NSParameterAssert(size); | |
NSParameterAssert([fileURL isFileURL]); | |
BOOL fileExists; | |
if ([fileURL checkResourceIsReachableAndReturnError:nil]) | |
{ | |
fileExists = YES; | |
NSNumber *sizeObject; | |
NSError *error; | |
if ([fileURL getResourceValue:&sizeObject forKey:NSURLFileSizeKey error:&error]) | |
{ | |
*size = [sizeObject unsignedIntegerValue]; | |
} | |
else | |
{ | |
NSLog(@"%@", error); | |
return NO; | |
} | |
} | |
else | |
{ | |
fileExists = NO; | |
*size = 0; | |
} | |
return fileExists; | |
} | |
- (instancetype)initDownloadOperationWithRemoteURL:(NSURL *)remoteURL fileURL:(NSURL *)fileURL completion:(ESHTTPOperationCompletionBlock)completion | |
{ | |
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:remoteURL]; | |
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData]; | |
NSOutputStream *outputStream; | |
NSUInteger size; | |
if ([self exists:fileURL size:&size]) | |
{ | |
[request setValue:[NSString stringWithFormat:@"bytes=%d-", size] forHTTPHeaderField:@"Range"]; | |
outputStream = [NSOutputStream outputStreamWithURL:fileURL append:YES]; | |
} | |
else | |
{ | |
outputStream = [NSOutputStream outputStreamWithURL:fileURL append:NO]; | |
} | |
self = [super initWithRequest:request work:NULL completion:completion]; | |
if (self) | |
{ | |
self.outputStream = outputStream; | |
self.cancelOnStatusCodeError = YES; | |
if (size) | |
{ | |
self.acceptableStatusCodes = [NSIndexSet indexSetWithIndex:206]; | |
} | |
else | |
{ | |
self.acceptableStatusCodes = [NSIndexSet indexSetWithIndex:200]; | |
} | |
} | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment