PQAliOssUtil.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. //
  2. // PQAliOssUtil.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2020/12/9.
  6. // Copyright © 2020 BytesFlow. All rights reserved.
  7. //
  8. import UIKit
  9. import BFCommonKit
  10. import BFNetRequestKit
  11. // MARK: - 阿里OSS工具类
  12. /// 阿里OSS工具类
  13. public class PQAliOssUtil: NSObject {
  14. static public let shared = PQAliOssUtil()
  15. public var client: OSSClient?
  16. // 文件类型:materialType (1:PICTURE, 2:VIDEO, 3:VOICE, 4:FILE, 5:GIF)
  17. public var aliOssHander: ((_ isMatarialUpload: Bool, _ materialType: StickerType, _ fileExtensions: String, _ code: Int, _ objectKey: String?, _ contentMD5: String, _ width: CGFloat, _ height: CGFloat, _ duration: CGFloat, _ frameNumber: Int, _ netResourceUrl: String?, _ fileURL: URL?, _ data: Data?, _ msg: String?) -> Void)?
  18. public var aliOssProgressHander: ((_ bytesSent: Int64, _ totalBytesSent: Int64, _ totalBytesExpectedToSend: Int64, _ resttime: Int64, _ uploadSpeed: String?) -> Void)?
  19. public var lastInterfaceBytes: Int64 = 0 // 上次网速
  20. public var oldTime: Int = 0 // 上次进度时间S
  21. public var oloaded: Int64 = 0
  22. public var dics: [String: Any] = [:]
  23. public var allTasks: [String: OSSMultipartUploadRequest] = [:] // add by ak 保存当前所有任务 用于取消某个任务使用
  24. public func startClient(accessKeyId: String, secretKeyId: String, securityToken: String, endpoint: String) -> PQAliOssUtil {
  25. if endpoint.count == 0 {
  26. debugPrint("endpoint is nil xxxxxx \(endpoint)")
  27. }
  28. let credential = OSSStsTokenCredentialProvider(accessKeyId: accessKeyId, secretKeyId: secretKeyId, securityToken: securityToken)
  29. let conf = OSSClientConfiguration()
  30. conf.timeoutIntervalForRequest = 60 // 连接超时,默认15秒
  31. conf.maxRetryCount = 3
  32. conf.maxConcurrentRequestCount = 5 // 最大并发请求书,默认5个
  33. client = OSSClient(endpoint: endpoint, credentialProvider: credential, clientConfiguration: conf)
  34. return .shared
  35. }
  36. /// 图片上传
  37. /// - Parameters:
  38. /// - bucketName: <#bucketName description#>
  39. /// - objectKey: <#objectKey description#>
  40. /// - data: <#data description#>
  41. /// - Returns: <#description#>
  42. public func uploadObjectAsync(bucketName: String, objectKey: String, data: Data, materialType: StickerType = .IMAGE, fileExtensions: String, isMatarialUpload: Bool = false, contentMD5: String = "", width: CGFloat = 0, height: CGFloat = 0, imageUploadBlock: @escaping (_ osstask: OSSTask<AnyObject>?, _ code: Int, _ objectKey: String, _ fileExtensions: String) -> Void) -> PQAliOssUtil {
  43. let putRequest: OSSPutObjectRequest = OSSPutObjectRequest()
  44. putRequest.bucketName = bucketName
  45. putRequest.objectKey = objectKey
  46. putRequest.uploadingData = data
  47. putRequest.uploadProgress = { [weak self] _, totalByteSent, totalBytesExpectedToSend in
  48. debugPrint("文件上传进度:totalByteSent = \(totalByteSent),totalBytesExpectedToSend = \(totalBytesExpectedToSend)")
  49. if totalBytesExpectedToSend > 0, totalByteSent == totalBytesExpectedToSend {
  50. if isMatarialUpload, self?.aliOssHander != nil {
  51. self?.aliOssHander!(isMatarialUpload, materialType, fileExtensions, 1, objectKey, contentMD5, width, height, 0, 0, nil, nil, data, "上传成功")
  52. }
  53. }
  54. }
  55. putRequest.contentType = "application/octet-stream"
  56. // putRequest.contentMd5 = contentMD5
  57. putRequest.contentEncoding = ""
  58. putRequest.contentDisposition = ""
  59. let putTask: OSSTask = (client?.putObject(putRequest))!
  60. putTask.continue(successBlock: { [weak self] (osstask) -> Any? in
  61. if osstask.error == nil {
  62. let task = self?.client?.presignPublicURL(withBucketName: putRequest.bucketName, withObjectKey: putRequest.objectKey)
  63. debugPrint("图片原方法上传完成=\(objectKey)")
  64. debugPrint("url == \(task?.result ?? "" as AnyObject)")
  65. imageUploadBlock(osstask, 1, objectKey, fileExtensions)
  66. } else {
  67. if self?.aliOssHander != nil {
  68. self?.aliOssHander!(isMatarialUpload, materialType, fileExtensions, 0, objectKey, contentMD5, width, height, 0, 0, nil, nil, data, "上传失败")
  69. }
  70. debugPrint("图片原方法上传失败=\(objectKey),osstask.error = \(osstask.error ?? PQError(msg: "失败"))")
  71. imageUploadBlock(osstask, 0, objectKey, fileExtensions)
  72. }
  73. return nil
  74. }).waitUntilFinished()
  75. return .shared
  76. }
  77. /// 快速上传视频
  78. /// - Parameters:
  79. /// - localPath: <#localPath description#>
  80. /// - response: <#response description#>
  81. /// - Returns: <#description#>
  82. class public func multipartUpload(localPath: String, response: [String: Any]?,videoSource:String? = nil) {
  83. let accessKeyId: String = "\(response?["AccessKeyId"] ?? "")"
  84. let secretKeyId: String = "\(response?["AccessKeySecret"] ?? "")"
  85. let securityToken: String = "\(response?["SecurityToken"] ?? "")"
  86. let endpoint: String = "\(response?["Host"] ?? "")"
  87. let bucketName: String = "\(response?["Bucket"] ?? "")"
  88. let FileName: String = "\(response?["FileName"] ?? "")"
  89. let uploadID: String = "\(response?["Upload"] ?? "")"
  90. var endpoints: [String] = Array<String>.init()
  91. if response?.keys.contains("Hosts") ?? false {
  92. endpoints = response?["Hosts"] as! [String]
  93. endpoints.append(endpoint)
  94. }
  95. PQAliOssUtil.shared.PQOSSMultipartUpload(accessKeyId: accessKeyId, secretKeyId: secretKeyId, securityToken: securityToken, bucketName: bucketName, endpoints: endpoints, FileName: FileName, fileURL: URL(fileURLWithPath: localPath.replacingOccurrences(of: "file:///", with: "")), ossUploadID: uploadID, videoSource: videoSource)
  96. }
  97. /// 重新上传
  98. /// - Parameters:
  99. /// - response: <#response description#>
  100. /// - localPath: <#localPath description#>
  101. /// - Returns: <#description#>
  102. public func reloadTask(response: [String: Any]?, localPath: String?,videoSource:String? = nil) {
  103. if response == nil {
  104. return
  105. }
  106. let accessKeyId: String = "\(response?["AccessKeyId"] ?? "")"
  107. let secretKeyId: String = "\(response?["AccessKeySecret"] ?? "")"
  108. let securityToken: String = "\(response?["SecurityToken"] ?? "")"
  109. let endpoint: String = "\(response?["Host"] ?? "")"
  110. let bucketName: String = "\(response?["Bucket"] ?? "")"
  111. let FileName: String = "\(response?["FileName"] ?? "")"
  112. let uploadID: String = "\(response?["Upload"] ?? "")"
  113. var endpoints: [String] = Array<String>.init()
  114. if response?.keys.contains("Hosts") ?? false {
  115. endpoints = response?["Hosts"] as! [String]
  116. endpoints.append(endpoint)
  117. }
  118. debugPrint("取我方服务器STS 返回数据 \(String(describing: response))")
  119. PQAliOssUtil.shared.PQOSSMultipartUpload(accessKeyId: accessKeyId, secretKeyId: secretKeyId, securityToken: securityToken, bucketName: bucketName, endpoints: endpoints, FileName: FileName, fileURL: URL(fileURLWithPath: (localPath ?? "").replacingOccurrences(of: "file:///", with: "")), ossUploadID: uploadID,videoSource: videoSource)
  120. }
  121. /// 分片上传
  122. /// - Parameters:
  123. /// - accessKeyId: <#accessKeyId description#>
  124. /// - secretKeyId: <#secretKeyId description#>
  125. /// - securityToken: <#securityToken description#>
  126. /// - bucketName: <#bucketName description#>
  127. /// - endpoints: oss host域名
  128. /// - FileName: oss 资源地址
  129. /// - fileURL: 文件本地地址
  130. /// - ossUploadID: oss 上传ID
  131. /// - contentType: 文件类型
  132. /// - materialType: 素材类型
  133. /// - isMatarialUpload: 是否是素材上传
  134. /// - contentMD5: 素材内容MD5值 isMatarialUpload = true时传
  135. /// - width: 视频宽 isMatarialUpload = true时传
  136. /// - height: 视频高 isMatarialUpload = true时传
  137. /// - Returns: <#description#>
  138. public func PQOSSMultipartUpload(accessKeyId: String, secretKeyId: String, securityToken: String, bucketName: String, endpoints: [String], FileName: String, fileURL: URL, ossUploadID: String, materialType: StickerType = .VIDEO, isMatarialUpload: Bool = false, contentMD5: String = "", width: CGFloat = 0, height: CGFloat = 0,videoSource:String? = nil) -> PQAliOssUtil {
  139. debugPrint("上传数据 参数\n accessKeyId:\(accessKeyId)\n secretKeyId:\(secretKeyId)\n securityToken:\(securityToken) \n bucketName:\(bucketName)\n endpoint:\(endpoints)\n FileName:\(FileName)")
  140. if endpoints.count <= 0 || (endpoints.first?.count ?? 0) <= 0 {
  141. debugPrint("endpoints 为空")
  142. return .shared
  143. }
  144. #if DEBUG
  145. // 打开 oss 日志
  146. OSSLog.enable()
  147. #endif
  148. // 1,设置鉴权
  149. let url = PQENVUtil.shared.longvideoapi + (isMatarialUpload ? materialUploadStsTokenUrl : getStsTokenUrl)
  150. let authServerUrl = url + "?appType=\(commonParams()["appType"] as? String ?? "")" + "&machineCode=" + getMachineCode()
  151. + "&token=" + (BFConfig.shared.token ?? "") + "&loginUid" + (BFConfig.shared.uid ?? "") + "&fileType=2" + "&uploadId=\(ossUploadID)"
  152. debugPrint("authServerUrl is: \(authServerUrl)")
  153. debugPrint("当前上传authServerUrl线程:\(Thread.isMainThread) ")
  154. let provider = OSSAuthCredentialProvider(authServerUrl: authServerUrl) { (data) -> Data? in
  155. debugPrint("当前上传provider线程:\(Thread.isMainThread) ")
  156. // 在 OSSModel 代码中解析有自己的格式 所以接收到我方服务器后的数据要进行二次处理 AccessKeyId 等信息
  157. let str = String(data: data, encoding: .utf8)
  158. let jsonDic = jsonStringToDictionary(str!)
  159. var respDic: [String: Any] = [:]
  160. if jsonDic?.keys.contains("code") ?? false, "\(jsonDic?["code"] ?? "")" == "0" {
  161. respDic = [
  162. "StatusCode": "200",
  163. "AccessKeyId": (jsonDic?["data"] as? [String: Any])?["AccessKeyId"] ?? accessKeyId,
  164. "AccessKeySecret": (jsonDic?["data"] as? [String: Any])?["AccessKeySecret"] ?? secretKeyId,
  165. "SecurityToken": (jsonDic?["data"] as? [String: Any])?["SecurityToken"] ?? securityToken,
  166. "Expiration": (jsonDic?["data"] as? [String: Any])?["Expiration"] ?? securityToken,
  167. ]
  168. }
  169. let proStr = dictionaryToJsonString(respDic as [String: Any])
  170. debugPrint("处理后准备给 OSS SDK数据 \(String(describing: proStr))")
  171. let decodedData = proStr?.data(using: .utf8)
  172. if decodedData != nil {
  173. return decodedData
  174. }
  175. return data
  176. }
  177. // 2 拼装 request
  178. let request = OSSMultipartUploadRequest()
  179. // add by ak set contect type . doc https://help.aliyun.com/knowledge_detail/39522.html
  180. request.contentType = fileURL.absoluteString.mimeType() // 媒体类型
  181. request.uploadingFileURL = fileURL
  182. request.bucketName = bucketName
  183. request.objectKey = FileName
  184. // 根据文件大小 设置 part size
  185. var fileSize: UInt64 = 0
  186. do {
  187. let attr = try FileManager.default.attributesOfItem(atPath: fileURL.relativePath)
  188. fileSize = attr[FileAttributeKey.size] as! UInt64
  189. } catch {
  190. debugPrint("取文件大小 Error: \(error)")
  191. if aliOssHander != nil {
  192. DispatchQueue.main.async { [weak self] in
  193. self?.aliOssHander!(isMatarialUpload, materialType, fileURL.absoluteString.pathExtension, 260, FileName, contentMD5, width, height, 0, 0, nil, fileURL, nil, "文件已丢失")
  194. }
  195. }
  196. }
  197. debugPrint("文件已经存在 \(fileURL) 文件大小 \(fileSize)")
  198. var partSize: Int = 0
  199. // sdk中规定kClientMaximumOfChunks = 5000;
  200. let defaultChunkSize: UInt64 = 1024 * 1024
  201. if ceil(Float(fileSize / defaultChunkSize)) <= 1 {
  202. partSize = 500 * 1024
  203. } else if ceil(Float(fileSize / defaultChunkSize)) < 5000 {
  204. partSize = Int(fileSize / UInt64(ceil(Float(fileSize / defaultChunkSize))))
  205. } else {
  206. partSize = Int(ceil(Float(fileSize / 5000)))
  207. }
  208. debugPrint("partSize \(partSize)")
  209. // 除最后一片外 不能小于 102400(100kb)
  210. request.partSize = UInt(partSize)
  211. request.uploadId = ossUploadID
  212. request.uploadProgress = { [weak self] (bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) -> Void in
  213. debugPrint("当前上传uploadProgress线程:\(Thread.isMainThread) ")
  214. self?.paraseSpeedAndRestTime(bytesSent: bytesSent, totalBytesSent: totalBytesSent, totalBytesExpectedToSend: totalBytesExpectedToSend) { uploadSpeed, resttime in
  215. if self?.aliOssProgressHander != nil {
  216. DispatchQueue.main.async { [weak self] in
  217. self?.aliOssProgressHander!(bytesSent, totalBytesSent, totalBytesExpectedToSend, resttime, uploadSpeed)
  218. }
  219. }
  220. }
  221. // 多个界面都要处理上传进度 所以使用通知 ? 通知有回到主线?
  222. // postNotification(name: cOSSUploadFileProgress,userInfo: ["bytesSent":bytesSent,"totalBytesSent":totalBytesSent,"totalBytesExpectedToSend":totalBytesExpectedToSend,"resttime":resttime])
  223. }
  224. // 3,设置 config
  225. let conf = OSSClientConfiguration()
  226. conf.timeoutIntervalForRequest = 30 // 连接超时,默认15秒
  227. conf.maxConcurrentRequestCount = 5 // 最大并发请求书,默认5个
  228. conf.maxRetryCount = 3 // 失败后最大重试次数,默认2次
  229. // 打开后台上传
  230. conf.enableBackgroundTransmitService = true
  231. conf.backgroundSesseionIdentifier = getUniqueId(desc: "\(ossUploadID)\(fileURL.absoluteString)\(FileName)")
  232. // 4,设置 clinet
  233. let client = OSSClient(endpoint: endpoints[0], credentialProvider: provider, clientConfiguration: conf)
  234. if !isMatarialUpload {
  235. allTasks[FileName] = request
  236. }
  237. // 5,发起任务
  238. let task = client.multipartUpload(request)
  239. task.continue ({ [weak self] (osstask) -> Any? in
  240. debugPrint("当前上传线程:\(Thread.isMainThread) ")
  241. if osstask.error == nil {
  242. debugPrint("上传成功")
  243. // 文件URL的格式为:BucketName.Endpoint/ObjectName。
  244. debugPrint(" 上传成功注意使用时拼接域名 url == \(FileName)")
  245. request.callbackParam = ["code": 1, "objectKey": FileName, "msg": "上传成功"]
  246. DispatchQueue.main.async { [weak self] in
  247. if self?.aliOssHander != nil {
  248. self?.aliOssHander!(isMatarialUpload, materialType, fileURL.absoluteString.pathExtension, 1, FileName, contentMD5, width, height, 0, 0, nil, fileURL, nil, "上传成功")
  249. }
  250. }
  251. // 如果在后台发送上传成功的本地通知
  252. if !isMatarialUpload {
  253. DispatchQueue.main.async {
  254. if UIApplication.shared.applicationState == .background {
  255. sendUploadNotification(isSuccess: true)
  256. }
  257. }
  258. postNotification(name: cUploadSuccessKey, userInfo: ["code": 1, "objectKey": FileName, "msg": "上传成功"])
  259. }
  260. // 上传完成
  261. var extParams:Dictionary<String,Any>?
  262. if videoSource != nil && (videoSource?.count ?? 0) > 0 {
  263. extParams = ["source":videoSource ?? ""]
  264. }
  265. PQEventTrackViewModel.baseReportUpload(businessType: .bt_up_process, objectType: .ot_up_success, pageSource: nil,extParams: extParams, remindmsg: "上传相关")
  266. } else {
  267. debugPrint("上传失败 \(osstask.error!)")
  268. request.callbackParam = ["code": (osstask.error! as NSError).code, "objectKey": FileName, "msg": osstask.error?.localizedDescription ?? ""]
  269. if self?.aliOssHander != nil {
  270. DispatchQueue.main.async { [weak self] in
  271. self?.aliOssHander!(isMatarialUpload, materialType, fileURL.absoluteString.pathExtension, (osstask.error! as NSError).code, FileName, contentMD5, width, height, 0, 0, nil, fileURL, nil, osstask.error?.localizedDescription)
  272. }
  273. }
  274. // 如果在后台发送上传失败的本地通知
  275. if !isMatarialUpload {
  276. DispatchQueue.main.async {
  277. if UIApplication.shared.applicationState == .background {
  278. sendUploadNotification(isSuccess: false)
  279. }
  280. }
  281. }
  282. // 上传失败
  283. var extParams:Dictionary<String,Any>?
  284. if videoSource != nil && (videoSource?.count ?? 0) > 0 {
  285. extParams = ["source":videoSource ?? ""]
  286. }
  287. PQEventTrackViewModel.baseReportUpload(businessType: .bt_up_process, objectType: .ot_up_fail, pageSource: nil,extParams: extParams, remindmsg: "上传相关")
  288. }
  289. return nil
  290. })
  291. // 开始上传
  292. var extParams:Dictionary<String,Any>?
  293. if videoSource != nil && (videoSource?.count ?? 0) > 0 {
  294. extParams = ["source":videoSource ?? ""]
  295. }
  296. PQEventTrackViewModel.baseReportUpload(businessType: .bt_up_process, objectType: .ot_up_start, pageSource: .sp_upload_coverSelect,extParams: extParams, remindmsg: "上传相关")
  297. return .shared
  298. }
  299. /// add by ak 简单上传方式 本方法支持后台运行 设置支持后台时会把 DATA
  300. /// - Parameters:
  301. /// - accessKeyId: <#accessKeyId description#>
  302. /// - secretKeyId: <#secretKeyId description#>
  303. /// - securityToken: <#securityToken description#>
  304. /// - bucketName: <#bucketName description#>
  305. /// - endpoint: oss host域名
  306. /// - objectKey: oss 资源地址
  307. /// - fileURL: 文件本地地址
  308. /// - data: 文件数据
  309. /// - fileExtensions: 文件后缀名 mp4/mp3
  310. /// - enableBackground: 是否支持后台下载
  311. /// - materialType: 素材类型
  312. /// - isMatarialUpload: 是否是素材上传
  313. /// - contentMD5: 素材内容MD5值 isMatarialUpload = true时传
  314. /// - width: 视频宽 isMatarialUpload = true时传
  315. /// - height: 视频高 isMatarialUpload = true时传
  316. /// - duration: 素材时长 isMatarialUpload = true时传
  317. /// - frameNumber: gif素材帧数 isMatarialUpload = true时传
  318. /// - netResourceUrl: 网络素材地址 isMatarialUpload = true时传
  319. /// - Returns: <#description#>
  320. public func putObjectAsync(accessKeyId: String, secretKeyId: String, securityToken: String, bucketName: String, endpoint: [String], objectKey: String, fileURL: URL?, data: Data?, fileExtensions: String, enableBackground: Bool, materialType: StickerType = .VIDEO, isMatarialUpload: Bool = false, contentMD5: String = "", width: CGFloat = 0, height: CGFloat = 0, duration: CGFloat, frameNumber: Int, netResourceUrl: String? = nil,videoSource:String? = nil) -> PQAliOssUtil {
  321. #if DEBUG
  322. OSSLog.enable()
  323. #endif
  324. debugPrint("普通上传数据 参数 accessKeyId:\(accessKeyId) secretKeyId:\(secretKeyId) securityToken:\(securityToken) bucketName:\(bucketName) endpoint:\(endpoint) objectKey:\(objectKey) enableBackground:\(enableBackground) fileURL : \(fileURL!)")
  325. if endpoint.count <= 0 || (endpoint.first?.count ?? 0) <= 0 {
  326. debugPrint("endpoints 为空")
  327. return .shared
  328. }
  329. let provider = OSSStsTokenCredentialProvider(accessKeyId: accessKeyId, secretKeyId: secretKeyId, securityToken: securityToken)
  330. // let configuration = OSSClientConfiguration()
  331. // if enableBackground {
  332. // configuration.enableBackgroundTransmitService = true
  333. // configuration.backgroundSesseionIdentifier = objectKey
  334. // }
  335. // configuration.timeoutIntervalForRequest = 60 // 连接超时,默认15秒
  336. // configuration.maxConcurrentRequestCount = 20 // 最大并发请求书,默认5个
  337. // configuration.maxRetryCount = 3 // 失败后最大重试次数,默认2次
  338. startClient(accessKeyId: accessKeyId, secretKeyId: secretKeyId, securityToken: securityToken, endpoint: endpoint.first ?? "")
  339. let request = OSSPutObjectRequest()
  340. if fileURL != nil {
  341. request.uploadingFileURL = fileURL!
  342. request.contentType = fileURL!.absoluteString.mimeType() // 媒体类型
  343. } else if data != nil {
  344. request.uploadingData = data!
  345. request.contentType = materialType.mimeType() // 媒体类型
  346. }
  347. request.bucketName = bucketName
  348. request.objectKey = objectKey
  349. // request.contentMd5 = contentMD5
  350. request.uploadProgress = { (bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) -> Void in
  351. var resttime: Int64 = 0
  352. if self.oldTime == 0 {
  353. self.oldTime = Int(Date().timeIntervalSince1970)
  354. }
  355. let nowTime = Int(Date().timeIntervalSince1970)
  356. let pertime = Int(nowTime - self.oldTime)
  357. if pertime != 0 {
  358. var speed = totalBytesSent / Int64(pertime) // 单位b/s
  359. if speed != 0 {
  360. resttime = ((totalBytesExpectedToSend - totalBytesSent) / speed)
  361. var units = "b/s" // 单位名称
  362. if speed / 1024 > 1 {
  363. speed = speed / 1024
  364. units = "k/s"
  365. }
  366. if speed / 1024 > 1 {
  367. speed = speed / 1024
  368. units = "M/s"
  369. }
  370. debugPrint("上传速度: \(speed)\(units) 还剩时间 \(resttime)")
  371. }
  372. }
  373. // 多个界面都要处理上传进度 所以使用通知 ? 通知有回到主线?
  374. postNotification(name: cOSSUploadFileProgress, userInfo: ["bytesSent": bytesSent, "totalBytesSent": totalBytesSent, "totalBytesExpectedToSend": totalBytesExpectedToSend, "resttime": resttime])
  375. }
  376. let task = client?.putObject(request)
  377. if fileURL?.absoluteString.contains("_noise_") ?? false {
  378. task?.continue ({ [weak self] (osstask) -> Any? in
  379. debugPrint("当前上传线程:\(Thread.isMainThread) ")
  380. if osstask.error == nil {
  381. debugPrint("上传成功")
  382. // 文件URL的格式为:BucketName.Endpoint/ObjectName。
  383. debugPrint(" 上传成功注意使用时拼接域名 url == \(objectKey)")
  384. request.callbackParam = ["code": 1, "objectKey": objectKey, "msg": "上传成功"]
  385. DispatchQueue.main.async { [weak self] in
  386. if self?.aliOssHander != nil {
  387. self?.aliOssHander!(isMatarialUpload, materialType, fileExtensions, 1, objectKey, contentMD5, width, height, duration, frameNumber, netResourceUrl, fileURL, data, "上传成功")
  388. }
  389. }
  390. // 如果在后台发送上传成功的本地通知
  391. if !isMatarialUpload {
  392. DispatchQueue.main.async {
  393. if UIApplication.shared.applicationState == .background {
  394. sendUploadNotification(isSuccess: true)
  395. }
  396. }
  397. postNotification(name: cUploadSuccessKey, userInfo: ["code": 1, "objectKey": objectKey, "msg": "上传成功"])
  398. }
  399. // 上传完成
  400. var extParams:Dictionary<String,Any>?
  401. if videoSource != nil && (videoSource?.count ?? 0) > 0 {
  402. extParams = ["source":videoSource ?? ""]
  403. }
  404. // 上传完成
  405. PQEventTrackViewModel.baseReportUpload(businessType: .bt_up_process, objectType: .ot_up_success, pageSource: nil,extParams: extParams, remindmsg: "上传相关")
  406. } else {
  407. debugPrint("上传失败 \(osstask.error!)")
  408. request.callbackParam = ["code": (osstask.error! as NSError).code, "objectKey": objectKey, "msg": osstask.error?.localizedDescription ?? ""]
  409. if self?.aliOssHander != nil {
  410. DispatchQueue.main.async { [weak self] in
  411. self?.aliOssHander!(isMatarialUpload, materialType, fileExtensions, (osstask.error! as NSError).code, objectKey, contentMD5, width, height, duration, frameNumber, netResourceUrl, fileURL, data, osstask.error?.localizedDescription)
  412. }
  413. }
  414. // 如果在后台发送上传失败的本地通知
  415. if !isMatarialUpload {
  416. DispatchQueue.main.async {
  417. if UIApplication.shared.applicationState == .background {
  418. sendUploadNotification(isSuccess: false)
  419. }
  420. }
  421. }
  422. // 上传失败
  423. PQEventTrackViewModel.baseReportUpload(businessType: .bt_up_process, objectType: .ot_up_fail, pageSource: nil, remindmsg: "上传相关")
  424. }
  425. return nil
  426. }).waitUntilFinished()
  427. } else {
  428. task?.continue ({ [weak self] (osstask) -> Any? in
  429. debugPrint("当前上传线程:\(Thread.isMainThread) ")
  430. if osstask.error == nil {
  431. debugPrint("上传成功")
  432. // 文件URL的格式为:BucketName.Endpoint/ObjectName。
  433. debugPrint(" 上传成功注意使用时拼接域名 url == \(objectKey)")
  434. request.callbackParam = ["code": 1, "objectKey": objectKey, "msg": "上传成功"]
  435. DispatchQueue.main.async { [weak self] in
  436. if self?.aliOssHander != nil {
  437. self?.aliOssHander!(isMatarialUpload, materialType, fileExtensions, 1, objectKey, contentMD5, width, height, duration, frameNumber, netResourceUrl, fileURL, data, "上传成功")
  438. }
  439. }
  440. // 如果在后台发送上传成功的本地通知
  441. if !isMatarialUpload {
  442. DispatchQueue.main.async {
  443. if UIApplication.shared.applicationState == .background {
  444. sendUploadNotification(isSuccess: true)
  445. }
  446. }
  447. postNotification(name: cUploadSuccessKey, userInfo: ["code": 1, "objectKey": objectKey, "msg": "上传成功"])
  448. }
  449. // 上传完成
  450. PQEventTrackViewModel.baseReportUpload(businessType: .bt_up_process, objectType: .ot_up_success, pageSource: nil, remindmsg: "上传相关")
  451. } else {
  452. debugPrint("上传失败 \(osstask.error!)")
  453. request.callbackParam = ["code": (osstask.error! as NSError).code, "objectKey": objectKey, "msg": osstask.error?.localizedDescription ?? ""]
  454. if self?.aliOssHander != nil {
  455. DispatchQueue.main.async { [weak self] in
  456. self?.aliOssHander!(isMatarialUpload, materialType, fileExtensions, (osstask.error! as NSError).code, objectKey, contentMD5, width, height, duration, frameNumber, netResourceUrl, fileURL, data, osstask.error?.localizedDescription)
  457. }
  458. }
  459. // 如果在后台发送上传失败的本地通知
  460. if !isMatarialUpload {
  461. DispatchQueue.main.async {
  462. if UIApplication.shared.applicationState == .background {
  463. sendUploadNotification(isSuccess: false)
  464. }
  465. }
  466. }
  467. // 上传失败
  468. PQEventTrackViewModel.baseReportUpload(businessType: .bt_up_process, objectType: .ot_up_fail, pageSource: nil, remindmsg: "上传相关")
  469. }
  470. return nil
  471. })
  472. }
  473. return .shared
  474. }
  475. /// 取消某个任务
  476. /// - Parameter objectKey: 任务唯一标识
  477. public func putObjectCancel(objectKey: String) {
  478. for key in allTasks.keys {
  479. if key == objectKey {
  480. allTasks[key]!.cancel()
  481. allTasks.removeValue(forKey: key)
  482. }
  483. }
  484. }
  485. /// 解析上传速度及剩余时间
  486. /// - Parameters:
  487. /// - bytesSent: <#bytesSent description#>
  488. /// - totalBytesSent: <#totalBytesSent description#>
  489. /// - totalBytesExpectedToSend: <#totalBytesExpectedToSend description#>
  490. /// - complateHandle: <#complateHandle description#>
  491. /// - Returns: <#description#>
  492. public func paraseSpeedAndRestTime(bytesSent _: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64, complateHandle: (_ uploadSpeed: String, _ resttime: Int64) -> Void) {
  493. let newInterfaceBytes = PQBridgeObject.getInterfaceBytes()
  494. var interfaceBytes = abs(newInterfaceBytes - lastInterfaceBytes)
  495. if interfaceBytes <= 0 {
  496. interfaceBytes = 1
  497. }
  498. lastInterfaceBytes = newInterfaceBytes
  499. let resttime = (totalBytesExpectedToSend - totalBytesSent) / interfaceBytes
  500. debugPrint("interfaceBytes = \(interfaceBytes),totalBytesExpectedToSend = \(totalBytesExpectedToSend),totalBytesSent = \(totalBytesSent),")
  501. complateHandle("\(PQBridgeObject.formatNetWork(interfaceBytes))", resttime)
  502. }
  503. override private init() {
  504. super.init()
  505. }
  506. public override func copy() -> Any {
  507. return self
  508. }
  509. public override func mutableCopy() -> Any {
  510. return self
  511. }
  512. }