1818#define CRC64PartLength 10 * 1024 * 1024
1919
2020@interface QCloudCOSXMLDownloadObjectRequest ()
21+ @property (nonatomic ,assign )NSInteger retryCount;
2122// 存储所有的下载请求
2223@property (nonatomic , strong ) NSPointerArray *requestCacheArray;
2324@property (nonatomic , strong ) dispatch_source_t queueSource;
@@ -42,6 +43,7 @@ - (instancetype)init {
4243 if (!self) {
4344 return nil ;
4445 }
46+ self.enablePartCrc64 = YES ;
4547 self.objectKeySimplifyCheck = YES ;
4648 _customHeaders = [NSMutableDictionary dictionary ];
4749 _partCrc64Map = [NSMutableDictionary new ];
@@ -246,14 +248,6 @@ - (uint64_t)crc64ForFileAtPath:(NSString *)filePath
246248-(void )loadLocalCrc64 {
247249 self.crc64Start = self.localCacheDownloadOffset ;
248250 self.crc64Complete = self.crc64Start ;
249- // uint64_t crc64 = [self crc64ForFileAtPath:self.downloadingTempURL.path chunkSize:CRC64PartLength length:0];
250- // self.crc64Complete = self.crc64Start;
251- // NSString * range = [NSString stringWithFormat:@"0-%ld",(long)self.crc64Complete];
252- // [self.partCrc64Map setObject:@(crc64) forKey:range];
253- // QCloudLogDebugN(@"CRC64", @"calculateCrc64,partRang:%@,crc64:%ld",range,crc64);
254- // NSData *info =[NSJSONSerialization dataWithJSONObject:[self.partCrc64Map copy] options:NSJSONWritingPrettyPrinted error:nil];
255- // [info writeToFile:self.partCrc64Filepath options:0 error:nil];
256-
257251}
258252
259253- (void )startGetObject {
@@ -380,27 +374,32 @@ - (void)startGetObject {
380374 }
381375 return ;
382376 }
377+ QCloudRemoveFileByPath (strongSelf.resumableTaskFile );
383378 uint64_t localCrc64;
384379 if (self.enablePartCrc64 == YES ) {
385380 [self calculateCrc64: self .downloadingURL fileSize: 0 ];
386381 localCrc64 = [self mergePartCrc64 ];
387- }else {
388- localCrc64 = [strongSelf crc64ForFileAtPath: strongSelf.downloadingURL.relativePath chunkSize: CRC64PartLength length: 0 ];
389- }
390- NSString *localCrc64Str = [NSString stringWithFormat: @" %llu " ,localCrc64];
391- QCloudRemoveFileByPath (strongSelf.resumableTaskFile );
392- QCloudRemoveFileByPath (strongSelf.partCrc64Filepath );
393- if (![localCrc64Str isEqualToString: dic[@" crc64ecma" ]]){
394- // 下载完成之后如果crc64不一致,删除记录文件和已经下载的文件,重新开始下载
395- if (self.enablePartCrc64 == YES ) {
382+
383+ NSString *localCrc64Str = [NSString stringWithFormat: @" %llu " ,localCrc64];
384+ QCloudRemoveFileByPath (strongSelf.partCrc64Filepath );
385+ if (![localCrc64Str isEqualToString: dic[@" crc64ecma" ]]){
386+ // 下载完成之后如果crc64不一致,删除记录文件和已经下载的文件,重新开始下载
396387 self.crc64Start = 0 ;
397388 self.crc64Complete = 0 ;
398389 self.partCrc64Map = [NSMutableDictionary new ];
399390 self.localCacheDownloadOffset = 0 ;
391+ if (self.retryCount >= 3 ) {
392+ NSString * message = [NSString stringWithFormat: @" 本地文件与远端文件不一致,请重新下载:远端CRC64 值:%@ , 本地文件 CRC64值:%@ " ,
393+ dic[@" crc64ecma" ], localCrc64Str];
394+ strongSelf.finishBlock (nil , [NSError errorWithDomain: kQCloudNetworkDomain code: QCloudNetworkErrorCodeNotMatch userInfo: @{NSLocalizedDescriptionKey :message}]);
395+ return ;
396+ }else {
397+ QCloudRemoveFileByPath (strongSelf.downloadingURL .relativePath );
398+ self.retryCount ++;
399+ [self fakeStart ];
400+ }
401+ return ;
400402 }
401- QCloudRemoveFileByPath (strongSelf.downloadingURL .relativePath );
402- [self fakeStart ];
403- return ;
404403 }
405404 if (self.finishBlock ){
406405 strongSelf.finishBlock (outputObject, error);
0 commit comments