-
Notifications
You must be signed in to change notification settings - Fork 3
/
VHDownloadManager.m
204 lines (137 loc) · 5.56 KB
/
VHDownloadManager.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// VHDownloadManager.m
// VHDownloadManager
//
// Created by 刁志远 on 2016/10/30.
// Copyright © 2016年 刁志远. All rights reserved.
//
#import "VHDownloadManager.h"
#import "VHDownload.h"
@interface VHDownloadManager () <VHDownloadDelegate>{
NSString *strRequestUrl;
NSString *fileName;
unsigned long long downloadedSize;
unsigned long long totalSize;
NSMutableArray *arrDownload;
NSMutableDictionary *downloadPath;
}
@end
@implementation VHDownloadManager
#pragma mark - VHDownloadDelegate
//download did start
- (void)download:(VHDownload *)download didStart:(NSHTTPURLResponse *)response{
if (download.isTest) {
if (response.statusCode == 206 && !fileName) {//can be split
fileName = [self URLDecodedString:response.suggestedFilename];
NSString *str = response.allHeaderFields[@"Content-Range"];
NSRange ran = [str rangeOfString:@"/"];
totalSize = [[str substringFromIndex:ran.location+1] longLongValue];
downloadedSize = 0;
[self splitDownload];
if (self.delegate && [self.delegate respondsToSelector:@selector(didStartDownloadWithFileName:andTotalSize:)]) {
[self.delegate didStartDownloadWithFileName:fileName
andTotalSize:totalSize];
}
}
return;
}
}
//download write data
- (void)download:(VHDownload *)download didWriteData:(unsigned long long)length andTotalData:(unsigned long long)total{
if (!download.isTest) {
downloadedSize+=length;
// if(totalSize != total)
// totalSize = total;
if (self.delegate && [self.delegate respondsToSelector:@selector(didLoadData:andProgress:)]) {
[self.delegate didLoadData:length andProgress:(double)downloadedSize/totalSize];
}
}
}
//download finish
- (void)download:(VHDownload *)download didFinishDownload:(NSString *)location {
[arrDownload removeObject:download];
if (!downloadPath) {
downloadPath = [NSMutableDictionary dictionaryWithCapacity:3];
}
NSString *sk = [NSString stringWithFormat:@"%td",download.index];
downloadPath[sk] = location;
NSLog(@"download cnt : %d",arrDownload.count);
if (arrDownload.count <= 0) {
NSMutableData *m_data = [NSMutableData dataWithCapacity:3];
for (int i=0; i<[[downloadPath allKeys] count]; i++) {
NSString *sk = [NSString stringWithFormat:@"%d",i];
NSString *file_path = downloadPath[sk];
NSData *data = [NSData dataWithContentsOfFile:file_path];
[m_data appendData:data];
[[NSFileManager defaultManager] removeItemAtPath:file_path
error:nil];
}
NSString *des_file = [FILEPATH stringByAppendingPathComponent:fileName];
[m_data writeToFile:des_file atomically:YES];
[m_data resetBytesInRange:NSMakeRange(0, [m_data length])];
[m_data setLength:0];
m_data = nil;
if (self.delegate && [self.delegate respondsToSelector:@selector(didFinishLoadInDirectory:)]) {
[self.delegate didFinishLoadInDirectory:des_file];
}
}
}
#pragma mark - Private Methods
-(NSString *)URLDecodedString:(NSString *)str
{
NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
return decodedString;
}
- (void)startAllDownload {
for (int i=0; i<arrDownload.count; i++) {
VHDownload *download = arrDownload[i];
[download startDownload];
}
}
- (void)splitDownload {
if (!arrDownload) {
arrDownload = [NSMutableArray arrayWithCapacity:3];
}
[arrDownload removeAllObjects];
unsigned long long per_size = totalSize/self.maxDownloadThread;
for (int i=0; i<self.maxDownloadThread; i++) {
VHRequestRange *ran;
if (i== self.maxDownloadThread-1) {//最后一个
ran = [[VHRequestRange alloc] initWithLocation:i*(per_size+1)
andLength:totalSize-i*per_size];
}
else {
ran = [[VHRequestRange alloc] initWithLocation:i*(per_size+1)
andLength:per_size];
}
VHDownload *down = [VHDownload startDownloadWithUrl:strRequestUrl
andRange:ran];
down.index = i;
down.delegate = self;
[arrDownload addObject:down];
}
[self startAllDownload];
}
#pragma mark - Public Method
- (void)startDownload {
VHRequestRange *range = [[VHRequestRange alloc] initWithLocation:0 andLength:1024];
VHDownload *download = [VHDownload startDownloadWithUrl:strRequestUrl
andRange:range];
download.isTest = YES;
download.delegate = self;
[download startDownload];
}
#pragma mark - Initinal
- (void)initinal {
self.maxDownloadThread = 10;
}
#pragma mark - SYS
- (id)initWithDownloadUrl:(NSString *)surl {
self = [super init];
if (self) {
strRequestUrl = [NSString stringWithString:surl];
[self initinal];
}
return self;
}
@end