NCDragDrop.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // NCDragDrop.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 27/04/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 UniformTypeIdentifiers
  25. import NextcloudKit
  26. class NCDragDrop: NSObject {
  27. let utilityFileSystem = NCUtilityFileSystem()
  28. let database = NCManageDatabase.shared
  29. func performDrag(metadata: tableMetadata? = nil, fileSelect: [String]? = nil) -> [UIDragItem] {
  30. var metadatas: [tableMetadata] = []
  31. if let metadata, metadata.status == 0, !metadata.isDirectoryE2EE, !metadata.e2eEncrypted {
  32. metadatas.append(metadata)
  33. } else if let fileSelect {
  34. for ocId in fileSelect {
  35. if let metadata = database.getMetadataFromOcId(ocId), metadata.status == 0, !metadata.isDirectoryE2EE, !metadata.e2eEncrypted {
  36. metadatas.append(metadata)
  37. }
  38. }
  39. }
  40. let dragItems = metadatas.map { metadata in
  41. let itemProvider = NSItemProvider()
  42. itemProvider.registerDataRepresentation(forTypeIdentifier: NCGlobal.shared.metadataOcIdDataRepresentation, visibility: .all) { completion in
  43. let data = metadata.ocId.data(using: .utf8)
  44. completion(data, nil)
  45. return nil
  46. }
  47. return UIDragItem(itemProvider: itemProvider)
  48. }
  49. return dragItems
  50. }
  51. func performDrop(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator, serverUrl: String, isImageVideo: Bool, controller: NCMainTabBarController?) -> [tableMetadata]? {
  52. var serverUrl = serverUrl
  53. var metadatas: [tableMetadata] = []
  54. DragDropHover.shared.cleanPushDragDropHover()
  55. DragDropHover.shared.sourceMetadatas = nil
  56. for item in coordinator.session.items {
  57. if item.itemProvider.hasItemConformingToTypeIdentifier(NCGlobal.shared.metadataOcIdDataRepresentation) {
  58. let semaphore = DispatchSemaphore(value: 0)
  59. item.itemProvider.loadDataRepresentation(forTypeIdentifier: NCGlobal.shared.metadataOcIdDataRepresentation) { data, error in
  60. if error == nil, let data, let ocId = String(data: data, encoding: .utf8),
  61. let metadata = self.database.getMetadataFromOcId(ocId) {
  62. if !isImageVideo {
  63. metadatas.append(metadata)
  64. } else if isImageVideo, (metadata.isImageOrVideo) {
  65. metadatas.append(metadata)
  66. }
  67. }
  68. semaphore.signal()
  69. }
  70. semaphore.wait()
  71. } else {
  72. item.itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.data.identifier) { url, error in
  73. if error == nil, let url = url {
  74. if let destinationMetadata = DragDropHover.shared.destinationMetadata, destinationMetadata.directory {
  75. serverUrl = destinationMetadata.serverUrl + "/" + destinationMetadata.fileName
  76. }
  77. self.uploadFile(url: url, serverUrl: serverUrl, controller: controller)
  78. }
  79. }
  80. }
  81. }
  82. var invalidNameIndexes: [Int] = []
  83. let session = NCSession.shared.getSession(controller: controller)
  84. for (index, metadata) in metadatas.enumerated() {
  85. if let fileNameError = FileNameValidator.shared.checkFileName(metadata.fileName, account: session.account) {
  86. if metadatas.count == 1 {
  87. let alert = UIAlertController.renameFile(metadata: metadata) { newFileName in
  88. metadatas[index].fileName = newFileName
  89. metadatas[index].fileNameView = newFileName
  90. }
  91. controller?.present(alert, animated: true)
  92. return nil
  93. } else {
  94. controller?.present(UIAlertController.warning(message: "\(fileNameError.errorDescription) \(NSLocalizedString("_please_rename_file_", comment: ""))"), animated: true)
  95. invalidNameIndexes.append(index)
  96. }
  97. }
  98. }
  99. for index in invalidNameIndexes.reversed() {
  100. metadatas.remove(at: index)
  101. }
  102. if metadatas.isEmpty {
  103. return nil
  104. } else {
  105. return metadatas
  106. }
  107. }
  108. func uploadFile(url: URL, serverUrl: String, controller: NCMainTabBarController?) {
  109. do {
  110. let data = try Data(contentsOf: url)
  111. Task {
  112. let ocId = NSUUID().uuidString
  113. let session = NCSession.shared.getSession(controller: controller)
  114. let newFileName = FileAutoRenamer.shared.rename(url.lastPathComponent, account: session.account)
  115. let fileNamePath = utilityFileSystem.getDirectoryProviderStorageOcId(ocId, fileNameView: newFileName)
  116. if let fileNameError = FileNameValidator.shared.checkFileName(newFileName, account: session.account) {
  117. await controller?.present(UIAlertController.warning(message: "\(fileNameError.errorDescription) \(NSLocalizedString("_please_rename_file_", comment: ""))"), animated: true)
  118. return
  119. }
  120. let fileName = await NCNetworking.shared.createFileName(fileNameBase: newFileName, account: session.account, serverUrl: serverUrl)
  121. try data.write(to: URL(fileURLWithPath: fileNamePath))
  122. let metadataForUpload = await database.createMetadata(fileName: fileName,
  123. fileNameView: fileName,
  124. ocId: ocId,
  125. serverUrl: serverUrl,
  126. url: "",
  127. contentType: "",
  128. session: session,
  129. sceneIdentifier: controller?.sceneIdentifier)
  130. metadataForUpload.session = NCNetworking.shared.sessionUploadBackground
  131. metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
  132. metadataForUpload.size = utilityFileSystem.getFileSize(filePath: fileNamePath)
  133. metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
  134. metadataForUpload.sessionDate = Date()
  135. database.addMetadata(metadataForUpload)
  136. }
  137. } catch {
  138. NCContentPresenter().showError(error: NKError(error: error))
  139. return
  140. }
  141. }
  142. func copyFile(metadatas: [tableMetadata], serverUrl: String) {
  143. for metadata in metadatas {
  144. NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrl, overwrite: false)
  145. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterCopyMoveFile, userInfo: ["ocId": [metadata.ocId], "serverUrl": metadata.serverUrl, "account": metadata.account, "dragdrop": true, "type": "copy"])
  146. }
  147. }
  148. func moveFile(metadatas: [tableMetadata], serverUrl: String) {
  149. for metadata in metadatas {
  150. NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrl, overwrite: false)
  151. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterCopyMoveFile, userInfo: ["ocId": [metadata.ocId], "serverUrl": metadata.serverUrl, "account": metadata.account, "dragdrop": true, "type": "move"])
  152. }
  153. }
  154. }
  155. // MARK: -
  156. class DragDropHover {
  157. static let shared = DragDropHover()
  158. var pushTimerIndexPath: Timer?
  159. var pushCollectionView: UICollectionView?
  160. var pushIndexPath: IndexPath?
  161. var sourceMetadatas: [tableMetadata]?
  162. var destinationMetadata: tableMetadata?
  163. func cleanPushDragDropHover() {
  164. pushTimerIndexPath?.invalidate()
  165. pushTimerIndexPath = nil
  166. pushCollectionView = nil
  167. pushIndexPath = nil
  168. }
  169. }