-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCFileDownloadOperation.m
92 lines (73 loc) · 2.6 KB
/
DCFileDownloadOperation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// DCFileDownloadOperation.m
//
// Created by David Cairns on 10/4/10.
// Copyright 2010 David Cairns. All rights reserved.
//
#import "DCFileDownloadOperation.h"
#import "DCOperation+ARCSupport.h"
NSString *DCFileDownloadErrorDomain = @"DCFileDownloadErrorDomain";
@interface DCFileDownloadOperation()
@property(nonatomic, copy)NSURL *destinationURL;
@end
@implementation DCFileDownloadOperation
@synthesize destinationURL = _destinationURL;
@synthesize bytesDownloaded = _bytesDownloaded;
- (id)initWithURLString:(NSString *)urlString destinationURL:(NSURL *)destinationURL {
if((self = [super init])) {
NSAssert(urlString, @"Attempted to download from nil url.");
self.urlString = urlString;
self.destinationURL = destinationURL;
// This operation should not worry about timeout.
self.timeoutInterval = 0.0;
}
return self;
}
- (void)dealloc {
DC_RELEASE(_destinationURL);
#if !defined(DC_USE_ARC)
[super dealloc];
#endif
}
- (BOOL)operationShouldStart {
// Generate the path for this language pack archive.
NSFileManager *fileManager = DC_AUTORELEASE([[NSFileManager alloc] init]);
// If it exists, delete the pre-existing archive file.
NSError *error = nil;
BOOL isDirectory = NO;
NSString *filePath = [self.destinationURL absoluteString];
if([fileManager fileExistsAtPath:filePath isDirectory:&isDirectory]) {
if(isDirectory) {
NSLog(@"Expected: old language pack; found: directory. Deleting anyway...");
}
// Attempt to delete the file.
if(![fileManager removeItemAtPath:filePath error:&error]) {
self.error = [NSError errorWithDomain:DCFileDownloadErrorDomain code:DCFileDownloadErrorCodeFileExists userInfo:nil];
return NO;
}
}
// Create the file we're to write into.
if(![fileManager createFileAtPath:filePath contents:[NSData data] attributes:nil]) {
self.error = [NSError errorWithDomain:DCFileDownloadErrorDomain code:DCFileDownloadErrorCodeCreationFailure userInfo:nil];
return NO;
}
return [super operationShouldStart];
}
- (void)finish {
NSLog(@"Downloaded file stored in : %@", self.destinationURL);
[super finish];
}
// Override the NSURLConnection delegate method.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// We've received some data; append it to our file.
NSError *error = nil;
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingToURL:self.destinationURL error:&error];
[fileHandle seekToEndOfFile];
[fileHandle writeData:data];
self.bytesDownloaded += [data length];
// Make sure the transfer update block still gets called.
if(self.transferUpdateBlock) {
self.transferUpdateBlock();
}
}
@end