Skip to content

Commit 06ab35a

Browse files
committed
add crc for resume
1 parent d3b98cd commit 06ab35a

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

QiniuSDK/Common/QNCrc32.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,12 @@ + (UInt32)file:(NSString *)filePath
3232
int count = (len + kQNBlockSize - 1) / kQNBlockSize;
3333

3434
uLong crc = crc32(0L, Z_NULL, 0);
35-
NSLog(@"%lu %d", crc, count);
3635
for (int i = 0; i < count; i++) {
3736
int offset = i * kQNBlockSize;
3837
int size = (len - offset) > kQNBlockSize ? kQNBlockSize : (len - offset);
3938
NSData *d = [data subdataWithRange:NSMakeRange(offset, (unsigned int)size)];
4039
crc = crc32(crc, [d bytes], (uInt)[d length]);
4140
}
42-
NSLog(@"%lu %d", crc, count);
4341
return (UInt32)crc;
4442
}
4543
}

QiniuSDK/Storage/QNResumeUpload.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#import "QNHttpManager.h"
1515
#import "QNUploadOption+Private.h"
1616
#import "QNRecorderDelegate.h"
17+
#import "QNCrc32.h"
1718

1819
typedef void (^task)(void);
1920

@@ -34,6 +35,7 @@ @interface QNResumeUpload ()
3435
@property UInt64 modifyTime;
3536
@property (nonatomic, weak) id <QNRecorderDelegate> recorder;
3637

38+
@property UInt32 chunkCrc;
3739

3840
- (void)makeBlock:(NSString *)uphost
3941
offset:(UInt32)offset
@@ -216,8 +218,15 @@ - (void)nextTask:(UInt32)offset retriedTimes:(int)retried host:(NSString *)host
216218
[self nextTask:offset retriedTimes:retried + 1 host:nextHost];
217219
return;
218220
}
221+
222+
if (resp == nil) {
223+
[self nextTask:offset retriedTimes:retried host:host];
224+
return;
225+
}
226+
219227
NSString *ctx = resp[@"ctx"];
220-
if (ctx == nil) {
228+
NSNumber *crc = resp[@"crc32"];
229+
if (ctx == nil || crc == nil || [crc unsignedLongValue] != _chunkCrc) {
221230
[self nextTask:offset retriedTimes:retried host:host];
222231
return;
223232
}
@@ -252,6 +261,7 @@ - (void)makeBlock:(NSString *)uphost
252261
complete:(QNCompleteBlock)complete {
253262
NSData *data = [self.data subdataWithRange:NSMakeRange(offset, (unsigned int)chunkSize)];
254263
NSString *url = [[NSString alloc] initWithFormat:@"http://%@/mkblk/%u", uphost, (unsigned int)blockSize];
264+
_chunkCrc = [QNCrc32 data:data];
255265
[self post:url withData:data withCompleteBlock:complete withProgressBlock:progressBlock];
256266
}
257267

@@ -264,7 +274,7 @@ - (void)putChunk:(NSString *)uphost
264274
NSData *data = [self.data subdataWithRange:NSMakeRange(offset, (unsigned int)size)];
265275
UInt32 chunkOffset = offset % kQNBlockSize;
266276
NSString *url = [[NSString alloc] initWithFormat:@"http://%@/bput/%@/%u", uphost, context, (unsigned int)chunkOffset];
267-
277+
_chunkCrc = [QNCrc32 data:data];
268278
[self post:url withData:data withCompleteBlock:complete withProgressBlock:progressBlock];
269279
}
270280

0 commit comments

Comments
 (0)