Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
## CHANGE LOG

### v6.2.7

- [#63] 添加额外错误信息
- [#64] mkfile 支持 key 为 nil
- [#65] 修正 传入的blockComplete在第一次运行的退出时被释放,如果失败重试会变成nil,导致出错
- [#70] 修改上传域名为upload.qiniu.com
- [#72] operation.responseObject 返回增加nil 判断

### v6.2.6

- [#61] 错误信息增加reqid

### v6.2.5

- [#50] update AFN 2.2.3
- [#52] bugfix: 701 retry

### v6.2.2

2014-04-09 issue [#44](https://github.com/qiniu/ios-sdk/pull/44)
Expand Down
3 changes: 2 additions & 1 deletion QiniuSDK/QiniuConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
#define kQiniuUpHost @"http://upload.qiniu.com"

#define kQiniuUndefinedKey @"?"
#define kQiniuUserAgent @"qiniu-ios-sdk"

#define kQiniuVersion @"6.2.7"
5 changes: 3 additions & 2 deletions QiniuSDK/QiniuHttpClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#import "QiniuHttpClient.h"
#import "QiniuConfig.h"
#import "QiniuUtils.h"

@implementation QiniuHttpClient

Expand Down Expand Up @@ -89,7 +90,7 @@ - (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)theR
failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure{

NSMutableURLRequest *request = (NSMutableURLRequest *)theRequest;
[request addValue:kQiniuUserAgent forHTTPHeaderField:@"User-Agent"];
[request addValue:qiniuUserAgent() forHTTPHeaderField:@"User-Agent"];
AFHTTPRequestOperation *operation = [super HTTPRequestOperationWithRequest:request success:success failure:failure];

return operation;
Expand All @@ -103,7 +104,7 @@ @implementation QiniuPutExtra
- (NSDictionary *)convertToPostParams{
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:self.params];
if (self.checkCrc == 1) {
params[@"crc32"] = [NSString stringWithFormat:@"%u", (unsigned int)self.crc32];
params[@"crc32"] = [NSString stringWithFormat:@"%ld", self.crc32];
}
return params;
}
Expand Down
67 changes: 34 additions & 33 deletions QiniuSDK/QiniuResumableClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "QiniuResumableClient.h"
#import "QiniuConfig.h"
#import "QiniuUtils.h"

@implementation QiniuResumableClient

Expand All @@ -20,11 +21,11 @@ - (QiniuResumableClient *)initWithToken:(NSString *)token
self.token = [[NSString alloc] initWithFormat:@"UpToken %@", token];
self.retryTime = tryTime;
self.chunkSize = chunkSize; // 256k

self.responseSerializer = [AFJSONResponseSerializer serializer];
self.operationQueue = [[NSOperationQueue alloc] init];
[self.operationQueue setMaxConcurrentOperationCount:maxWorkers];

return self;
}

Expand All @@ -47,10 +48,10 @@ - (void)mkblock:(NSData *)mappedData
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:callUrl]];

[self setHeaders:request];

NSData *postData = [mappedData subdataWithRange:NSMakeRange(offset, bodyLength)];
[request setHTTPBody:postData];

QNCompleteBlock success = ^(AFHTTPRequestOperation *operation, id responseObject)
{
// TODO: check crc32
Expand All @@ -60,7 +61,7 @@ - (void)mkblock:(NSData *)mappedData
{
complete(operation, error);
};

AFHTTPRequestOperation *operation = [super HTTPRequestOperationWithRequest:request
success:success
failure:failure];
Expand All @@ -79,12 +80,12 @@ - (void)chunkPut:(NSData *)mappedData
}
NSString *callUrl = [[NSString alloc] initWithFormat:@"%@/bput/%@/%d", blockPutRet.host, blockPutRet.ctx, (unsigned int)blockPutRet.offset];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:callUrl]];

[self setHeaders:request];

NSData *postData = [mappedData subdataWithRange:NSMakeRange(offsetBase + blockPutRet.offset, bodyLength)];
[request setHTTPBody:postData];

QNCompleteBlock success = ^(AFHTTPRequestOperation *operation, id responseObject)
{
// TODO: check crc32
Expand All @@ -94,7 +95,7 @@ - (void)chunkPut:(NSData *)mappedData
{
complete(operation, error);
};

AFHTTPRequestOperation *operation = [super HTTPRequestOperationWithRequest:request
success:success
failure:failure];
Expand All @@ -109,21 +110,21 @@ - (void)blockPut:(NSData *)mappedData
complete:(QNCompleteBlock)complete
{
// @autoreleasepool {


UInt32 offsetBase = blockIndex << QiniuBlockBits;

__block UInt32 bodyLength = self.chunkSize < blockSize ? self.chunkSize : blockSize;
__block QiniuBlkputRet *blockPutRet;
__block UInt32 retryTime = self.retryTime;
__block BOOL isMkblock = YES;

QNCompleteBlock __block __weak weakChunkComplete;
QNCompleteBlock chunkComplete;
weakChunkComplete = chunkComplete = ^(AFHTTPRequestOperation *operation, NSError *error)
{
if (error != nil) {

if (retryTime == 0 || isMkblock || [operation.response statusCode] == 701) {
complete(operation, error);
return;
Expand All @@ -137,24 +138,24 @@ - (void)blockPut:(NSData *)mappedData
retryTime = self.retryTime;
isMkblock = NO;
blockPutRet = [[QiniuBlkputRet alloc] initWithDictionary:operation.responseObject];

UInt32 remainLength = blockSize - blockPutRet.offset;
bodyLength = self.chunkSize < remainLength ? self.chunkSize : remainLength;
}

if (blockPutRet.offset == blockSize) {
complete(operation, nil);
return;
}

[self chunkPut:mappedData
blockPutRet:blockPutRet
offsetBase:offsetBase
bodyLength:bodyLength
progress:progressBlock
complete:weakChunkComplete];
};

[self mkblock:mappedData
offsetBase:offsetBase
blockSize:blockSize
Expand All @@ -167,7 +168,7 @@ - (void)blockPut:(NSData *)mappedData
+ (NSString *)encode:(NSString *)str
{
str = [[str dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];

// is there other methed?
str = [str stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
str = [str stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
Expand All @@ -180,31 +181,31 @@ - (void)mkfile:(NSString *)key
progress:(QNProgressBlock)progressBlock
complete:(QNCompleteBlock)complete
{

NSString *mimeStr = extra.mimeType == nil ? @"" : [[NSString alloc] initWithFormat:@"/mimetype/%@", [QiniuResumableClient encode:extra.mimeType]];

NSString *callUrl = [[NSString alloc] initWithFormat:@"%@/mkfile/%u%@", kQiniuUpHost, (unsigned int)fileSize, mimeStr];

if (key != nil) {
NSString *keyStr = [[NSString alloc] initWithFormat:@"/key/%@", [QiniuResumableClient encode:key]];
callUrl = [NSString stringWithFormat:@"%@%@", callUrl, keyStr];
}

if (extra.params != nil) {
NSEnumerator *e = [extra.params keyEnumerator];
for (id key = [e nextObject]; key != nil; key = [e nextObject]) {
callUrl = [NSString stringWithFormat:@"%@/%@/%@", callUrl, key, [QiniuResumableClient encode:[extra.params objectForKey:key]]];
}
}

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:callUrl]];
[self setHeaders:request];

NSMutableData *postData = [NSMutableData data];
NSString *bodyStr = [extra.progresses componentsJoinedByString:@","];
[postData appendData:[bodyStr dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postData];

QNCompleteBlock success = ^(AFHTTPRequestOperation *operation, id responseObject)
{
complete(operation, nil);
Expand All @@ -213,7 +214,7 @@ - (void)mkfile:(NSString *)key
{
complete(operation, error);
};

AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request
success:success
failure:failure];
Expand All @@ -224,7 +225,7 @@ - (void)setHeaders:(NSMutableURLRequest *)request
{
[request setValue:self.token forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/octet-stream" forHTTPHeaderField:@"Content-Type"];
[request addValue:kQiniuUserAgent forHTTPHeaderField:@"User-Agent"];
[request addValue:qiniuUserAgent() forHTTPHeaderField:@"User-Agent"];
[request setHTTPMethod:@"POST"];
}

Expand Down Expand Up @@ -263,14 +264,14 @@ - (BOOL) blockUploadedAndCheck
if (blockNumlock == nil) {
blockNumlock = [[NSLock alloc] init];
}

BOOL allBlockOk;

[blockNumlock lock];
self.uploadedBlockNumber ++;
allBlockOk = self.uploadedBlockNumber == self.blockCount;
[blockNumlock unlock];

return allBlockOk;
}

Expand All @@ -280,13 +281,13 @@ - (float) chunkUploadedAndPercent
if (chunkNumlock == nil) {
chunkNumlock = [[NSLock alloc] init];
}

float percent;
[chunkNumlock lock];
self.uploadedChunkNumber ++;
percent = (float)self.uploadedChunkNumber / self.chunkCount;
[chunkNumlock unlock];

return percent;
}

Expand Down
4 changes: 1 addition & 3 deletions QiniuSDK/QiniuSimpleUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#import "QiniuUtils.h"
#import "QiniuHttpClient.h"

#define kQiniuUserAgent @"qiniu-ios-sdk"

@interface QiniuSimpleUploader ()
//@property(nonatomic,copy)NSString *filePath;
//@property(nonatomic,assign)uint64_t *fileSize;
Expand Down Expand Up @@ -58,7 +56,7 @@ - (void) uploadFile:(NSString *)filePath
[self.delegate uploadSucceeded:filePath ret:resp];
}
}];

}

@end
Expand Down
2 changes: 2 additions & 0 deletions QiniuSDK/QiniuUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
NSError *qiniuError(int errorCode, NSString *errorDescription);

NSError *qiniuErrorWithOperation(AFHTTPRequestOperation *operation, NSError *error);

NSString *qiniuUserAgent();
19 changes: 13 additions & 6 deletions QiniuSDK/QiniuUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// Created by Qiniu Developers 2013
//

#import <UIKit/UIKit.h>

#import "QiniuConfig.h"
#import "QiniuUtils.h"

#define kQiniuErrorKey @"error"
Expand All @@ -15,25 +18,29 @@
}

NSError *qiniuErrorWithOperation(AFHTTPRequestOperation *operation, NSError *error) {

if (operation == nil || operation.responseObject == nil) {
return error;
}

NSMutableDictionary *userInfo = nil;
NSInteger errorCode = -1;

if ([operation.responseObject isKindOfClass:NSDictionary.class]) {
userInfo = [NSMutableDictionary dictionaryWithDictionary:operation.responseObject];
}
errorCode = [operation.response statusCode];

if (!userInfo) {
userInfo = [NSMutableDictionary init];
}

NSString *reqid = [[operation.response allHeaderFields] objectForKey:@"X-Reqid"];
[userInfo setObject:reqid forKey:@"reqid"];

return [NSError errorWithDomain:kQiniuErrorDomain code:errorCode userInfo:userInfo];
}

NSString *qiniuUserAgent() {
return [NSString stringWithFormat:@"Qiniu-iOS/%@ (%@; iOS %@; )", kQiniuVersion, [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion]];
}