Removing AFDownloadRequestOperation

This commit is contained in:
Mattt Thompson 2012-04-24 20:43:46 -07:00
parent 7e16e13a3a
commit 4c8634dfce
3 changed files with 0 additions and 260 deletions

View file

@ -1,64 +0,0 @@
// AFDownloadRequestOperation.h
//
// Copyright (c) 2012 Mattt Thompson (http://mattt.me)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "AFHTTPRequestOperation.h"
/**
*/
@interface AFDownloadRequestOperation : AFHTTPRequestOperation;
/**
A Boolean value that indicates if we should try to resume the download. Defaults is `YES`.
Can only be set while creating the request.
Note: This allows long-lasting resumes between app-starts. Use this for content that doesn't change.
If the file changed in the meantime, you'll end up with a broken file.
*/
@property (assign, readonly) BOOL shouldResume;
/**
Set a destination. If you don't manually set one, this defaults to the documents directory.
Note: This can point to a path or a file. If this is a path, response.suggestedFilename will be used for the filename.
*/
- (void)setDestination:(NSString *)path allowOverwrite:(BOOL)allowOverwrite;
/**
Deletes the temporary file if operation fails/is cancelled. Defaults to `NO`.
*/
@property (assign) BOOL deletesFileUponFailure;
///**
//
// */
//- (void)setDecideDestinationWithSuggestedFilenameBlock:(void (^)(NSString *filename))block;
//
///**
//
// */
//- (void)setShouldDecodeSourceDataOfMimeTypeBlock:(BOOL (^)(NSString *encodingType))block;
//
@end

View file

@ -1,190 +0,0 @@
// AFDownloadRequestOperation.m
//
// Copyright (c) 2012 Mattt Thompson (http://mattt.me)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "AFDownloadRequestOperation.h"
#import "AFURLConnectionOperation.h"
@interface AFDownloadRequestOperation()
@property (readwrite, nonatomic, retain) NSURLRequest *request;
@property (readwrite, nonatomic, retain) NSError *downloadError;
@property (readwrite, nonatomic, copy) NSString *destination;
@property (readwrite, nonatomic, assign) BOOL allowOverwrite;
@end
static unsigned long long AFFileSizeForPath(NSString *path) {
unsigned long long fileSize = 0;
NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
if ([fileManager fileExistsAtPath:path]) {
NSError *error = nil;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:&error];
if (!error && attributes) {
fileSize = [attributes fileSize];
}
}
return fileSize;
}
@implementation AFDownloadRequestOperation
@synthesize shouldResume = _shouldResume;
@synthesize downloadError = _downloadError;
@synthesize destination = _destination;
@synthesize allowOverwrite = _allowOverwrite;
@synthesize deletesFileUponFailure = _deletesFileUponFailure;
@dynamic request;
- (id)initWithRequest:(NSURLRequest *)urlRequest {
if ((self = [super initWithRequest:urlRequest])) {
_shouldResume = YES;
_destination = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] copy];
}
return self;
}
- (void)dealloc {
[_downloadError release];
[_destination release];
[super dealloc];
}
- (NSError *)error {
if (_downloadError) {
return _downloadError;
} else {
return [super error];
}
}
// the temporary path depends on the request URL.
- (NSString *)temporaryPath {
NSString *temporaryPath = nil;
if (self.destination) {
NSString *hashString = [NSString stringWithFormat:@"%u", [self.request.URL hash]];
temporaryPath = [AFCreateIncompleteDownloadDirectoryPath() stringByAppendingPathComponent:hashString];
}
return temporaryPath;
}
// build the final destination with _destination and response.suggestedFilename.
- (NSString *)destinationPath {
NSString *destinationPath = _destination;
// we assume that at least the directory has to exist on the targetPath
BOOL isDirectory;
if(![[NSFileManager defaultManager] fileExistsAtPath:_destination isDirectory:&isDirectory]) {
isDirectory = NO;
}
// if targetPath is a directory, use the file name we got from the urlRequest.
if (isDirectory) {
destinationPath = [NSString pathWithComponents:[NSArray arrayWithObjects:_destination, self.response.suggestedFilename, nil]];
}
return destinationPath;
}
- (BOOL)deleteTempFileWithError:(NSError **)error {
NSFileManager *fileManager = [[NSFileManager alloc] init];
BOOL success = YES;
@synchronized(self) {
NSString *tempPath = [self temporaryPath];
if ([fileManager fileExistsAtPath:tempPath]) {
success = [fileManager removeItemAtPath:[self temporaryPath] error:error];
}
}
[fileManager release];
return success;
}
#pragma mark -
- (void)setDestination:(NSString *)path allowOverwrite:(BOOL)allowOverwrite {
self.allowOverwrite = allowOverwrite;
self.destination = path;
}
#pragma mark - NSOperation
- (BOOL)isReady {
return [super isReady] && self.destination;
}
- (void)start {
if ([self isReady]) {
self.responseFilePath = [self temporaryPath];
if (_shouldResume) {
unsigned long long tempFileSize = AFFileSizeForPath([self temporaryPath]);
if (tempFileSize > 0) {
NSMutableURLRequest *mutableURLRequest = [[self.request mutableCopy] autorelease];
[mutableURLRequest setValue:[NSString stringWithFormat:@"bytes=%llu-", tempFileSize] forHTTPHeaderField:@"Range"];
self.request = mutableURLRequest;
[self.outputStream setProperty:[NSNumber numberWithUnsignedLongLong:tempFileSize] forKey:NSStreamFileCurrentOffsetKey];
}
}
[super start];
}
}
#pragma mark - AFURLRequestOperation
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
self.completionBlock = ^ {
if([self isCancelled]) {
if (self.deletesFileUponFailure) {
[self deleteTempFileWithError:&_downloadError];
}
return;
}else {
@synchronized(self) {
NSString *destinationPath = [self destinationPath];
NSFileManager *fileManager = [[NSFileManager alloc] init];
if (_allowOverwrite && [fileManager fileExistsAtPath:destinationPath]) {
[fileManager removeItemAtPath:destinationPath error:&_downloadError];
}
if (!_downloadError) {
[fileManager moveItemAtPath:[self temporaryPath] toPath:destinationPath error:&_downloadError];
}
[fileManager release];
}
}
if (self.error) {
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
failure(self, self.error);
});
} else {
dispatch_async(self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{
success(self, _destination);
});
}
};
}
#pragma mark -
//- (void)setDecideDestinationWithSuggestedFilenameBlock:(void (^)(NSString *filename))block;
//
//- (void)setShouldDecodeSourceDataOfMimeTypeBlock:(BOOL (^)(NSString *encodingType))block;
@end

View file

@ -7,7 +7,6 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
F8BF7FE0153234AE00885289 /* AFDownloadRequestOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = F8BF7FDF153234AE00885289 /* AFDownloadRequestOperation.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
F8D0701B14310F4A00653FD3 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8E469E213957DF700DB05C8 /* SystemConfiguration.framework */; }; F8D0701B14310F4A00653FD3 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8E469E213957DF700DB05C8 /* SystemConfiguration.framework */; };
F8D0701C14310F4F00653FD3 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8E469E013957DF100DB05C8 /* Security.framework */; }; F8D0701C14310F4F00653FD3 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8E469E013957DF100DB05C8 /* Security.framework */; };
F8DA09E41396AC040057D0CC /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = F8DA09E31396AC040057D0CC /* main.m */; }; F8DA09E41396AC040057D0CC /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = F8DA09E31396AC040057D0CC /* main.m */; };
@ -40,8 +39,6 @@
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
F8BF7FDE153234AE00885289 /* AFDownloadRequestOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFDownloadRequestOperation.h; sourceTree = "<group>"; };
F8BF7FDF153234AE00885289 /* AFDownloadRequestOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFDownloadRequestOperation.m; sourceTree = "<group>"; };
F8DA09E31396AC040057D0CC /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = SOURCE_ROOT; }; F8DA09E31396AC040057D0CC /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = SOURCE_ROOT; };
F8DA09E51396AC220057D0CC /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = SOURCE_ROOT; }; F8DA09E51396AC220057D0CC /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = SOURCE_ROOT; };
F8DA09E61396AC220057D0CC /* Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Prefix.pch; sourceTree = SOURCE_ROOT; }; F8DA09E61396AC220057D0CC /* Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Prefix.pch; sourceTree = SOURCE_ROOT; };
@ -238,8 +235,6 @@
F8FA94AC150EFEC100ED4EAD /* AFURLConnectionOperation.m */, F8FA94AC150EFEC100ED4EAD /* AFURLConnectionOperation.m */,
F8FA949E150EFEC100ED4EAD /* AFHTTPRequestOperation.h */, F8FA949E150EFEC100ED4EAD /* AFHTTPRequestOperation.h */,
F8FA949F150EFEC100ED4EAD /* AFHTTPRequestOperation.m */, F8FA949F150EFEC100ED4EAD /* AFHTTPRequestOperation.m */,
F8BF7FDE153234AE00885289 /* AFDownloadRequestOperation.h */,
F8BF7FDF153234AE00885289 /* AFDownloadRequestOperation.m */,
F8FA94A2150EFEC100ED4EAD /* AFJSONRequestOperation.h */, F8FA94A2150EFEC100ED4EAD /* AFJSONRequestOperation.h */,
F8FA94A3150EFEC100ED4EAD /* AFJSONRequestOperation.m */, F8FA94A3150EFEC100ED4EAD /* AFJSONRequestOperation.m */,
F8FA94AD150EFEC100ED4EAD /* AFXMLRequestOperation.h */, F8FA94AD150EFEC100ED4EAD /* AFXMLRequestOperation.h */,
@ -336,7 +331,6 @@
F8FA949A150EF9DA00ED4EAD /* PublicTimelineViewController.m in Sources */, F8FA949A150EF9DA00ED4EAD /* PublicTimelineViewController.m in Sources */,
F8FA94B1150EFEC100ED4EAD /* AFHTTPClient.m in Sources */, F8FA94B1150EFEC100ED4EAD /* AFHTTPClient.m in Sources */,
F8FA94B2150EFEC100ED4EAD /* AFHTTPRequestOperation.m in Sources */, F8FA94B2150EFEC100ED4EAD /* AFHTTPRequestOperation.m in Sources */,
F8BF7FE0153234AE00885289 /* AFDownloadRequestOperation.m in Sources */,
F8FA94B3150EFEC100ED4EAD /* AFImageRequestOperation.m in Sources */, F8FA94B3150EFEC100ED4EAD /* AFImageRequestOperation.m in Sources */,
F8FA94B4150EFEC100ED4EAD /* AFJSONRequestOperation.m in Sources */, F8FA94B4150EFEC100ED4EAD /* AFJSONRequestOperation.m in Sources */,
F8FA94B5150EFEC100ED4EAD /* AFJSONUtilities.m in Sources */, F8FA94B5150EFEC100ED4EAD /* AFJSONUtilities.m in Sources */,