@@ -237,39 +237,57 @@ - (void)uploadImage:(UIImage *)image path:(NSString *)path name:(NSString *)name
237
237
successBlock : (void (^)(AFHTTPRequestOperation *operation, id responseObject))success
238
238
failureBlock : (void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
239
239
progerssBlock : (void (^)(CGFloat progressValue))progress {
240
-
241
- NSData *data = [image dataForCodingUpload ];
242
240
243
- NSDateFormatter *formatter = [[NSDateFormatter alloc ] init ];
244
- formatter.dateFormat = @" yyyyMMddHHmmss" ;
245
- NSString *str = [formatter stringFromDate: [NSDate date ]];
246
- NSString *fileName = [NSString stringWithFormat: @" %@ _%@ .jpg" , [Login curLoginUser ].global_key, str];
241
+ NSData *data = [image dataForCodingUpload ];
242
+ NSString *fileName = [NSString stringWithFormat: @" %@ _%@ .jpg" , [Login curLoginUser ].global_key, [[NSDate date ] stringWithFormat: @" yyyyMMddHHmmss" ]];
247
243
DebugLog (@" \n uploadImageSize\n %@ : %.0f " , fileName, (float )data.length /1024 );
248
-
249
- AFHTTPRequestOperation *operation = [self POST: path parameters: nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
250
- [formData appendPartWithFileData: data name: name fileName: fileName mimeType: @" image/jpeg" ];
251
- } success: ^(AFHTTPRequestOperation *operation, id responseObject) {
252
- DebugLog (@" Success: %@ ***** %@ " , operation.responseString , responseObject);
253
- id error = [self handleResponse: responseObject];
254
- if (error && failure) {
255
- failure (operation, error);
256
- }else {
257
- success (operation, responseObject);
258
- }
259
- } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
260
- DebugLog (@" Error: %@ ***** %@ " , operation.responseString , error);
261
- if (failure) {
262
- failure (operation, error);
263
- }
264
- }];
265
244
266
- [operation setUploadProgressBlock: ^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
267
- CGFloat progressValue = (float )totalBytesWritten/(float )totalBytesExpectedToWrite;
268
- if (progress) {
269
- progress (progressValue);
270
- }
271
- }];
272
- [operation start ];
245
+ __weak typeof (self) weakSelf = self;
246
+ void (^uploadBlock)(NSDictionary *) = ^(NSDictionary *uploadParams){
247
+ AFHTTPRequestOperation *operation = [weakSelf POST: path parameters: uploadParams constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
248
+ [formData appendPartWithFileData: data name: name fileName: fileName mimeType: @" image/jpeg" ];
249
+ } success: ^(AFHTTPRequestOperation *operation, id responseObject) {
250
+ DebugLog (@" Success: %@ ***** %@ " , operation.responseString , responseObject);
251
+ id error = [self handleResponse: responseObject];
252
+ if (error && failure) {
253
+ failure (operation, error);
254
+ }else {
255
+ success (operation, responseObject);
256
+ }
257
+ } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
258
+ DebugLog (@" Error: %@ ***** %@ " , operation.responseString , error);
259
+ if (failure) {
260
+ failure (operation, error);
261
+ }
262
+ }];
263
+ [operation setUploadProgressBlock: ^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
264
+ CGFloat progressValue = (float )totalBytesWritten/(float )totalBytesExpectedToWrite;
265
+ if (progress) {
266
+ progress (progressValue);
267
+ }
268
+ }];
269
+ [operation start ];
270
+ };
271
+ if ([path isEqualToString: @" https://up.qbox.me/" ]) {// 先拿 token
272
+ NSDictionary *params = @{
273
+ @" fileName" : fileName,
274
+ @" fileSize" : @(data.length )
275
+ };
276
+ [[CodingNetAPIClient sharedJsonClient ] requestJsonDataWithPath: @" api/upload_token/public/images" withParams: params withMethodType: Get andBlock: ^(id data, NSError *error) {
277
+ if (data) {
278
+ NSDictionary *result = data[@" data" ];
279
+ NSMutableDictionary *uploadParams = @{}.mutableCopy ;
280
+ uploadParams[@" token" ] = result[@" uptoken" ];
281
+ uploadParams[@" x:time" ] = result[@" time" ];
282
+ uploadParams[@" x:authToken" ] = result[@" authToken" ];
283
+ uploadParams[@" x:userId" ] = result[@" userId" ];
284
+ uploadParams[@" key" ] = fileName;
285
+ uploadBlock (uploadParams);
286
+ }
287
+ }];
288
+ }else {
289
+ uploadBlock (nil );
290
+ }
273
291
}
274
292
275
293
- (void )uploadAssets : (NSArray *)assets
0 commit comments