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
140
141
142
143
144
145
146
147
148
149
//
// QNFormUpload.m
// QiniuSDK
//
// Created by bailong on 15/1/4.
// Copyright (c) 2015年 Qiniu. All rights reserved.
//
#import "QNFormUpload.h"
#import "QNConfiguration.h"
#import "QNCrc32.h"
#import "QNRecorderDelegate.h"
#import "QNResponseInfo.h"
#import "QNUploadManager.h"
#import "QNUploadOption+Private.h"
#import "QNUrlSafeBase64.h"
@interface QNFormUpload ()
@property (nonatomic, strong) NSData *data;
@property (nonatomic, strong) id<QNHttpDelegate> httpManager;
@property (nonatomic) int retryTimes;
@property (nonatomic, strong) NSString *key;
@property (nonatomic, strong) QNUpToken *token;
@property (nonatomic, strong) QNUploadOption *option;
@property (nonatomic, strong) QNUpCompletionHandler complete;
@property (nonatomic, strong) QNConfiguration *config;
@property (nonatomic) float previousPercent;
@property (nonatomic, strong) NSString *access; //AK
@end
@implementation QNFormUpload
- (instancetype)initWithData:(NSData *)data
withKey:(NSString *)key
withToken:(QNUpToken *)token
withCompletionHandler:(QNUpCompletionHandler)block
withOption:(QNUploadOption *)option
withHttpManager:(id<QNHttpDelegate>)http
withConfiguration:(QNConfiguration *)config {
if (self = [super init]) {
_data = data;
_key = key;
_token = token;
_option = option != nil ? option : [QNUploadOption defaultOptions];
_complete = block;
_httpManager = http;
_config = config;
_previousPercent = 0;
_access = token.access;
}
return self;
}
- (void)put {
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
NSString *fileName = _key;
if (_key) {
parameters[@"key"] = _key;
} else {
fileName = @"?";
}
parameters[@"token"] = _token.token;
[parameters addEntriesFromDictionary:_option.params];
parameters[@"crc32"] = [NSString stringWithFormat:@"%u", (unsigned int)[QNCrc32 data:_data]];
QNInternalProgressBlock p = ^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {
float percent = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
if (percent > 0.95) {
percent = 0.95;
}
if (percent > _previousPercent) {
_previousPercent = percent;
} else {
percent = _previousPercent;
}
_option.progressHandler(_key, percent);
};
__block NSString *upHost = [_config.zone up:_token isHttps:_config.useHttps frozenDomain:nil];
QNCompleteBlock complete = ^(QNResponseInfo *info, NSDictionary *resp) {
if (info.isOK) {
_option.progressHandler(_key, 1.0);
}
if (info.isOK || !info.couldRetry) {
_complete(info, _key, resp);
return;
}
if (_option.cancellationSignal()) {
_complete([QNResponseInfo cancel], _key, nil);
return;
}
__block NSString *nextHost = upHost;
if (info.isConnectionBroken || info.needSwitchServer) {
nextHost = [_config.zone up:_token isHttps:_config.useHttps frozenDomain:nextHost];
}
QNCompleteBlock retriedComplete = ^(QNResponseInfo *info, NSDictionary *resp) {
if (info.isOK) {
_option.progressHandler(_key, 1.0);
}
if (info.isOK || !info.couldRetry) {
_complete(info, _key, resp);
return;
}
if (_option.cancellationSignal()) {
_complete([QNResponseInfo cancel], _key, nil);
return;
}
NSString *thirdHost = nextHost;
if (info.isConnectionBroken || info.needSwitchServer) {
thirdHost = [_config.zone up:_token isHttps:_config.useHttps frozenDomain:nextHost];
}
QNCompleteBlock thirdComplete = ^(QNResponseInfo *info, NSDictionary *resp) {
if (info.isOK) {
_option.progressHandler(_key, 1.0);
}
_complete(info, _key, resp);
};
[_httpManager multipartPost:thirdHost
withData:_data
withParams:parameters
withFileName:fileName
withMimeType:_option.mimeType
withCompleteBlock:thirdComplete
withProgressBlock:p
withCancelBlock:_option.cancellationSignal
withAccess:_access];
};
[_httpManager multipartPost:nextHost
withData:_data
withParams:parameters
withFileName:fileName
withMimeType:_option.mimeType
withCompleteBlock:retriedComplete
withProgressBlock:p
withCancelBlock:_option.cancellationSignal
withAccess:_access];
};
[_httpManager multipartPost:upHost
withData:_data
withParams:parameters
withFileName:fileName
withMimeType:_option.mimeType
withCompleteBlock:complete
withProgressBlock:p
withCancelBlock:_option.cancellationSignal
withAccess:_access];
}
@end