Files
yinmeng-ios/xplan-ios/Base/Tool/File/UploadFile.m
2023-12-26 14:29:53 +08:00

53 lines
2.2 KiB
Objective-C

//
// UploadFile.m
// xplan-ios
//
// Created by GreenLand on 2022/2/23.
//
#import "UploadFile.h"
#import <Qiniu/QiniuSDK.h>
#import <AFNetworking.h>
@implementation UploadFile
/// 上传一个文件
/// @param filePath 文件地址
/// @param fileName 文件的名字
/// @param token token
/// @param success 成功
/// @param failure 失败
+ (void)uploadFile:(NSString *)filePath
named:(NSString *)fileName
token:(NSString *)token
success:(void (^)(NSString *key, NSDictionary *resp))success
failure:(void (^)(NSNumber *resCode, NSString *message))failure {
QNConfiguration *config = [QNConfiguration build:^(QNConfigurationBuilder *builder) {
builder.zone = [QNFixedZone zone2];
}];
QNUploadManager *upManager = [[QNUploadManager alloc] initWithConfiguration:config];
[upManager putFile:filePath key:fileName token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
if (resp) {
success(key,resp);
}else{
failure(@(info.statusCode),info.error.localizedDescription);
}
} option:nil];
}
+ (void)mew_downloadAudioWithFileName:(NSString *)fileName musicUrl:(NSString *)musicUrl mainFileName:(NSString *)mainFileName completion:(void (^) (BOOL isSuccess, NSString *editAudioPath))completion{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:musicUrl]];
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSString *filePath = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) safeObjectAtIndex1:0] stringByAppendingPathComponent:mainFileName] stringByAppendingPathComponent:fileName];
return [NSURL fileURLWithPath:filePath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if (!error) {
completion(YES, filePath.path);
} else {
completion(NO, nil);
}
}];
[download resume];
}
@end