NCNetworkingChunkedUpload.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // NCNetworkingUploadChunk.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 05/04/21.
  6. // Copyright © 2021 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 Foundation
  24. import NCCommunication
  25. import Queuer
  26. extension NCNetworking {
  27. internal func uploadChunkedFile(metadata: tableMetadata, userId: String, completion: @escaping (_ errorCode: Int, _ errorDescription: String)->()) {
  28. let directoryProviderStorageOcId = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId)!
  29. let chunkFolder = NCManageDatabase.shared.getChunkFolder(account: metadata.account, ocId: metadata.ocId)
  30. let chunkFolderPath = metadata.urlBase + "/" + NCUtilityFileSystem.shared.getDAV() + "/uploads/" + userId + "/" + chunkFolder
  31. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  32. let chunkSize = CCUtility.getChunkSize()
  33. var uploadErrorCode: Int = 0
  34. var uploadErrorDescription: String = ""
  35. var counterFileNameInUpload: Int = 0
  36. var filesNames = NCManageDatabase.shared.getChunks(account: metadata.account, ocId: metadata.ocId)
  37. if filesNames.count == 0 {
  38. if let chunkedFilesNames = NCCommunicationCommon.shared.chunkedFile(path: directoryProviderStorageOcId, fileName: metadata.fileName, outPath: directoryProviderStorageOcId, sizeInMB: chunkSize) {
  39. filesNames = chunkedFilesNames
  40. NCManageDatabase.shared.addChunks(account: metadata.account, ocId: metadata.ocId, chunkFolder: chunkFolder, fileNames: filesNames)
  41. } else {
  42. NCContentPresenter.shared.messageNotification("_error_", description: "_err_file_not_found_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode:NCGlobal.shared.errorReadFile, forced: true)
  43. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  44. completion(uploadErrorCode, uploadErrorDescription)
  45. return
  46. }
  47. } else {
  48. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl":metadata.serverUrl])
  49. }
  50. NCContentPresenter.shared.noteTop(text: NSLocalizedString("_upload_chunk_", comment: ""), image: nil, color: .lightGray, type: NCContentPresenter.messageType.info, delay: NCGlobal.shared.dismissAfterSecond, name: nil)
  51. createChunkedFolder(chunkFolderPath: chunkFolderPath, account: metadata.account) { (errorCode, errorDescription) in
  52. completion(errorCode, errorDescription)
  53. if errorCode == 0 {
  54. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterUploadStartFile, userInfo: ["ocId": metadata.ocId])
  55. DispatchQueue.global(qos: .background).async {
  56. for fileName in filesNames {
  57. let serverUrlFileName = chunkFolderPath + "/" + fileName
  58. let fileNameChunkLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: fileName)!
  59. let semaphore = Semaphore()
  60. NCCommunication.shared.upload(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameChunkLocalPath, requestHandler: { (request) in
  61. self.uploadRequest[fileNameLocalPath] = request
  62. counterFileNameInUpload += 1
  63. let progress: Float = Float(counterFileNameInUpload) / Float(filesNames.count)
  64. let totalBytes: Int64 = (metadata.size / Int64(filesNames.count)) * Int64(counterFileNameInUpload)
  65. let totalBytesExpected: Int64 = metadata.size
  66. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterProgressTask, userInfo: ["account":metadata.account, "ocId":metadata.ocId, "serverUrl":metadata.serverUrl, "status":NSNumber(value: NCGlobal.shared.metadataStatusInUpload), "progress":NSNumber(value: progress), "totalBytes":NSNumber(value: totalBytes), "totalBytesExpected":NSNumber(value: totalBytesExpected)])
  67. }, taskHandler: { (task) in
  68. NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId, sessionError: "", sessionTaskIdentifier: task.taskIdentifier, status: NCGlobal.shared.metadataStatusUploading)
  69. }, progressHandler: { (_) in
  70. }) { (_, _, _, _, _, _, _, errorCode, errorDescription) in
  71. self.uploadRequest[fileNameLocalPath] = nil
  72. uploadErrorCode = errorCode
  73. uploadErrorDescription = errorDescription
  74. semaphore.continue()
  75. }
  76. semaphore.wait()
  77. if uploadErrorCode == 0 {
  78. NCManageDatabase.shared.deleteChunk(account: metadata.account, ocId: metadata.ocId, fileName: fileName)
  79. } else {
  80. break
  81. }
  82. }
  83. if uploadErrorCode == 0 {
  84. // Assembling the chunks
  85. let serverUrlFileNameSource = chunkFolderPath + "/.file"
  86. let pathServerUrl = CCUtility.returnPathfromServerUrl(metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
  87. let serverUrlFileNameDestination = metadata.urlBase + "/" + NCUtilityFileSystem.shared.getDAV() + "/files/" + userId + pathServerUrl + "/" + metadata.fileName
  88. var addCustomHeaders: [String:String] = [:]
  89. let creationDate = "\(metadata.creationDate.timeIntervalSince1970)"
  90. let modificationDate = "\(metadata.date.timeIntervalSince1970)"
  91. addCustomHeaders["X-OC-CTime"] = creationDate
  92. addCustomHeaders["X-OC-MTime"] = modificationDate
  93. NCCommunication.shared.moveFileOrFolder(serverUrlFileNameSource: serverUrlFileNameSource, serverUrlFileNameDestination: serverUrlFileNameDestination, overwrite: true, addCustomHeaders: addCustomHeaders) { (_, errorCode, errorDescription) in
  94. if errorCode == 0 {
  95. // Delete chunk folder
  96. NCCommunication.shared.deleteFileOrFolder(chunkFolderPath) { (_, _, _) in }
  97. let serverUrl = metadata.serverUrl
  98. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  99. NCManageDatabase.shared.deleteChunks(account: metadata.account, ocId: metadata.ocId)
  100. NCUtilityFileSystem.shared.deleteFile(filePath: directoryProviderStorageOcId)
  101. self.readFile(serverUrlFileName: serverUrlFileNameDestination, account: metadata.account) { (_, metadata, _, _) in
  102. if errorCode == 0, let metadata = metadata {
  103. NCManageDatabase.shared.addMetadata(metadata)
  104. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl":serverUrl])
  105. } else {
  106. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSourceNetworkForced, userInfo: ["serverUrl": serverUrl])
  107. }
  108. }
  109. } else {
  110. self.uploadChunkFileError(metadata: metadata, chunkFolderPath: chunkFolderPath, directoryProviderStorageOcId: directoryProviderStorageOcId, errorCode: errorCode, errorDescription: errorDescription)
  111. }
  112. }
  113. } else {
  114. self.uploadChunkFileError(metadata: metadata, chunkFolderPath: chunkFolderPath, directoryProviderStorageOcId: directoryProviderStorageOcId, errorCode: uploadErrorCode, errorDescription: uploadErrorDescription)
  115. }
  116. }
  117. } else {
  118. self.uploadChunkFileError(metadata: metadata, chunkFolderPath: chunkFolderPath, directoryProviderStorageOcId: directoryProviderStorageOcId, errorCode: errorCode, errorDescription: errorDescription)
  119. }
  120. }
  121. }
  122. private func createChunkedFolder(chunkFolderPath: String, account: String, completion: @escaping (_ errorCode: Int, _ errorDescription: String)->()) {
  123. NCCommunication.shared.readFileOrFolder(serverUrlFileName: chunkFolderPath, depth: "0", showHiddenFiles: CCUtility.getShowHiddenFiles()) { (_, _, _, errorCode, errorDescription) in
  124. if errorCode == 0 {
  125. completion(0, "")
  126. } else if errorCode == NCGlobal.shared.errorResourceNotFound {
  127. NCCommunication.shared.createFolder(chunkFolderPath) { (_, _, _, errorCode, errorDescription) in
  128. completion(errorCode, errorDescription)
  129. }
  130. } else {
  131. completion(errorCode, errorDescription)
  132. }
  133. }
  134. }
  135. private func uploadChunkFileError(metadata: tableMetadata, chunkFolderPath: String, directoryProviderStorageOcId: String, errorCode: Int, errorDescription: String) {
  136. if errorCode == NSURLErrorCancelled || errorCode == NCGlobal.shared.errorRequestExplicityCancelled {
  137. // Delete chunk folder
  138. NCCommunication.shared.deleteFileOrFolder(chunkFolderPath) { (_, _, _) in }
  139. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  140. NCManageDatabase.shared.deleteChunks(account: metadata.account, ocId: metadata.ocId)
  141. NCUtilityFileSystem.shared.deleteFile(filePath: directoryProviderStorageOcId)
  142. NCCommunication.shared.deleteFileOrFolder(chunkFolderPath) { (_, _, _) in }
  143. } else {
  144. NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId, session: nil, sessionError: errorDescription, sessionTaskIdentifier: 0, status: NCGlobal.shared.metadataStatusUploadError)
  145. let description = errorDescription + " code: \(errorCode)"
  146. NCContentPresenter.shared.messageNotification("_error_", description: description, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError, forced: true)
  147. }
  148. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl":metadata.serverUrl])
  149. }
  150. }