AliOSSUploadMangae.m 4.47 KB
//
//  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