AliOSSUploadMangae.m
4.47 KB
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
//
// AliOSSUploadMangae.m
// IM_ios_client
//
// Created by 赵世强 on 2018/3/16.
// Copyright © 2018年 xiniu. All rights reserved.
//
#import "AliOSSUploadMangae.h"
#import "FileUploadAuthInfoGetRequest.h"
#import "FileUploadAuthInfoGetResponse.h"
@implementation AliOSSUploadMangae
{
OSSClient *client;
}
#pragma mark 创建单例
/**
* @author zsq
*
* @brief 创建单例
*
* @return self
*/
+ (instancetype)sharedInstance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
+ (void)getUrlWithAliossUploadWith:(NSString *)passportId name:(NSString *)fileName data:(NSData *)data url:(IMAGEURLBLOCK)block baseView:(xn_base_BaseViewController *)vc{
FileUploadAuthInfoGetRequest *req_FileUploadAuthInfoGet = [[FileUploadAuthInfoGetRequest alloc]init];
[req_FileUploadAuthInfoGet.baseRequest setSession:passportId];
[req_FileUploadAuthInfoGet.baseRequest setMethod:req_FileUploadAuthInfoGet.apimethodName];
[req_FileUploadAuthInfoGet setName:fileName];
[[SharedDataManager sharedInstance].NIM_Client_AF f_sendRequest:req_FileUploadAuthInfoGet.baseRequest isPost:YES completion:^(NSDictionary *_dict) {
BaseResponse *_res_base = (BaseResponse *)[_dict objectForKey:kResponseNotiKey_baseResponse];
NSString *_str_error = [_dict objectForKey:kResponseNotiKey_error];
if (![_str_error isEqualToString:kNoError]) {
// 出错
[vc f_HUD_hide];
[[AlertHandler sharedInstance] showToastHUD:[CommonFun sharedInstance].topViewController.view message:@"文件上传失败,请稍后再试" sleepTime:Toast_Sleep];
}else{
FileUploadAuthInfoGetResponse *res_FileUploadAuthInfoGet = [[FileUploadAuthInfoGetResponse alloc]init];
res_FileUploadAuthInfoGet.baseResponse = _res_base;
id<OSSCredentialProvider> credential = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:res_FileUploadAuthInfoGet.accessKeyId secretKeyId:res_FileUploadAuthInfoGet.accessKeySecret securityToken:res_FileUploadAuthInfoGet.securityToken];
OSSClient *_client = [[OSSClient alloc] initWithEndpoint:res_FileUploadAuthInfoGet.info.endpoint credentialProvider:credential];
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
// 必填字段
put.bucketName = res_FileUploadAuthInfoGet.info.bucket;
NSString *string = res_FileUploadAuthInfoGet.info.fileUrl;
if ([[string substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"/"]) {
string = [res_FileUploadAuthInfoGet.info.fileUrl substringWithRange:NSMakeRange(1, res_FileUploadAuthInfoGet.info.fileUrl.length-1)];
}
put.objectKey = string;
put.uploadingData = data;
OSSTask * putTask = [_client putObject:put];
[putTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
NSString * constrainURL = nil;
OSSTask *result = [_client presignPublicURLWithBucketName:res_FileUploadAuthInfoGet.info.bucket
withObjectKey:string];
if (!result.error) {
constrainURL = result.result;
block(constrainURL);
} else {
}
} else {
}
return nil;
}];
}
}];
}
- (UIViewController *)topViewController {
UIViewController *resultVC;
resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
while (resultVC.presentedViewController) {
resultVC = [self _topViewController:resultVC.presentedViewController];
}
return resultVC;
}
- (UIViewController *)_topViewController:(UIViewController *)vc {
if ([vc isKindOfClass:[UINavigationController class]]) {
return [self _topViewController:[(UINavigationController *)vc topViewController]];
} else if ([vc isKindOfClass:[UITabBarController class]]) {
return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
} else {
return vc;
}
return nil;
}
@end