Skip to content

Commit b33e412

Browse files
committed
add crc32 for formupload
1 parent 9cb4c1b commit b33e412

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

QiniuSDK/Common/QNCrc32.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ + (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);
3536
for (int i = 0; i < count; i++) {
3637
int offset = i * kQNBlockSize;
3738
int size = (len - offset) > kQNBlockSize ? kQNBlockSize : (len - offset);
3839
NSData *d = [data subdataWithRange:NSMakeRange(offset, (unsigned int)size)];
3940
crc = crc32(crc, [d bytes], (uInt)[d length]);
4041
}
42+
NSLog(@"%lu %d", crc, count);
4143
return (UInt32)crc;
4244
}
4345
}

QiniuSDK/Storage/QNResumeUpload.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ - (void)putChunk:(NSString *)uphost
265265
UInt32 chunkOffset = offset % kQNBlockSize;
266266
NSString *url = [[NSString alloc] initWithFormat:@"http://%@/bput/%@/%u", uphost, context, (unsigned int)chunkOffset];
267267

268-
// Todo: check crc
269268
[self post:url withData:data withCompleteBlock:complete withProgressBlock:progressBlock];
270269
}
271270

QiniuSDK/Storage/QNUploadManager.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#import "QNConfig.h"
1212
#import "QNHttpManager.h"
1313
#import "QNResponseInfo.h"
14-
14+
#import "QNCrc32.h"
1515
#import "QNUploadManager.h"
1616
#import "QNResumeUpload.h"
1717
#import "QNUploadOption+Private.h"
@@ -71,7 +71,7 @@ - (void)putData:(NSData *)data
7171

7272
parameters[@"token"] = token;
7373

74-
if (option.params) {
74+
if (option && option.params) {
7575
[parameters addEntriesFromDictionary:[option p_convertToPostParams]];
7676
}
7777

@@ -81,6 +81,10 @@ - (void)putData:(NSData *)data
8181
mimeType = @"application/octet-stream";
8282
}
8383

84+
if (option && option.checkCrc) {
85+
parameters[@"crc32"] = [NSNumber numberWithUnsignedLong:[QNCrc32 data:data]];
86+
}
87+
8488
QNInternalProgressBlock p = nil;
8589

8690
if (option && option.progressHandler) {

QiniuSDKTests/QNCrc32Test.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import <XCTest/XCTest.h>
1010
#import "QNCrc32.h"
11+
#import "QNTempFile.h"
1112

1213
@interface QNCrc32Test : XCTestCase
1314

@@ -22,4 +23,13 @@ - (void)testData {
2223
XCTAssert(crc == 3964322768, @"Pass");
2324
}
2425

26+
- (void)testFile {
27+
NSError *error;
28+
NSURL *file = [QNTempFile createTempfileWithSize:5 * 1024 * 1024];
29+
UInt32 u = [QNCrc32 file:[file relativePath] error:&error];
30+
31+
XCTAssert(u == 3376132981, @"Pass");
32+
[QNTempFile removeTempfile:file];
33+
}
34+
2535
@end

QiniuSDKTests/QNFormUploadTest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ - (void)testUp {
3535
__block QNResponseInfo *testInfo = nil;
3636
__block NSDictionary *testResp = nil;
3737

38-
QNUploadOption *opt = [[QNUploadOption alloc] initWithMime:@"text/plain" progressHandler:nil params:@{ @"foo":@"bar" } checkCrc:NO cancellationSignal:nil];
38+
QNUploadOption *opt = [[QNUploadOption alloc] initWithMime:@"text/plain" progressHandler:nil params:@{ @"x:foo":@"bar" } checkCrc:YES cancellationSignal:nil];
3939
NSData *data = [@"Hello, World!" dataUsingEncoding : NSUTF8StringEncoding];
4040
[self.upManager putData:data key:@"你好" token:g_token complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
4141
testInfo = info;

0 commit comments

Comments
 (0)