Files
featherVoice/QXLive/Tools/OSS/QXOSSManager.m
2025-08-08 10:49:36 +08:00

285 lines
11 KiB
Objective-C
Executable File

////
// SocketManager.m
// SoundRiver
//
//
#import "QXOSSManager.h"
#import "OSSProgressModel.h"
#import "OSSThreadSafeMutableDictionary.h"
#import "QXApi.h"
static QXOSSManager *sharedManager = nil;
@interface QXOSSManager()
@property (nonatomic, strong) NSOperationQueue *activityUploadQueue;
@end
@implementation QXOSSManager
+(QXOSSManager *)sharedInstance {
@synchronized(self) {
if(!sharedManager) {
sharedManager = [[self alloc] init];
}
}
return sharedManager;
}
+(id)allocWithZone:(NSZone *)zone{
@synchronized(self){
if(!sharedManager){
//确保使用同一块内存地址
sharedManager = [super allocWithZone:zone];
return sharedManager;
}
}
return nil;
}
- (id)copyWithZone:(NSZone*)zone{
return self;
}
- (instancetype)init {
self = [super init];
if (self) {
id<OSSCredentialProvider> credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:OSSAccessKeyId secretKey:OSSAccessKeySecret];
_client = [[OSSClient alloc] initWithEndpoint:[NSString stringWithFormat:@"https://%@", OSSEndPoint] credentialProvider:credential];
_formatter = [[NSDateFormatter alloc] init];
[_formatter setDateFormat:@"yyyy-MM-dd"];
}
return self;
}
- (void)uploadFile:(NSArray *)files withObjectKey:(NSArray *)objectKeys isAsync:(BOOL)isAsync complete:(void (^)(NSArray<NSString *> *names, UploadImageState state))complete {
if (files.count != objectKeys.count) {
NSLog(@"文件数量和名称数量不一致");
return;
}
OSSThreadSafeMutableDictionary *progressModelDic = [[OSSThreadSafeMutableDictionary alloc] init];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = files.count;
NSMutableArray *callBackNames = [NSMutableArray array];
int i = 0;
for (id file in files) {
if (file) {
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
//任务执行
NSString *keyString = objectKeys[i];
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
put.bucketName = OSS_BUCKET_NAME;
put.objectKey = objectKeys[i];
[callBackNames addObject:objectKeys[i]];
if ([file isKindOfClass:[UIImage class]]) {
NSData *data = UIImageJPEGRepresentation(file, 0.3);
put.uploadingData = data;
}else if ([file isKindOfClass:[NSData class]]){
put.uploadingData = (NSData *)file;
}else if ([file isKindOfClass:[NSURL class]]){
put.uploadingFileURL = (NSURL *)file;
}
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
// 当前上传段长度、当前已经上传总长度、一共需要上传的总长度
[self.delegate OSSManagerUploadProgressWithTotalSent:totalByteSent andTotalExpectedToSend:totalBytesExpectedToSend];
if ([self.delegate respondsToSelector:@selector(OSSManagerUploadTotalProgress:)]) {
OSSProgressModel *pm = [OSSProgressModel new];
pm.currentBytes = totalByteSent;
pm.totalBytes = totalBytesExpectedToSend;
[progressModelDic setObject:pm forKey:keyString];
CGFloat totalProgress = 0;
for (OSSProgressModel *pm in progressModelDic.allValues.reverseObjectEnumerator) {
CGFloat singleProgress = 0;
if (pm.currentBytes == pm.totalBytes) {
singleProgress = 1.0 / files.count;
}else {
singleProgress = pm.currentBytes / (float)pm.totalBytes * (1.0 / files.count);
}
totalProgress = totalProgress + singleProgress;
}
[self.delegate OSSManagerUploadTotalProgress:totalProgress];
}
};
OSSTask * putTask = [self->_client putObject:put];
[putTask continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
if (!putTask.error) {
NSLog(@"upload object success!");
} else {
NSLog(@"upload object failed, error: %@" , putTask.error);
}
if (isAsync) {
if (file == files.lastObject) {
NSLog(@"upload object finished!");
if (complete) {
complete([NSArray arrayWithArray:callBackNames] ,UploadImageSuccess);
}
}
}
return nil;
}];
// [putTask waitUntilFinished]; // 阻塞直到上传完成
}];
if (queue.operations.count != 0) {
[operation addDependency:queue.operations.lastObject];
}
[queue addOperation:operation];
}
i++;
}
if (!isAsync) {
[queue waitUntilAllOperationsAreFinished];
NSLog(@"haha");
if (complete) {
if (complete) {
complete([NSArray arrayWithArray:callBackNames], UploadImageSuccess);
}
}
}
}
- (NSString *)currentDate {
return [_formatter stringFromDate:[NSDate date]];
}
/**
动态资源上传
*/
- (void)activityUploadFile:(NSArray *)files withObjectKey:(NSArray *)objectKeys isAsync:(BOOL)isAsync complete:(void(^)(NSArray<NSString *> *names, UploadImageState state))complete
{
if (files.count != objectKeys.count) {
NSLog(@"文件数量和名称数量不一致");
return;
}
OSSThreadSafeMutableDictionary *progressModelDic = [[OSSThreadSafeMutableDictionary alloc] init];
if (self.activityUploadQueue.operations.count) {
[self.activityUploadQueue cancelAllOperations];
}
self.activityUploadQueue.maxConcurrentOperationCount = files.count > 9 ? 9 : files.count;
NSMutableArray *callBackNames = [NSMutableArray array];
int i = 0;
for (id file in files) {
if (file) {
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
//任务执行
NSString *keyString = objectKeys[i];
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
put.bucketName = OSS_BUCKET_NAME;
put.objectKey = objectKeys[i];
[callBackNames addObject:objectKeys[i]];
if ([file isKindOfClass:[UIImage class]]) {
NSData *data = UIImageJPEGRepresentation(file, 0.3);
put.uploadingData = data;
}else if ([file isKindOfClass:[NSData class]]){
put.uploadingData = (NSData *)file;
}else if ([file isKindOfClass:[NSURL class]]){
put.uploadingFileURL = (NSURL *)file;
}
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
// 当前上传段长度、当前已经上传总长度、一共需要上传的总长度
[self.delegate OSSManagerUploadProgressWithTotalSent:totalByteSent andTotalExpectedToSend:totalBytesExpectedToSend];
if ([self.delegate respondsToSelector:@selector(OSSManagerUploadTotalProgress:)]) {
OSSProgressModel *pm = [OSSProgressModel new];
pm.currentBytes = totalByteSent;
pm.totalBytes = totalBytesExpectedToSend;
[progressModelDic setObject:pm forKey:keyString];
CGFloat totalProgress = 0;
for (OSSProgressModel *pm in progressModelDic.allValues.reverseObjectEnumerator) {
CGFloat singleProgress = 0;
if (pm.currentBytes == pm.totalBytes) {
singleProgress = 1.0 / files.count;
}else {
singleProgress = pm.currentBytes / (float)pm.totalBytes * (1.0 / files.count);
}
totalProgress = totalProgress + singleProgress;
}
[self.delegate OSSManagerUploadTotalProgress:totalProgress];
}
};
OSSTask * putTask = [self->_client putObject:put];
[putTask continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {
if (!putTask.error) {
NSLog(@"upload object success!");
} else {
NSLog(@"upload object failed, error: %@" , putTask.error);
}
if (isAsync) {
if (file == files.lastObject) {
NSLog(@"upload object finished!");
if (complete) {
complete([NSArray arrayWithArray:callBackNames] ,UploadImageSuccess);
}
}
}
return nil;
}];
[putTask waitUntilFinished]; // 阻塞直到上传完成
}];
if (self.activityUploadQueue.operations.count != 0) {
[operation addDependency:self.activityUploadQueue.operations.lastObject];
}
[self.activityUploadQueue addOperation:operation];
}
i++;
}
if (!isAsync) {
[self.activityUploadQueue waitUntilAllOperationsAreFinished];
NSLog(@"haha");
if (complete) {
if (complete) {
complete([NSArray arrayWithArray:callBackNames], UploadImageSuccess);
}
}
}
}
- (void)cancelUpload {
[self.activityUploadQueue cancelAllOperations];
}
//+(NSString *)getNowTimeTimestamp3{
//
// NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
//
// [formatter setDateStyle:NSDateFormatterMediumStyle];
//
// [formatter setTimeStyle:NSDateFormatterShortStyle];
//
// [formatter setDateFormat:@"YYYY-MM-dd"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
//
//
// NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
//
// NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
//
// NSLog(@"%@",timeSp);
//
//
//
//
//
// return timeSp;
//
//}
- (NSOperationQueue *)activityUploadQueue {
if (!_activityUploadQueue) {
_activityUploadQueue = [[NSOperationQueue alloc] init];
}
return _activityUploadQueue;
}
@end