88
99#import " QiniuResumableClient.h"
1010#import " QiniuConfig.h"
11+ #import " QiniuUtils.h"
1112
1213@implementation QiniuResumableClient
1314
@@ -20,11 +21,11 @@ - (QiniuResumableClient *)initWithToken:(NSString *)token
2021 self.token = [[NSString alloc ] initWithFormat: @" UpToken %@ " , token];
2122 self.retryTime = tryTime;
2223 self.chunkSize = chunkSize; // 256k
23-
24+
2425 self.responseSerializer = [AFJSONResponseSerializer serializer ];
2526 self.operationQueue = [[NSOperationQueue alloc ] init ];
2627 [self .operationQueue setMaxConcurrentOperationCount: maxWorkers];
27-
28+
2829 return self;
2930}
3031
@@ -47,10 +48,10 @@ - (void)mkblock:(NSData *)mappedData
4748 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: callUrl]];
4849
4950 [self setHeaders: request];
50-
51+
5152 NSData *postData = [mappedData subdataWithRange: NSMakeRange (offset, bodyLength)];
5253 [request setHTTPBody: postData];
53-
54+
5455 QNCompleteBlock success = ^(AFHTTPRequestOperation *operation, id responseObject)
5556 {
5657 // TODO: check crc32
@@ -60,7 +61,7 @@ - (void)mkblock:(NSData *)mappedData
6061 {
6162 complete (operation, error);
6263 };
63-
64+
6465 AFHTTPRequestOperation *operation = [super HTTPRequestOperationWithRequest: request
6566 success: success
6667 failure: failure];
@@ -79,12 +80,12 @@ - (void)chunkPut:(NSData *)mappedData
7980 }
8081 NSString *callUrl = [[NSString alloc ] initWithFormat: @" %@ /bput/%@ /%d " , blockPutRet.host, blockPutRet.ctx, (unsigned int )blockPutRet.offset];
8182 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: callUrl]];
82-
83+
8384 [self setHeaders: request];
84-
85+
8586 NSData *postData = [mappedData subdataWithRange: NSMakeRange (offsetBase + blockPutRet.offset, bodyLength)];
8687 [request setHTTPBody: postData];
87-
88+
8889 QNCompleteBlock success = ^(AFHTTPRequestOperation *operation, id responseObject)
8990 {
9091 // TODO: check crc32
@@ -94,7 +95,7 @@ - (void)chunkPut:(NSData *)mappedData
9495 {
9596 complete (operation, error);
9697 };
97-
98+
9899 AFHTTPRequestOperation *operation = [super HTTPRequestOperationWithRequest: request
99100 success: success
100101 failure: failure];
@@ -109,21 +110,21 @@ - (void)blockPut:(NSData *)mappedData
109110 complete : (QNCompleteBlock)complete
110111{
111112 // @autoreleasepool {
112-
113-
113+
114+
114115 UInt32 offsetBase = blockIndex << QiniuBlockBits;
115-
116+
116117 __block UInt32 bodyLength = self.chunkSize < blockSize ? self.chunkSize : blockSize;
117118 __block QiniuBlkputRet *blockPutRet;
118119 __block UInt32 retryTime = self.retryTime ;
119120 __block BOOL isMkblock = YES ;
120-
121+
121122 QNCompleteBlock __block __weak weakChunkComplete;
122123 QNCompleteBlock chunkComplete;
123124 weakChunkComplete = chunkComplete = ^(AFHTTPRequestOperation *operation, NSError *error)
124125 {
125126 if (error != nil ) {
126-
127+
127128 if (retryTime == 0 || isMkblock || [operation.response statusCode ] == 701 ) {
128129 complete (operation, error);
129130 return ;
@@ -137,24 +138,24 @@ - (void)blockPut:(NSData *)mappedData
137138 retryTime = self.retryTime ;
138139 isMkblock = NO ;
139140 blockPutRet = [[QiniuBlkputRet alloc ] initWithDictionary: operation.responseObject];
140-
141+
141142 UInt32 remainLength = blockSize - blockPutRet.offset ;
142143 bodyLength = self.chunkSize < remainLength ? self.chunkSize : remainLength;
143144 }
144-
145+
145146 if (blockPutRet.offset == blockSize) {
146147 complete (operation, nil );
147148 return ;
148149 }
149-
150+
150151 [self chunkPut: mappedData
151152 blockPutRet: blockPutRet
152153 offsetBase: offsetBase
153154 bodyLength: bodyLength
154155 progress: progressBlock
155156 complete: weakChunkComplete];
156157 };
157-
158+
158159 [self mkblock: mappedData
159160 offsetBase: offsetBase
160161 blockSize: blockSize
@@ -167,7 +168,7 @@ - (void)blockPut:(NSData *)mappedData
167168+ (NSString *)encode : (NSString *)str
168169{
169170 str = [[str dataUsingEncoding: NSUTF8StringEncoding] base64EncodedStringWithOptions: 0 ];
170-
171+
171172 // is there other methed?
172173 str = [str stringByReplacingOccurrencesOfString: @" +" withString: @" -" ];
173174 str = [str stringByReplacingOccurrencesOfString: @" /" withString: @" _" ];
@@ -180,31 +181,31 @@ - (void)mkfile:(NSString *)key
180181 progress : (QNProgressBlock)progressBlock
181182 complete : (QNCompleteBlock)complete
182183{
183-
184+
184185 NSString *mimeStr = extra.mimeType == nil ? @" " : [[NSString alloc ] initWithFormat: @" /mimetype/%@ " , [QiniuResumableClient encode: extra.mimeType]];
185-
186+
186187 NSString *callUrl = [[NSString alloc ] initWithFormat: @" %@ /mkfile/%u %@ " , kQiniuUpHost , (unsigned int )fileSize, mimeStr];
187-
188+
188189 if (key != nil ) {
189190 NSString *keyStr = [[NSString alloc ] initWithFormat: @" /key/%@ " , [QiniuResumableClient encode: key]];
190191 callUrl = [NSString stringWithFormat: @" %@%@ " , callUrl, keyStr];
191192 }
192-
193+
193194 if (extra.params != nil ) {
194195 NSEnumerator *e = [extra.params keyEnumerator ];
195196 for (id key = [e nextObject ]; key != nil ; key = [e nextObject ]) {
196197 callUrl = [NSString stringWithFormat: @" %@ /%@ /%@ " , callUrl, key, [QiniuResumableClient encode: [extra.params objectForKey: key]]];
197198 }
198199 }
199-
200+
200201 NSMutableURLRequest *request = [[NSMutableURLRequest alloc ] initWithURL: [[NSURL alloc ] initWithString: callUrl]];
201202 [self setHeaders: request];
202-
203+
203204 NSMutableData *postData = [NSMutableData data ];
204205 NSString *bodyStr = [extra.progresses componentsJoinedByString: @" ," ];
205206 [postData appendData: [bodyStr dataUsingEncoding: NSUTF8StringEncoding]];
206207 [request setHTTPBody: postData];
207-
208+
208209 QNCompleteBlock success = ^(AFHTTPRequestOperation *operation, id responseObject)
209210 {
210211 complete (operation, nil );
@@ -213,7 +214,7 @@ - (void)mkfile:(NSString *)key
213214 {
214215 complete (operation, error);
215216 };
216-
217+
217218 AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest: request
218219 success: success
219220 failure: failure];
@@ -224,7 +225,7 @@ - (void)setHeaders:(NSMutableURLRequest *)request
224225{
225226 [request setValue: self .token forHTTPHeaderField: @" Authorization" ];
226227 [request setValue: @" application/octet-stream" forHTTPHeaderField: @" Content-Type" ];
227- [request addValue: kQiniuUserAgent forHTTPHeaderField: @" User-Agent" ];
228+ [request addValue: qiniuUserAgent () forHTTPHeaderField: @" User-Agent" ];
228229 [request setHTTPMethod: @" POST" ];
229230}
230231
@@ -263,14 +264,14 @@ - (BOOL) blockUploadedAndCheck
263264 if (blockNumlock == nil ) {
264265 blockNumlock = [[NSLock alloc ] init ];
265266 }
266-
267+
267268 BOOL allBlockOk;
268-
269+
269270 [blockNumlock lock ];
270271 self.uploadedBlockNumber ++;
271272 allBlockOk = self.uploadedBlockNumber == self.blockCount ;
272273 [blockNumlock unlock ];
273-
274+
274275 return allBlockOk;
275276}
276277
@@ -280,13 +281,13 @@ - (float) chunkUploadedAndPercent
280281 if (chunkNumlock == nil ) {
281282 chunkNumlock = [[NSLock alloc ] init ];
282283 }
283-
284+
284285 float percent;
285286 [chunkNumlock lock ];
286287 self.uploadedChunkNumber ++;
287288 percent = (float )self.uploadedChunkNumber / self.chunkCount ;
288289 [chunkNumlock unlock ];
289-
290+
290291 return percent;
291292}
292293
0 commit comments