NCNetworking+Upload.swift 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. //
  2. // NCNetworking+Upload.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 07/02/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import JGProgressHUD
  25. import NextcloudKit
  26. import Alamofire
  27. extension NCNetworking {
  28. func upload(metadata: tableMetadata,
  29. uploadE2EEDelegate: uploadE2EEDelegate? = nil,
  30. hudView: UIView?,
  31. hud: JGProgressHUD?,
  32. start: @escaping () -> Void = { },
  33. requestHandler: @escaping (_ request: UploadRequest) -> Void = { _ in },
  34. progressHandler: @escaping (_ totalBytesExpected: Int64, _ totalBytes: Int64, _ fractionCompleted: Double) -> Void = { _, _, _ in },
  35. completion: @escaping (_ afError: AFError?, _ error: NKError) -> Void = { _, _ in }) {
  36. let metadata = tableMetadata.init(value: metadata)
  37. var numChunks: Int = 0
  38. NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] Upload file \(metadata.fileNameView) with Identifier \(metadata.assetLocalIdentifier) with size \(metadata.size) [CHUNK \(metadata.chunk), E2EE \(metadata.isDirectoryE2EE)]")
  39. if metadata.isDirectoryE2EE {
  40. #if !EXTENSION_FILE_PROVIDER_EXTENSION && !EXTENSION_WIDGET
  41. Task {
  42. let error = await NCNetworkingE2EEUpload().upload(metadata: metadata, uploadE2EEDelegate: uploadE2EEDelegate, hudView: hudView, hud: hud)
  43. completion(nil, error)
  44. }
  45. #endif
  46. } else if metadata.chunk > 0 {
  47. if let hudView {
  48. DispatchQueue.main.async {
  49. if let hud {
  50. hud.indicatorView = JGProgressHUDRingIndicatorView()
  51. if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView {
  52. indicatorView.ringWidth = 1.5
  53. }
  54. hud.tapOnHUDViewBlock = { _ in
  55. NotificationCenter.default.postOnMainThread(name: "NextcloudKit.chunkedFile.stop")
  56. }
  57. hud.textLabel.text = NSLocalizedString("_wait_file_preparation_", comment: "")
  58. hud.detailTextLabel.text = NSLocalizedString("_tap_to_cancel_", comment: "")
  59. hud.show(in: hudView)
  60. }
  61. }
  62. }
  63. uploadChunkFile(metadata: metadata) { num in
  64. numChunks = num
  65. } counterChunk: { counter in
  66. DispatchQueue.main.async { hud?.progress = Float(counter) / Float(numChunks) }
  67. } start: {
  68. DispatchQueue.main.async { hud?.dismiss() }
  69. } completion: { account, _, afError, error in
  70. DispatchQueue.main.async { hud?.dismiss() }
  71. var sessionTaskFailedCode = 0
  72. let directory = self.utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId)
  73. if let error = NextcloudKit.shared.nkCommonInstance.getSessionErrorFromAFError(afError) {
  74. sessionTaskFailedCode = error.code
  75. }
  76. switch error.errorCode {
  77. case NKError.chunkNoEnoughMemory, NKError.chunkCreateFolder, NKError.chunkFilesNull, NKError.chunkFileNull:
  78. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  79. NCManageDatabase.shared.deleteChunks(account: account, ocId: metadata.ocId, directory: directory)
  80. NCContentPresenter().messageNotification("_error_files_upload_", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error, afterDelay: 0.5)
  81. case NKError.chunkFileUpload:
  82. if let afError, (afError.isExplicitlyCancelledError || sessionTaskFailedCode == NCGlobal.shared.errorExplicitlyCancelled ) {
  83. NCManageDatabase.shared.deleteChunks(account: account, ocId: metadata.ocId, directory: directory)
  84. }
  85. case NKError.chunkMoveFile:
  86. NCManageDatabase.shared.deleteChunks(account: account, ocId: metadata.ocId, directory: directory)
  87. NCContentPresenter().messageNotification("_chunk_move_", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error, afterDelay: 0.5)
  88. default: break
  89. }
  90. completion(afError, error)
  91. }
  92. } else if metadata.session == NextcloudKit.shared.nkCommonInstance.sessionIdentifierUpload {
  93. let fileNameLocalPath = utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)
  94. uploadFile(metadata: metadata, fileNameLocalPath: fileNameLocalPath, start: start, progressHandler: progressHandler) { _, _, _, _, _, _, afError, error in
  95. completion(afError, error)
  96. }
  97. } else {
  98. uploadFileInBackground(metadata: metadata, start: start) { error in
  99. completion(nil, error)
  100. }
  101. }
  102. }
  103. func uploadFile(metadata: tableMetadata,
  104. fileNameLocalPath: String,
  105. withUploadComplete: Bool = true,
  106. customHeaders: [String: String]? = nil,
  107. start: @escaping () -> Void = { },
  108. requestHandler: @escaping (_ request: UploadRequest) -> Void = { _ in },
  109. progressHandler: @escaping (_ totalBytesExpected: Int64, _ totalBytes: Int64, _ fractionCompleted: Double) -> Void = { _, _, _ in },
  110. completion: @escaping (_ account: String, _ ocId: String?, _ etag: String?, _ date: NSDate?, _ size: Int64, _ allHeaderFields: [AnyHashable: Any]?, _ afError: AFError?, _ error: NKError) -> Void) {
  111. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
  112. var uploadTask: URLSessionTask?
  113. let options = NKRequestOptions(customHeader: customHeaders, queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)
  114. NextcloudKit.shared.upload(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, dateCreationFile: metadata.creationDate as Date, dateModificationFile: metadata.date as Date, options: options, requestHandler: { request in
  115. self.uploadRequest[fileNameLocalPath] = request
  116. NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId,
  117. status: NCGlobal.shared.metadataStatusUploading)
  118. requestHandler(request)
  119. }, taskHandler: { task in
  120. uploadTask = task
  121. NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId,
  122. taskIdentifier: task.taskIdentifier)
  123. NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterUploadStartFile),
  124. object: nil,
  125. userInfo: ["ocId": metadata.ocId,
  126. "serverUrl": metadata.serverUrl,
  127. "account": metadata.account,
  128. "fileName": metadata.fileName,
  129. "sessionSelector": metadata.sessionSelector])
  130. start()
  131. }, progressHandler: { progress in
  132. if Int(floor(progress.fractionCompleted * 100)).isMultiple(of: 5) {
  133. NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask),
  134. object: nil,
  135. userInfo: ["account": metadata.account,
  136. "ocId": metadata.ocId,
  137. "fileName": metadata.fileName,
  138. "serverUrl": metadata.serverUrl,
  139. "status": NSNumber(value: NCGlobal.shared.metadataStatusUploading),
  140. "progress": NSNumber(value: progress.fractionCompleted),
  141. "totalBytes": NSNumber(value: progress.totalUnitCount),
  142. "totalBytesExpected": NSNumber(value: progress.completedUnitCount)])
  143. }
  144. progressHandler(progress.completedUnitCount, progress.totalUnitCount, progress.fractionCompleted)
  145. }) { account, ocId, etag, date, size, allHeaderFields, afError, error in
  146. var error = error
  147. self.uploadRequest.removeValue(forKey: fileNameLocalPath)
  148. if withUploadComplete, let uploadTask = uploadTask {
  149. if afError?.isExplicitlyCancelledError ?? false {
  150. error = NKError(errorCode: NCGlobal.shared.errorRequestExplicityCancelled, errorDescription: "error request explicity cancelled")
  151. }
  152. self.uploadComplete(fileName: metadata.fileName, serverUrl: metadata.serverUrl, ocId: ocId, etag: etag, date: date, size: size, fileNameLocalPath: fileNameLocalPath, task: uploadTask, error: error)
  153. }
  154. completion(account, ocId, etag, date, size, allHeaderFields, afError, error)
  155. }
  156. }
  157. func uploadChunkFile(metadata: tableMetadata,
  158. withUploadComplete: Bool = true,
  159. customHeaders: [String: String]? = nil,
  160. numChunks: @escaping (_ num: Int) -> Void = { _ in },
  161. counterChunk: @escaping (_ counter: Int) -> Void = { _ in },
  162. start: @escaping () -> Void = { },
  163. progressHandler: @escaping (_ totalBytesExpected: Int64, _ totalBytes: Int64, _ fractionCompleted: Double) -> Void = { _, _, _ in },
  164. completion: @escaping (_ account: String, _ file: NKFile?, _ afError: AFError?, _ error: NKError) -> Void) {
  165. let directory = utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId)
  166. let fileNameLocalPath = utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)
  167. let chunkFolder = NCManageDatabase.shared.getChunkFolder(account: metadata.account, ocId: metadata.ocId)
  168. let filesChunk = NCManageDatabase.shared.getChunks(account: metadata.account, ocId: metadata.ocId)
  169. var uploadTask: URLSessionTask?
  170. var chunkSize = NCGlobal.shared.chunkSizeMBCellular
  171. if NCNetworking.shared.networkReachability == NKCommon.TypeReachability.reachableEthernetOrWiFi {
  172. chunkSize = NCGlobal.shared.chunkSizeMBEthernetOrWiFi
  173. }
  174. let options = NKRequestOptions(customHeader: customHeaders, queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)
  175. NextcloudKit.shared.uploadChunk(directory: directory, fileName: metadata.fileName, date: metadata.date as Date, creationDate: metadata.creationDate as Date, serverUrl: metadata.serverUrl, chunkFolder: chunkFolder, filesChunk: filesChunk, chunkSize: chunkSize, options: options) { num in
  176. numChunks(num)
  177. } counterChunk: { counter in
  178. counterChunk(counter)
  179. } start: { filesChunk in
  180. start()
  181. NCManageDatabase.shared.addChunks(account: metadata.account, ocId: metadata.ocId, chunkFolder: chunkFolder, filesChunk: filesChunk)
  182. NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterUploadStartFile),
  183. object: nil,
  184. userInfo: ["ocId": metadata.ocId,
  185. "serverUrl": metadata.serverUrl,
  186. "account": metadata.account,
  187. "fileName": metadata.fileName,
  188. "sessionSelector": metadata.sessionSelector])
  189. } requestHandler: { request in
  190. self.uploadRequest[fileNameLocalPath] = request
  191. NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId,
  192. status: NCGlobal.shared.metadataStatusUploading)
  193. } taskHandler: { task in
  194. uploadTask = task
  195. NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId,
  196. taskIdentifier: task.taskIdentifier)
  197. } progressHandler: { totalBytesExpected, totalBytes, fractionCompleted in
  198. NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask),
  199. object: nil,
  200. userInfo: ["account": metadata.account,
  201. "ocId": metadata.ocId,
  202. "fileName": metadata.fileName,
  203. "serverUrl": metadata.serverUrl,
  204. "status": NSNumber(value: NCGlobal.shared.metadataStatusUploading),
  205. "chunk": metadata.chunk,
  206. "e2eEncrypted": metadata.e2eEncrypted,
  207. "progress": NSNumber(value: fractionCompleted),
  208. "totalBytes": NSNumber(value: totalBytes),
  209. "totalBytesExpected": NSNumber(value: totalBytesExpected)])
  210. progressHandler(totalBytesExpected, totalBytes, fractionCompleted)
  211. } uploaded: { fileChunk in
  212. NCManageDatabase.shared.deleteChunk(account: metadata.account, ocId: metadata.ocId, fileChunk: fileChunk, directory: directory)
  213. } completion: { account, _, file, afError, error in
  214. self.uploadRequest.removeValue(forKey: fileNameLocalPath)
  215. if error == .success {
  216. NCManageDatabase.shared.deleteChunks(account: account, ocId: metadata.ocId, directory: directory)
  217. }
  218. if withUploadComplete, let uploadTask {
  219. self.uploadComplete(fileName: metadata.fileName, serverUrl: metadata.serverUrl, ocId: file?.ocId, etag: file?.etag, date: file?.date, size: file?.size ?? 0, fileNameLocalPath: fileNameLocalPath, task: uploadTask, error: error)
  220. }
  221. completion(account, file, afError, error)
  222. }
  223. }
  224. private func uploadFileInBackground(metadata: tableMetadata,
  225. start: @escaping () -> Void = { },
  226. completion: @escaping (_ error: NKError) -> Void) {
  227. var session: URLSession?
  228. let metadata = tableMetadata.init(value: metadata)
  229. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
  230. let fileNameLocalPath = utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)
  231. if metadata.session == sessionUploadBackground || metadata.session == sessionUploadBackgroundExtension {
  232. session = sessionManagerUploadBackground
  233. } else if metadata.session == sessionUploadBackgroundWWan {
  234. session = sessionManagerUploadBackgroundWWan
  235. }
  236. start()
  237. // Check file dim > 0
  238. if utilityFileSystem.getFileSize(filePath: fileNameLocalPath) == 0 && metadata.size != 0 {
  239. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  240. completion(NKError(errorCode: NCGlobal.shared.errorResourceNotFound, errorDescription: NSLocalizedString("_error_not_found_", value: "The requested resource could not be found", comment: "")))
  241. } else {
  242. if let task = nkBackground.upload(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, dateCreationFile: metadata.creationDate as Date, dateModificationFile: metadata.date as Date, session: session!) {
  243. NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] Upload file \(metadata.fileNameView) with task with taskIdentifier \(task.taskIdentifier)")
  244. NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId,
  245. status: NCGlobal.shared.metadataStatusUploading,
  246. taskIdentifier: task.taskIdentifier)
  247. NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterUploadStartFile),
  248. object: nil,
  249. userInfo: ["ocId": metadata.ocId,
  250. "serverUrl": metadata.serverUrl,
  251. "account": metadata.account,
  252. "fileName": metadata.fileName,
  253. "sessionSelector": metadata.sessionSelector])
  254. completion(NKError())
  255. } else {
  256. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  257. completion(NKError(errorCode: NCGlobal.shared.errorResourceNotFound, errorDescription: "task null"))
  258. }
  259. }
  260. }
  261. func uploadComplete(fileName: String,
  262. serverUrl: String,
  263. ocId: String?,
  264. etag: String?,
  265. date: NSDate?,
  266. size: Int64,
  267. fileNameLocalPath: String?,
  268. task: URLSessionTask,
  269. error: NKError) {
  270. var isApplicationStateActive = false
  271. #if !EXTENSION
  272. isApplicationStateActive = UIApplication.shared.applicationState == .active
  273. #endif
  274. DispatchQueue.global().async {
  275. guard let metadata = NCManageDatabase.shared.getMetadataFromFileNameLocalPath(fileNameLocalPath) else { return }
  276. let ocIdTemp = metadata.ocId
  277. let selector = metadata.sessionSelector
  278. if error == .success, let ocId = ocId, size == metadata.size {
  279. let metadata = tableMetadata.init(value: metadata)
  280. metadata.uploadDate = date ?? NSDate()
  281. metadata.etag = etag ?? ""
  282. metadata.ocId = ocId
  283. metadata.chunk = 0
  284. if let fileId = self.utility.ocIdToFileId(ocId: ocId) {
  285. metadata.fileId = fileId
  286. }
  287. metadata.session = ""
  288. metadata.sessionError = ""
  289. metadata.status = NCGlobal.shared.metadataStatusNormal
  290. // Delete Asset on Photos album
  291. if NCKeychain().removePhotoCameraRoll, !metadata.assetLocalIdentifier.isEmpty {
  292. metadata.deleteAssetLocalIdentifier = true
  293. }
  294. NCManageDatabase.shared.addMetadata(metadata)
  295. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", ocIdTemp))
  296. if selector == NCGlobal.shared.selectorUploadFileNODelete {
  297. self.utilityFileSystem.moveFile(atPath: self.utilityFileSystem.getDirectoryProviderStorageOcId(ocIdTemp), toPath: self.utilityFileSystem.getDirectoryProviderStorageOcId(ocId))
  298. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  299. } else {
  300. self.utilityFileSystem.removeFile(atPath: self.utilityFileSystem.getDirectoryProviderStorageOcId(ocIdTemp))
  301. }
  302. NextcloudKit.shared.nkCommonInstance.writeLog("[SUCCESS] Upload complete " + serverUrl + "/" + fileName + ", result: success(\(size) bytes)")
  303. let userInfo: [AnyHashable: Any] = ["ocId": metadata.ocId, "serverUrl": metadata.serverUrl, "account": metadata.account, "fileName": metadata.fileName, "ocIdTemp": ocIdTemp, "error": error]
  304. if metadata.isLivePhoto, NCGlobal.shared.isLivePhotoServerAvailable {
  305. self.uploadLivePhoto(metadata: metadata, userInfo: userInfo)
  306. } else {
  307. NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile),
  308. object: nil,
  309. userInfo: userInfo)
  310. }
  311. } else {
  312. if error.errorCode == NSURLErrorCancelled || error.errorCode == NCGlobal.shared.errorRequestExplicityCancelled {
  313. self.utilityFileSystem.removeFile(atPath: self.utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId))
  314. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  315. NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterUploadCancelFile),
  316. object: nil,
  317. userInfo: ["ocId": metadata.ocId,
  318. "serverUrl": metadata.serverUrl,
  319. "account": metadata.account])
  320. } else if error.errorCode == NCGlobal.shared.errorBadRequest || error.errorCode == NCGlobal.shared.errorUnsupportedMediaType {
  321. self.utilityFileSystem.removeFile(atPath: self.utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId))
  322. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  323. NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterUploadCancelFile),
  324. object: nil,
  325. userInfo: ["ocId": metadata.ocId,
  326. "serverUrl": metadata.serverUrl,
  327. "account": metadata.account])
  328. if isApplicationStateActive {
  329. NCContentPresenter().showError(error: NKError(errorCode: error.errorCode, errorDescription: "_virus_detect_"))
  330. }
  331. // Client Diagnostic
  332. NCManageDatabase.shared.addDiagnostic(account: metadata.account, issue: NCGlobal.shared.diagnosticIssueVirusDetected)
  333. } else if error.errorCode == NCGlobal.shared.errorForbidden && isApplicationStateActive {
  334. #if !EXTENSION
  335. DispatchQueue.main.async {
  336. let newFileName = self.utilityFileSystem.createFileName(metadata.fileName, serverUrl: metadata.serverUrl, account: metadata.account)
  337. let alertController = UIAlertController(title: error.errorDescription, message: NSLocalizedString("_change_upload_filename_", comment: ""), preferredStyle: .alert)
  338. alertController.addAction(UIAlertAction(title: String(format: NSLocalizedString("_save_file_as_", comment: ""), newFileName), style: .default, handler: { _ in
  339. let atpath = self.utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + metadata.fileName
  340. let toPath = self.utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + newFileName
  341. self.utilityFileSystem.moveFile(atPath: atpath, toPath: toPath)
  342. NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId,
  343. newFileName: newFileName,
  344. sessionError: "",
  345. status: NCGlobal.shared.metadataStatusWaitUpload,
  346. errorCode: error.errorCode)
  347. }))
  348. alertController.addAction(UIAlertAction(title: NSLocalizedString("_discard_changes_", comment: ""), style: .destructive, handler: { _ in
  349. self.utilityFileSystem.removeFile(atPath: self.utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId))
  350. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  351. NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterUploadCancelFile),
  352. object: nil,
  353. userInfo: ["ocId": metadata.ocId,
  354. "serverUrl": metadata.serverUrl,
  355. "account": metadata.account])
  356. }))
  357. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  358. appDelegate.window?.rootViewController?.present(alertController, animated: true)
  359. // Client Diagnostic
  360. NCManageDatabase.shared.addDiagnostic(account: metadata.account, issue: NCGlobal.shared.diagnosticIssueProblems, error: NCGlobal.shared.diagnosticProblemsForbidden)
  361. }
  362. #endif
  363. } else {
  364. NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId,
  365. sessionError: error.errorDescription,
  366. status: NCGlobal.shared.metadataStatusUploadError,
  367. errorCode: error.errorCode)
  368. NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile),
  369. object: nil,
  370. userInfo: ["ocId": metadata.ocId,
  371. "serverUrl": metadata.serverUrl,
  372. "account": metadata.account,
  373. "fileName": metadata.fileName,
  374. "ocIdTemp": ocIdTemp,
  375. "error": error])
  376. // Client Diagnostic
  377. if error.errorCode == NCGlobal.shared.errorInternalServerError {
  378. NCManageDatabase.shared.addDiagnostic(account: metadata.account, issue: NCGlobal.shared.diagnosticIssueProblems, error: NCGlobal.shared.diagnosticProblemsBadResponse)
  379. } else {
  380. NCManageDatabase.shared.addDiagnostic(account: metadata.account, issue: NCGlobal.shared.diagnosticIssueProblems, error: NCGlobal.shared.diagnosticProblemsUploadServerError)
  381. }
  382. }
  383. }
  384. self.uploadMetadataInBackground.removeValue(forKey: FileNameServerUrl(fileName: fileName, serverUrl: serverUrl))
  385. self.delegate?.uploadComplete?(fileName: fileName, serverUrl: serverUrl, ocId: ocId, etag: etag, date: date, size: size, fileNameLocalPath: fileNameLocalPath, task: task, error: error)
  386. }
  387. }
  388. func uploadProgress(_ progress: Float,
  389. totalBytes: Int64,
  390. totalBytesExpected: Int64,
  391. fileName: String,
  392. serverUrl: String,
  393. session: URLSession,
  394. task: URLSessionTask) {
  395. DispatchQueue.global().async {
  396. self.delegate?.uploadProgress?(progress, totalBytes: totalBytes, totalBytesExpected: totalBytesExpected, fileName: fileName, serverUrl: serverUrl, session: session, task: task)
  397. var metadata: tableMetadata?
  398. if let metadataTmp = self.uploadMetadataInBackground[FileNameServerUrl(fileName: fileName, serverUrl: serverUrl)] {
  399. metadata = metadataTmp
  400. } else if let metadataTmp = NCManageDatabase.shared.getMetadataFromFileName(fileName, serverUrl: serverUrl) {
  401. self.uploadMetadataInBackground[FileNameServerUrl(fileName: fileName, serverUrl: serverUrl)] = metadataTmp
  402. metadata = metadataTmp
  403. }
  404. if let metadata = metadata, Int(floor(progress * 100)).isMultiple(of: 5) {
  405. NotificationCenter.default.post(name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask),
  406. object: nil,
  407. userInfo: ["account": metadata.account,
  408. "ocId": metadata.ocId,
  409. "fileName": metadata.fileName,
  410. "serverUrl": serverUrl,
  411. "status": NSNumber(value: NCGlobal.shared.metadataStatusUploading),
  412. "chunk": metadata.chunk,
  413. "e2eEncrypted": metadata.e2eEncrypted,
  414. "progress": NSNumber(value: progress),
  415. "totalBytes": NSNumber(value: totalBytes),
  416. "totalBytesExpected": NSNumber(value: totalBytesExpected)])
  417. }
  418. }
  419. }
  420. func getUploadBackgroundSession(queue: DispatchQueue = .main,
  421. completion: @escaping (_ filesNameLocalPath: [String]) -> Void) {
  422. var filesNameLocalPath: [String] = []
  423. sessionManagerUploadBackground.getAllTasks(completionHandler: { tasks in
  424. for task in tasks {
  425. filesNameLocalPath.append(task.description)
  426. }
  427. self.sessionManagerUploadBackgroundWWan.getAllTasks(completionHandler: { tasks in
  428. for task in tasks {
  429. filesNameLocalPath.append(task.description)
  430. }
  431. queue.async { completion(filesNameLocalPath) }
  432. })
  433. })
  434. }
  435. func cancelUploadTasks() {
  436. uploadRequest.removeAll()
  437. let sessionManager = NextcloudKit.shared.sessionManager
  438. sessionManager.session.getTasksWithCompletionHandler { _, uploadTasks, _ in
  439. uploadTasks.forEach {
  440. $0.cancel()
  441. }
  442. }
  443. if let results = NCManageDatabase.shared.getResultsMetadatas(predicate: NSPredicate(format: "status > 0 AND session == %@", NextcloudKit.shared.nkCommonInstance.sessionIdentifierUpload)) {
  444. NCManageDatabase.shared.deleteMetadata(results: results)
  445. }
  446. }
  447. func cancelUploadBackgroundTask() {
  448. Task {
  449. let tasksBackground = await NCNetworking.shared.sessionManagerUploadBackground.tasks
  450. for task in tasksBackground.1 { // ([URLSessionDataTask], [URLSessionUploadTask], [URLSessionDownloadTask])
  451. task.cancel()
  452. }
  453. let tasksBackgroundWWan = await NCNetworking.shared.sessionManagerUploadBackgroundWWan.tasks
  454. for task in tasksBackgroundWWan.1 { // ([URLSessionDataTask], [URLSessionUploadTask], [URLSessionDownloadTask])
  455. task.cancel()
  456. }
  457. if let results = NCManageDatabase.shared.getResultsMetadatas(predicate: NSPredicate(format: "status > 0 AND (session == %@ || session == %@)", NCNetworking.shared.sessionUploadBackground, NCNetworking.shared.sessionUploadBackgroundWWan)) {
  458. NCManageDatabase.shared.deleteMetadata(results: results)
  459. }
  460. }
  461. }
  462. }