|
@@ -0,0 +1,321 @@
|
|
|
+//
|
|
|
+// KSUploadManager.m
|
|
|
+// KulexiuForTeacher
|
|
|
+//
|
|
|
+// Created by 王智 on 2022/5/8.
|
|
|
+//
|
|
|
+
|
|
|
+#import "KSUploadManager.h"
|
|
|
+#import <KS3YunSDK.h>
|
|
|
+#import "NSDate+Extension.h"
|
|
|
+
|
|
|
+#define BUCKET_DOMAIN (@"ks3-cn-beijing.ksyuncs.com/daya")
|
|
|
+@interface KSUploadManager ()<KingSoftServiceRequestDelegate>
|
|
|
+
|
|
|
+@property (nonatomic, copy) KSUploadSuccess successCallback;
|
|
|
+
|
|
|
+@property (nonatomic, copy) KSUploadFailer faliureCallback;
|
|
|
+
|
|
|
+@property (nonatomic, copy) KSUploadProgress uploadProgress;
|
|
|
+
|
|
|
+@property (nonatomic, strong) NSString *videoLinkUrl;
|
|
|
+
|
|
|
+@property (assign, nonatomic) long long fileSize;
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation KSUploadManager
|
|
|
++ (instancetype)shareInstance {
|
|
|
+ static KSUploadManager *manager = nil;
|
|
|
+ static dispatch_once_t onceToken;
|
|
|
+ dispatch_once(&onceToken, ^{
|
|
|
+ manager = [[KSUploadManager alloc] init];
|
|
|
+ [manager configCilentBucket];
|
|
|
+ });
|
|
|
+ return manager;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)configCilentBucket {
|
|
|
+ [[KS3Client initialize] setBucketDomain:BUCKET_DOMAIN];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)uploadImage:(NSData *)imageData fileName:(NSString *)fileName successCallback:(KSUploadSuccess)success faliure:(KSUploadFailer)faliure {
|
|
|
+ if (success) {
|
|
|
+ self.successCallback = success;
|
|
|
+ }
|
|
|
+ if (faliure) {
|
|
|
+ self.faliureCallback = faliure;
|
|
|
+ }
|
|
|
+ NSString *uploadFileName = [NSString stringWithFormat:@"%@%@%@",[NSDate getCurrentTimestamp], fileName,[UIImage typeForImageData:imageData]];
|
|
|
+ NSString *keyValue = uploadFileName;
|
|
|
+ [KSNetworkingManager getUploadSignRequest:KS_POST fileName:uploadFileName keyName:keyValue success:^(NSDictionary * _Nonnull dic) {
|
|
|
+ if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
|
|
|
+ KS3AccessControlList *acl = [[KS3AccessControlList alloc] init];
|
|
|
+ [acl setContronAccess:KingSoftYun_Permission_Public_Read];
|
|
|
+
|
|
|
+ KS3PutObjectRequest *putObjRequest = [[KS3PutObjectRequest alloc] initWithName:@"" withAcl:acl grantAcl:nil];
|
|
|
+ // token
|
|
|
+ [putObjRequest setStrKS3Token:[dic stringValueForKey:@"signature"]];
|
|
|
+ putObjRequest.filename = uploadFileName;
|
|
|
+ putObjRequest.data = imageData;
|
|
|
+ [putObjRequest setCompleteRequest];
|
|
|
+
|
|
|
+ KS3PutObjectResponse *response = [[KS3Client initialize] putObject:putObjRequest];
|
|
|
+ if (response.httpStatusCode == 200) {
|
|
|
+ if (response.error == nil) {
|
|
|
+ if (self.successCallback) {
|
|
|
+ NSMutableArray *fileUrlArray = [NSMutableArray array];
|
|
|
+ [fileUrlArray addObject:[NSString stringWithFormat:@"https://%@/%@",BUCKET_DOMAIN,uploadFileName]];
|
|
|
+ self.successCallback(fileUrlArray);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (self.faliureCallback) {
|
|
|
+ self.faliureCallback(response.error, @"上传文件失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ NSLog(@"Set object acl error: %@", response.error.description);
|
|
|
+ if (self.faliureCallback) {
|
|
|
+ self.faliureCallback(response.error, @"上传文件失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (self.faliureCallback) {
|
|
|
+ self.faliureCallback(nil, MESSAGEKEY);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } faliure:^(NSError * _Nonnull error) {
|
|
|
+ if (self.faliureCallback) {
|
|
|
+ self.faliureCallback(error, @"获取文件签名失败");
|
|
|
+ }
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)mutilUploadImage:(NSMutableArray *)fileDataArray fileName:(NSString *)fileName successCallback:(KSUploadSuccess)success faliure:(KSUploadFailer)faliure {
|
|
|
+ if (success) {
|
|
|
+ self.successCallback = success;
|
|
|
+ }
|
|
|
+ if (faliure) {
|
|
|
+ self.faliureCallback = faliure;
|
|
|
+ }
|
|
|
+ NSMutableArray *fileNameArray = [NSMutableArray array];
|
|
|
+ for (NSInteger i = 0 ; i < fileDataArray.count; i++) {
|
|
|
+ NSData *fileData = fileDataArray[i];
|
|
|
+ NSString *uploadFileName = [NSString stringWithFormat:@"%@%zd%@%@",[NSDate getCurrentTimestamp], i,fileName,[UIImage typeForImageData:fileData]];
|
|
|
+ [fileNameArray addObject:uploadFileName];
|
|
|
+ }
|
|
|
+
|
|
|
+ __block NSMutableArray *sessions = [NSMutableArray array];
|
|
|
+ __block NSMutableArray *responses = [NSMutableArray array];
|
|
|
+ __block NSMutableArray *failResponse = [NSMutableArray array];
|
|
|
+
|
|
|
+ dispatch_group_t uploadGroup = dispatch_group_create();
|
|
|
+ NSInteger count = fileDataArray.count;
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+
|
|
|
+ dispatch_group_enter(uploadGroup);
|
|
|
+ NSString *uploadFileName = fileNameArray[i];
|
|
|
+ NSString *keyName = fileNameArray[i];
|
|
|
+ NSData *fileData = fileDataArray[i];
|
|
|
+ [KSNetworkingManager getUploadSignRequest:KS_POST fileName:uploadFileName keyName:keyName success:^(NSDictionary * _Nonnull dic) {
|
|
|
+ if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
|
|
|
+ KS3AccessControlList *acl = [[KS3AccessControlList alloc] init];
|
|
|
+ [acl setContronAccess:KingSoftYun_Permission_Public_Read];
|
|
|
+
|
|
|
+ KS3PutObjectRequest *putObjRequest = [[KS3PutObjectRequest alloc] initWithName:@"" withAcl:acl grantAcl:nil];
|
|
|
+ if (putObjRequest) {
|
|
|
+ [sessions addObject:putObjRequest];
|
|
|
+ }
|
|
|
+ // token
|
|
|
+ [putObjRequest setStrKS3Token:[dic stringValueForKey:@"signature"]];
|
|
|
+ putObjRequest.filename = uploadFileName;
|
|
|
+ putObjRequest.data = fileData;
|
|
|
+ [putObjRequest setCompleteRequest];
|
|
|
+ KS3PutObjectResponse *response = [[KS3Client initialize] putObject:putObjRequest];
|
|
|
+ if (response.httpStatusCode == 200) {
|
|
|
+ if (response.error == nil) {
|
|
|
+ [responses addObject:[NSString stringWithFormat:@"https://%@/%@",BUCKET_DOMAIN,uploadFileName]];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ NSString *desc = [NSString stringWithFormat:@"第%d次上传文件失败",i];
|
|
|
+ [failResponse addObject:desc];
|
|
|
+ }
|
|
|
+ [sessions removeObject:putObjRequest];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ NSLog(@"Set object acl error: %@", response.error.description);
|
|
|
+ NSString *desc = [NSString stringWithFormat:@"第%d次上传文件失败",i];
|
|
|
+ [failResponse addObject:desc];
|
|
|
+ [sessions removeObject:putObjRequest];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ [failResponse addObject:MESSAGEKEY];
|
|
|
+ }
|
|
|
+ dispatch_group_leave(uploadGroup);
|
|
|
+ } faliure:^(NSError * _Nonnull error) {
|
|
|
+ NSError *Error = [NSError errorWithDomain:@"getUploadSign" code:-999 userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:@"第%d次获取签名失败",i]}];
|
|
|
+ [failResponse addObject:Error];
|
|
|
+ dispatch_group_leave(uploadGroup);
|
|
|
+ }];
|
|
|
+ }
|
|
|
+
|
|
|
+ dispatch_group_notify(uploadGroup, dispatch_get_main_queue(), ^{
|
|
|
+ if (responses.count > 0) {
|
|
|
+ if (self.successCallback) {
|
|
|
+ self.successCallback([responses copy]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (failResponse.count > 0) {
|
|
|
+ if (self.faliureCallback) {
|
|
|
+ self.faliureCallback(nil,@"上传文件失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)videoUpload:(NSData *)fileData fileName:(NSString *)fileName fileSuffix:(NSString *)fileSuffix progress:(KSUploadProgress)uploadProgress successCallback:(KSUploadSuccess)success faliure:(KSUploadFailer)faliure {
|
|
|
+ if (success) {
|
|
|
+ self.successCallback = success;
|
|
|
+ }
|
|
|
+ if (faliure) {
|
|
|
+ self.faliureCallback = faliure;
|
|
|
+ }
|
|
|
+ if (uploadProgress) {
|
|
|
+ self.uploadProgress = uploadProgress;
|
|
|
+ }
|
|
|
+ NSString *uploadFileName = [NSString stringWithFormat:@"%@%@%@",[NSDate getCurrentTimestamp], fileName,fileSuffix];
|
|
|
+ NSString *keyValue = uploadFileName;
|
|
|
+ self.videoLinkUrl = [NSString stringWithFormat:@"https://%@/%@",BUCKET_DOMAIN,uploadFileName];
|
|
|
+ [KSNetworkingManager getUploadSignRequest:KS_POST fileName:uploadFileName keyName:keyValue success:^(NSDictionary * _Nonnull dic) {
|
|
|
+ if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
|
|
|
+ KS3AccessControlList *acl = [[KS3AccessControlList alloc] init];
|
|
|
+ [acl setContronAccess:KingSoftYun_Permission_Public_Read];
|
|
|
+
|
|
|
+ KS3PutObjectRequest *putObjRequest = [[KS3PutObjectRequest alloc] initWithName:@"" withAcl:acl grantAcl:nil];
|
|
|
+ // token
|
|
|
+ [putObjRequest setStrKS3Token:[dic stringValueForKey:@"signature"]];
|
|
|
+ putObjRequest.filename = uploadFileName;
|
|
|
+ putObjRequest.data = fileData;
|
|
|
+ _fileSize = putObjRequest.data.length;
|
|
|
+ putObjRequest.delegate = self;
|
|
|
+ [putObjRequest setCompleteRequest];
|
|
|
+ self.videoLinkUrl = [NSString stringWithFormat:@"https://%@/%@",BUCKET_DOMAIN,uploadFileName];
|
|
|
+ KS3PutObjectResponse *response = [[KS3Client initialize] putObject:putObjRequest];
|
|
|
+ if (putObjRequest.delegate == nil) {
|
|
|
+ NSLog(@"%@",[[NSString alloc] initWithData:response.body encoding:NSUTF8StringEncoding]);
|
|
|
+ if (response.httpStatusCode == 200) {
|
|
|
+ NSLog(@"Put object success");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ NSLog(@"Put object failed");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (self.faliureCallback) {
|
|
|
+ self.faliureCallback(nil, MESSAGEKEY);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } faliure:^(NSError * _Nonnull error) {
|
|
|
+ if (self.faliureCallback) {
|
|
|
+ self.faliureCallback(error, @"获取文件签名失败");
|
|
|
+ }
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)uploadFile:(NSData *)fileData fileName:(NSString *)fileName fileSuffix:(NSString *)fileSuffix successCallback:(KSUploadSuccess)success faliure:(KSUploadFailer)faliure {
|
|
|
+ if (success) {
|
|
|
+ self.successCallback = success;
|
|
|
+ }
|
|
|
+ if (faliure) {
|
|
|
+ self.faliureCallback = faliure;
|
|
|
+ }
|
|
|
+ NSString *uploadFileName = [NSString stringWithFormat:@"%@%@%@",[NSDate getCurrentTimestamp], fileName,fileSuffix];
|
|
|
+ NSString *keyValue = uploadFileName;
|
|
|
+ [KSNetworkingManager getUploadSignRequest:KS_POST fileName:uploadFileName keyName:keyValue success:^(NSDictionary * _Nonnull dic) {
|
|
|
+ if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
|
|
|
+ KS3AccessControlList *acl = [[KS3AccessControlList alloc] init];
|
|
|
+ [acl setContronAccess:KingSoftYun_Permission_Public_Read];
|
|
|
+
|
|
|
+ KS3PutObjectRequest *putObjRequest = [[KS3PutObjectRequest alloc] initWithName:@"" withAcl:acl grantAcl:nil];
|
|
|
+ // token
|
|
|
+ [putObjRequest setStrKS3Token:[dic stringValueForKey:@"signature"]];
|
|
|
+ putObjRequest.filename = uploadFileName;
|
|
|
+ putObjRequest.data = fileData;
|
|
|
+ [putObjRequest setCompleteRequest];
|
|
|
+
|
|
|
+ KS3PutObjectResponse *response = [[KS3Client initialize] putObject:putObjRequest];
|
|
|
+ if (response.httpStatusCode == 200) {
|
|
|
+ if (response.error == nil) {
|
|
|
+ if (self.successCallback) {
|
|
|
+ NSMutableArray *fileUrlArray = [NSMutableArray array];
|
|
|
+ [fileUrlArray addObject:[NSString stringWithFormat:@"https://%@/%@",BUCKET_DOMAIN,uploadFileName]];
|
|
|
+ self.successCallback(fileUrlArray);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (self.faliureCallback) {
|
|
|
+ self.faliureCallback(response.error, @"上传文件失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ NSLog(@"Set object acl error: %@", response.error.description);
|
|
|
+ if (self.faliureCallback) {
|
|
|
+ self.faliureCallback(response.error, @"上传文件失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (self.faliureCallback) {
|
|
|
+ self.faliureCallback(nil, MESSAGEKEY);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } faliure:^(NSError * _Nonnull error) {
|
|
|
+ if (self.faliureCallback) {
|
|
|
+ self.faliureCallback(error, @"获取文件签名失败");
|
|
|
+ }
|
|
|
+ }];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)request:(KS3ServiceRequest *)request didSendData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten totalBytesExpectedToWrite:(long long)totalBytesExpectedToWrite {
|
|
|
+ if ([request isKindOfClass:[KS3PutObjectRequest class]]) {
|
|
|
+
|
|
|
+ long long alreadyTotalWriten = totalBytesWritten;
|
|
|
+ double progress = alreadyTotalWriten * 1.0 / _fileSize;
|
|
|
+ NSLog(@"upload progress: %f", progress);
|
|
|
+ if (self.uploadProgress) {
|
|
|
+ self.uploadProgress(bytesWritten, _fileSize);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+- (void)request:(KS3Request *)request didReceiveResponse:(NSURLResponse *)response {
|
|
|
+ NSInteger statusCode = ((NSHTTPURLResponse*) response).statusCode;
|
|
|
+ if ( (statusCode>= 200 && statusCode <300) || statusCode == 304) {
|
|
|
+ NSLog(@"Put object success");
|
|
|
+ NSMutableArray *fileUrlArray = [NSMutableArray array];
|
|
|
+ [fileUrlArray addObject:self.videoLinkUrl];
|
|
|
+ self.successCallback(fileUrlArray);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ NSLog(@"Put object failed");
|
|
|
+ if (self.faliureCallback) {
|
|
|
+ self.faliureCallback(nil, @"上传文件失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (void)request:(KS3ServiceRequest *)request didFailWithError:(NSError *)error {
|
|
|
+ if (self.faliureCallback) {
|
|
|
+ self.faliureCallback(nil, @"上传文件失败");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+@end
|