Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add base64 for iOS 6 #86

Merged
merged 8 commits into from
Aug 10, 2014
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
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: objective-c
xcode_project: QiniuSDK.xcodeproj
xcode_scheme: QiniuSDKTests
xcode_sdk: iphonesimulator7.0
xcode_sdk:
- iphonesimulator7.1
4 changes: 2 additions & 2 deletions QiniuSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 6.1;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
};
Expand Down Expand Up @@ -424,7 +424,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 6.1;
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
};
Expand Down
14 changes: 7 additions & 7 deletions QiniuSDK/QiniuHttpClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ - (AFHTTPRequestOperation *)uploadFile:(NSString *)filePath
uphost:(NSString *)uphost
progress:(void (^)(float percent))progressBlock
complete:(QNObjectResultBlock)complete{

NSParameterAssert(filePath);
NSParameterAssert(token);
NSError *error = nil;
[[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error] fileSize];

if (error) {
complete(nil,error);
return nil;
}

NSData *data = [NSData dataWithContentsOfFile:filePath];
return [self uploadFileData:data
key:key
Expand All @@ -62,7 +62,7 @@ - (AFHTTPRequestOperation *)uploadFileData:(NSData *)data

NSParameterAssert(data);
NSParameterAssert(token);

NSMutableDictionary *parameters = [NSMutableDictionary dictionary];

if (key && ![key isEqualToString:kQiniuUndefinedKey]) {
Expand All @@ -73,7 +73,7 @@ - (AFHTTPRequestOperation *)uploadFileData:(NSData *)data
}

parameters[@"token"] = token;

if (extra) {
[parameters addEntriesFromDictionary:extra.convertToPostParams];
}
Expand All @@ -87,7 +87,7 @@ - (AFHTTPRequestOperation *)uploadFileData:(NSData *)data
URLString:uphost
parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

[formData appendPartWithFileData:data
name:@"file"
fileName:key
Expand Down Expand Up @@ -129,7 +129,7 @@ @implementation QiniuPutExtra
- (NSDictionary *)convertToPostParams{
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:self.params];
if (self.checkCrc == 1) {
params[@"crc32"] = [NSString stringWithFormat:@"%ld", self.crc32];
params[@"crc32"] = [NSString stringWithFormat:@"%u", (unsigned int)self.crc32];
}
return params;
}
Expand Down
7 changes: 1 addition & 6 deletions QiniuSDK/QiniuResumableClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,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:@"_"];
return str;
return urlSafeBase64String(str);
}

- (void)mkfile:(NSString *)key
Expand Down
1 change: 1 addition & 0 deletions QiniuSDK/QiniuUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
typedef void (^QNProgress)(float percent);
typedef void (^QNComplete)(AFHTTPRequestOperation *operation, NSError *error);

NSString *urlSafeBase64String(NSString *sourceString);

NSError *qiniuError(int errorCode, NSString *errorDescription);

Expand Down
29 changes: 29 additions & 0 deletions QiniuSDK/QiniuUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@
#define kQiniuErrorKey @"error"
#define kQiniuErrorDomain @"QiniuErrorDomain"

NSString *urlSafeBase64String(NSString *sourceString) {
NSData *data = [NSData dataWithBytes:[sourceString UTF8String] length:[sourceString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
NSUInteger length = [data length];
NSMutableData *mutableData = [NSMutableData dataWithLength:((length + 2) / 3) * 4];

uint8_t *input = (uint8_t *)[data bytes];
uint8_t *output = (uint8_t *)[mutableData mutableBytes];

for (NSUInteger i = 0; i < length; i += 3) {
NSUInteger value = 0;
for (NSUInteger j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}

static uint8_t const kAFBase64EncodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

NSUInteger idx = (i / 3) * 4;
output[idx + 0] = kAFBase64EncodingTable[(value >> 18) & 0x3F];
output[idx + 1] = kAFBase64EncodingTable[(value >> 12) & 0x3F];
output[idx + 2] = (i + 1) < length ? kAFBase64EncodingTable[(value >> 6) & 0x3F] : '=';
output[idx + 3] = (i + 2) < length ? kAFBase64EncodingTable[(value >> 0) & 0x3F] : '=';
}

return [[NSString alloc] initWithData:mutableData encoding:NSASCIIStringEncoding];
}

NSError *qiniuError(int errorCode, NSString *errorDescription) {
return [NSError errorWithDomain:kQiniuErrorDomain code:errorCode userInfo:[NSDictionary dictionaryWithObject:errorDescription forKey:kQiniuErrorKey]];
}
Expand Down