forked from overtake/telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TGSDownloadOperation.m
309 lines (190 loc) · 8.62 KB
/
TGSDownloadOperation.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// TGSDownloadOperation
// Telegram
//
// Created by keepcoder on 07.05.14.
// Copyright (c) 2013 keepcoder. All rights reserved.
//
#import "TGSDownloadOperation.h"
#import "NSMutableData+Extension.h"
#import "DownloadPart.h"
#import "DownloadQueue.h"
#import "TGS_RPCRequest.h"
@interface TGSDownloadOperation ()
@property (nonatomic,strong)id target;
@property (nonatomic,assign) SEL selector;
@property (nonatomic,assign) BOOL isCancelled;
@property (nonatomic,strong) NSOutputStream *stream;
@property (nonatomic,assign) NSUInteger downloaded;
@property (nonatomic,strong) NSMutableData *resultData;
@property (nonatomic,strong) NSMutableArray *poll;
@property (nonatomic,strong) NSMutableArray *completePoll;
@property (nonatomic,assign) int partSize;
@property (nonatomic,assign) int partEnumerator;
@property (nonatomic,assign) int currentPartId;
@property (nonatomic,assign) int max_poll_size;
@property (nonatomic,assign) int startOffset;
@end
@implementation TGSDownloadOperation
@synthesize item = _item;
-(id)initWithItem:(DownloadItem *)item {
if(self = [super init]) {
_item = item;
self.poll = [[NSMutableArray alloc] init];
self.completePoll = [[NSMutableArray alloc] init];
self.resultData = [[NSMutableData alloc] init];
self.partSize = [item partSize];
self.max_poll_size = self.partSize == 0 ? 1 : 5;
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:_item.path error: NULL];
BOOL append = YES;
self.item.isRemoteLoaded = YES;
if(item.size != 0 && self.item.fileType != DownloadFileImage) {
self.downloaded = self.startOffset = (int)[attrs fileSize];
append = self.downloaded < item.size && self.downloaded > 0;
[DownloadQueue setProgress:(float)self.downloaded/(float)_item.size * 100.0f toOperation:self];
}
self.stream = [[NSOutputStream alloc] initToFileAtPath:_item.path append:append];
}
return self;
}
-(void)start:(id)target selector:(SEL)selector {
self.selector = selector;
self.target = target;
_item.errorType = DownloadErrorNone;
[DownloadQueue dispatchOnDownloadQueue:^{
[DownloadQueue setProgress:(float)self.downloaded/(float)_item.size * 100.0f toOperation:self];
if(self.partSize == 0) {
[DownloadQueue setProgress:70 toOperation:self];
}
if(self.item.size != 0 && self.downloaded == self.item.size && self.item.fileType != DownloadFileImage) {
_item.downloadState = DownloadStateCompleted;
[self.target performSelectorInBackground:self.selector withObject:self];
return;
}
if(_item.fileType == DownloadFileImage) {
NSData *imageData = [NSData dataWithContentsOfFile:self.item.path];
if(imageData == nil || (imageData.length == 0 || (self.item.size > 0 && self.item.size > imageData.length))) {
[self load];
} else {
_item.isRemoteLoaded = NO;
_item.result = imageData;
_item.downloadState = DownloadStateCompleted;
[self.target performSelectorInBackground:self.selector withObject:self];
}
} else
[self load];
}];
}
- (void)flush {
[DownloadQueue dispatchOnDownloadQueue:^{
while (self.poll.count > 0) {
DownloadPart *part = self.poll[0];
[part.request cancelRequest];
[self.poll removeObjectAtIndex:0];
part = nil;
}
while (self.completePoll.count > 0) {
DownloadPart *part = self.completePoll[0];
[part.request cancelRequest];
[self.completePoll removeObjectAtIndex:0];
part = nil;
}
}];
}
-(void)cancel {
[DownloadQueue dispatchOnDownloadQueue:^{
self.resultData = nil;
[self flush];
_item.errorType = DownloadErrorCantLoad;
if(self.item.isRemoteLoaded) {
[self.stream close];
if(self.item.size == 0)
[[NSFileManager defaultManager] removeItemAtPath:_item.path error:nil];
[self.target performSelectorInBackground:self.selector withObject:self];
}
}];
}
-(void)load:(DownloadPart *)part {
TLAPI_upload_getFile *upload = [TLAPI_upload_getFile createWithLocation:part.item.input offset:part.offset limit:self.partSize];
part.request = [TGS_RPCRequest sendRequest:upload forDc:part.dcId successHandler:^(TGS_RPCRequest *request, id response) {
[DownloadQueue dispatchOnDownloadQueue:^{
TLupload_File *obj = response;
part.resultData = obj.bytes;
obj.bytes = nil;
[self.poll removeObject:part];
[self.completePoll addObject:part];
[self.completePoll sortUsingComparator:^NSComparisonResult(DownloadPart *part1, DownloadPart *part2) {
return part1.partId > part2.partId ? NSOrderedDescending : NSOrderedAscending;
}];
while (_completePoll.count > 0 && [_completePoll[0] partId] == _currentPartId) {
++_currentPartId;
DownloadPart *p = _completePoll[0];
[self proccessPartData:p.resultData];
[_completePoll removeObject:p];
p.resultData = nil;
p.request = nil;
p = nil;
}
[self fill];
}];
} errorHandler:^(TGS_RPCRequest *request, RpcError *error) {
[DownloadQueue dispatchOnDownloadQueue:^{
int n_dc = error.resultId;
n_dc = n_dc == 0 ? part.dcId : n_dc;
if(part.dcId != n_dc) {
[self flush];
self.partEnumerator = 0;
self.item.dc_id = n_dc;
self.currentPartId = 0;
[self.stream close];
self.stream = [[NSOutputStream alloc] initToFileAtPath:_item.path append:NO];
[self load];
} else {
_item.downloadState = DownloadStateCompleted;
[self cancel];
}
}];
}];
}
-(void)proccessPartData:(NSData *)partData {
self.downloaded+=partData.length;
const uint8_t *bytes = (const uint8_t*)[partData bytes];
[self.stream write:bytes maxLength:partData.length];
if(_item.fileType == DownloadFileImage)
[self.resultData appendData:partData];
if( _item.size == 0 || _item.size <= self.downloaded || bytes == NULL) {
[self.stream close];
_item.progress = 100.0f;
_item.result = self.resultData;
if(_item.downloadState == DownloadStateCanceled) {
[self cancel];
} else {
_item.downloadState = DownloadStateCompleted;
[self.target performSelectorInBackground:self.selector withObject:self];
}
} else {
[DownloadQueue setProgress:(float)self.downloaded/(float)_item.size * 100.0f toOperation:self];
}
}
- (void)fill {
while (_item.downloadState == DownloadStateDownloading && _poll.count < _max_poll_size) {
if(self.item.size == 0 || (self.startOffset + self.partEnumerator*self.partSize <= self.item.size)) {
DownloadPart *newPart = [[DownloadPart alloc] initWithId:self.partEnumerator offset:self.startOffset + self.partEnumerator*self.partSize dcId:_item.dc_id downloadItem:_item];
self.partEnumerator++;
[_poll addObject:newPart];
[self load:[_poll lastObject]];
} else {
break;
}
}
}
-(void)load {
if(self.item.dc_id == 0) {
_item.downloadState = DownloadStateCompleted;
[self.target performSelectorInBackground:self.selector withObject:self];
return;
}
[self.stream open];
[self fill];
}
@end