CNLiveCommonQiniuTool.swift
6.94 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// CNLiveCommonQiniuTool.swift
// metaCode
//
// Created by open on 2023/6/15.
//
import Foundation
import Qiniu
import CNLiveRequestBastKit
typealias ProgressBlcok = (_ key: String,_ percent: CGFloat) -> Void
typealias SuccessBlock = (_ key: String,_ url: String) -> Void
class UploadManager: NSObject {
var uploadModels = NSMutableArray()
var qnUploadManager = QNUploadManager()
static let shared = UploadManager()
private override init() {
super.init()
let config = QNConfiguration.build { builder in
builder?.useHttps = true;
do {
builder?.recorder = try QNFileRecorder(folder: "元码")
} catch {}
}
qnUploadManager = QNUploadManager(configuration: config)
}
/// 上传图片
/// - Parameters:
/// - imageData: 图片数据
/// - progressBlock: 进度回调
/// - successBlock: 成功回调
static func uploadImage(imageData: NSData, progressBlock:@escaping ProgressBlcok, successBlock: @escaping SuccessBlock) -> String {
let key = UploadManager.createImageFileName()
UploadManager.uploadData(madiaData: imageData, key: key, progress: progressBlock, success: successBlock)
return key
}
/// 上传视频
/// - Parameters:
/// - imageData: 视频数据
/// - progressBlock: 进度回调
/// - successBlock: 成功回调
static func uploadVideo(videoData: NSData, progressBlock:@escaping ProgressBlcok, successBlock: @escaping SuccessBlock) -> String {
let key = UploadManager.createVideoFileName()
UploadManager.uploadData(madiaData: videoData, key: key, progress: progressBlock, success: successBlock)
return key
}
// MARK: - 取消上传
func cancelUploadKey(key:String){
for (_,obj) in self.uploadModels.enumerated() {
let model = obj as! QiNiuUploadModel
if model.key == key{
model.isCancel = true
break
}
}
}
// MARK: - 删除云中图片
func requestQiNiuDeleteImageUrl(imageUrl:String,resultBlock:@escaping downSlideResultBlock){
let param = NSMutableDictionary()
param.setValue(imageUrl, forKey: "img")
showLoading(message: "处理中...")
CNLiveNetworking.setAllowRequestDefaultArgument(true)
CNLiveNetworking.setupShowDataResult(false)
CNLiveNetworking.setupDSRequest(true)
CNLiveNetworking.requestNetwork(with: .POST, urlString: APPBaseWjjBUrl+"/Api/v1/delQiniuImg", param: param as! [String : String], cacheType: .networkOnly) { task, result, error in
hiddenLoading()
if (error == nil) {
resultBlock(result as Any,.success)
}else{
resultBlock(NSNull(),.failed)
}
}
}
/// 上传到七牛
/// - Parameters:
/// - data: 上传数据
/// - key: key
/// - uploadProgress: 上传进度
/// - resultBlock: 结果
static private func uploadData(madiaData: NSData, key: String, progress: ProgressBlcok?, success: SuccessBlock?) {
let param = NSMutableDictionary()
param.setValue(APPId, forKey: "appId")
param.setValue("POST", forKey: "verb")
param.setValue("2", forKey: "storageType")
param.setValue(kBucketName_QN, forKey: "bucket")
CNLiveNetworking.setupShowResult(true)
CNLiveNetworking.setAllowRequestDefaultArgument(true)
CNLiveNetworking.setupShowDataResult(false)
CNLiveNetworking.setupSignRequestKey(APPKey)//APPKey_wjj APPKey
CNLiveNetworking.requestNetwork(with: .POST, urlString: "\(CNLiveEnvironmentManager.shared.img_token_url)/vod_epg/getUploadAuthForApp", param: param as! [String : String], cacheType: .networkOnly) { task, result, error in
hiddenLoading()
if (error == nil) {
let resp = result as! NSDictionary
let errorCode = "\(resp["errorCode"] ?? 0)"
if (errorCode == "0") {
let data = resp["data"] as! Dictionary<String, Any>
let token = data["token"] as! String
if token.isNotBlank() {
let uploadModel = QiNiuUploadModel.init()
uploadModel.isCancel = false
uploadModel.key = key
let uploadOption = QNUploadOption.init(mime: nil, progressHandler: { key, percent in
DispatchQueue.main.async {
progress!(key!, CGFloat(percent))
}
}, params: nil, checkCrc: false) {
return uploadModel.isCancel
}
uploadModel.uploadOption = uploadOption!
UploadManager.shared.uploadModels.add(uploadModel)
showLoading(message: "上传中...")
let url_key = "http://wjjtest.ys2.cnliveimg.com/"+key
UploadManager.shared.qnUploadManager?.put(madiaData as Data, key: key, token: token, complete: { info, key, resp in
hiddenLoading()
//删除上传选项
UploadManager.shared.uploadModels.remove(uploadModel)
if info?.statusCode == 200 {
if success != nil {
success!(key!, url_key)
}
} else {
if info?.statusCode == -999 {
showMessage(message: "上传取消")
} else {
showLoading(message: "上传失败")
}
}
}, option: uploadOption)
}
} else {
showMessage(message: resp["errorMessage"] as! String)
}
} else {
showMessage(message: error?.localizedDescription ?? "上传失败")
}
}
}
/// 设置图片名称
/// - Returns: 图片名
static private func createImageFileName() -> String {
let imageName = "802/mateCode/img/\(NSDate().string(withFormat: "yyyy/MM/dd") )/\(arc4random()).jpg"
return imageName
}
/// 设置视频名称
/// - Returns: 图片名
static private func createVideoFileName() -> String {
let imageName = "802/mateCode/video/\(NSDate().string(withFormat: "yyyy/MM/dd") )/\(arc4random()).mp4"
return imageName
}
}
class QiNiuUploadModel: BaseModel {
required init() {
self.uploadOption = QNUploadOption.defaultOptions()
}
///上传文件的key
var key:String = ""
var uploadOption:QNUploadOption
var isCancel:Bool = false
}