-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathQiniuUploader.m
executable file
·139 lines (122 loc) · 6.49 KB
/
QiniuUploader.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// QiniuUploader by @hlcfan
#import "QiniuConfig.h"
#import "QiniuUploader.h"
#import "GTMBase64/GTMBase64.h"
#import "JSONKit/JSONKit.h"
#import <AFNetworking.h>
#import "QiniuPutPolicy.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>
#define kQiniuUserAgent @"qiniu-ios-sdk"
@interface QiniuUploader ()
@property (nonatomic, strong) AFURLSessionManager *manager;
@end
@implementation QiniuUploader
- (id)initWithAccessKey:(NSString *)accessKey secretKey:(NSString *)secretKey {
if (self = [super init]) {
self.accessKey = accessKey;
self.secretKey = secretKey;
}
return self;
}
- (NSString *)tokenWithScope:(NSString *)scope {
QiniuPutPolicy *policy = [QiniuPutPolicy new];
policy.scope = scope;
return [policy makeToken:self.accessKey secretKey:self.secretKey];
}
- (void) uploadFile:(NSArray *)filesInfo progress:(void(^)(float percentage))progressBlock complete:(void(^)(NSMutableDictionary *result)) block {
NSMutableArray *mutableOperations = [NSMutableArray array];
NSMutableDictionary *ret = [NSMutableDictionary dictionary];
for (NSDictionary *fileData in filesInfo) {
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:[fileData objectForKey:@"token"] forKey:@"token"];
if (![[fileData objectForKey:@"key"] isEqualToString:kQiniuUndefinedKey]) {
[params setObject:[fileData objectForKey:@"key"] forKey:@"key"];
}
NSString *mimeType = nil;
QiniuPutExtra *extra = (QiniuPutExtra*)[fileData objectForKey:@"extra"];
if (extra) {
mimeType = extra.mimeType;
if (extra.checkCrc == 1) {
[params setObject: [NSString stringWithFormat:@"%ld", extra.crc32] forKey:@"crc32"];
}
for (NSString *key in extra.params) {
[params setObject:[extra.params objectForKey:key] forKey:key];
}
}
NSString *filePath = [fileData objectForKey:@"filepath"];
NSURL *fileURL = [NSURL fileURLWithPath: filePath];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:kQiniuUpHost parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (mimeType != nil) {
[formData appendPartWithFileURL:fileURL name:@"file" fileName:filePath mimeType:mimeType error:nil];
} else {
[formData appendPartWithFileURL:fileURL name:@"file" error:nil];
}
}];
[request setValue:kQiniuUserAgent forHTTPHeaderField:@"User-Agent"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[mutableOperations addObject:operation];
}
__block BOOL all_success = YES;
NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperations progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
if(progressBlock) progressBlock((float)numberOfFinishedOperations / totalNumberOfOperations);
// NSLog(@"%d of %d complete", numberOfFinishedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
[ret setObject:operations forKey:@"operations"];
for (AFHTTPRequestOperation *op in operations) {
if ([[op response] statusCode] != 200 || op == nil) {
all_success = NO;
break;
}
}
[ret setObject:[NSNumber numberWithBool:all_success] forKey:@"success"];
if (block) block(ret);
}];
[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];
}
+ (NSDictionary *)prepareUploadContent:(NSDictionary *)theInfo filename:(NSString *)filename format:(NSString*)format bucket: (NSString *)bucket imageCompress:(UIImage*(^)(UIImage *image)) block {
//extracting image from the picker and saving it
NSString *mediaType = [theInfo objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage] || [mediaType isEqualToString:(NSString *)ALAssetTypePhoto]) {
NSString *key = [NSString stringWithFormat:@"%@.%@", filename, format];
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:key];
NSLog(@"Upload Path: %@", filePath);
UIImage *newImage;
if(block) {
newImage = block([theInfo objectForKey:UIImagePickerControllerOriginalImage]);
}else{
newImage = [theInfo objectForKey:UIImagePickerControllerOriginalImage];
}
// UIImage *ori_image = [theInfo objectForKey:UIImagePickerControllerOriginalImage];
// CGSize newSize = CGSizeMake(100.0f, 100.0f);
// UIGraphicsBeginImageContext(newSize);
// [ori_image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
// UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
NSData *webData = UIImageJPEGRepresentation(newImage, 1);
[webData writeToFile:filePath atomically:YES];
NSDictionary *upload_data = [NSDictionary dictionaryWithObjectsAndKeys: @YES, @"success", filePath, @"filepath", bucket, @"bucket", key, @"key", nil];
return upload_data;
}else {
return @{@"success": @NO};
}
}
- (BOOL)startUploadFiles:(NSArray *)uploadArray progress:(void(^)(float percentage))progressBlock complete:(void(^)(NSMutableDictionary *result)) block {
NSFileManager *manager = [NSFileManager defaultManager];
NSMutableArray *array = [NSMutableArray array];
for (NSDictionary *item in uploadArray) {
NSLog(@"===Item:%@", [item objectForKey:@"filepath"]);
if ([manager fileExistsAtPath:[item objectForKey:@"filepath"]]) {
NSLog(@"Scope:%@", [NSString stringWithFormat:@"%@:%@", [item objectForKey:@"bucket"], [item objectForKey:@"key"]]);
NSDictionary *uploadData = [NSDictionary dictionaryWithObjectsAndKeys: [item objectForKey:@"filepath"], @"filepath", [self tokenWithScope:[NSString stringWithFormat:@"%@:%@", [item objectForKey:@"bucket"], [item objectForKey:@"key"]]], @"token", [item objectForKey:@"key"], @"key", nil];
[array addObject:uploadData];
}
}
[self uploadFile:array progress:^(float percentage) {
if(progressBlock) progressBlock(percentage);
} complete:^(NSMutableDictionary *result) {
if(block) block(result);
}];
return YES;
}
@end