NCDragDrop.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 NextcloudKit
  25. class NCDragDrop: NSObject {
  26. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  27. let utilityFileSystem = NCUtilityFileSystem()
  28. func performDrag(metadata: tableMetadata? = nil, selectOcId: [String]? = nil) -> [UIDragItem] {
  29. var metadatas: [tableMetadata] = []
  30. if let metadata, metadata.status == 0, !metadata.isDirectoryE2EE, !metadata.e2eEncrypted {
  31. metadatas.append(metadata)
  32. } else if let selectOcId {
  33. for ocId in selectOcId {
  34. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), metadata.status == 0, !metadata.isDirectoryE2EE, !metadata.e2eEncrypted {
  35. metadatas.append(metadata)
  36. }
  37. }
  38. }
  39. let dragItems = metadatas.map { metadata in
  40. let itemProvider = NSItemProvider()
  41. itemProvider.registerDataRepresentation(forTypeIdentifier: NCGlobal.shared.metadataOcIdDataRepresentation, visibility: .all) { completion in
  42. let data = metadata.ocId.data(using: .utf8)
  43. completion(data, nil)
  44. return nil
  45. }
  46. return UIDragItem(itemProvider: itemProvider)
  47. }
  48. return dragItems
  49. }
  50. func performDrop(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator, serverUrl: String, isImageVideo: Bool) -> [tableMetadata]? {
  51. var serverUrl = serverUrl
  52. var metadatas: [tableMetadata] = []
  53. DragDropHover.shared.cleanPushDragDropHover()
  54. DragDropHover.shared.sourceMetadatas = nil
  55. for item in coordinator.session.items {
  56. if item.itemProvider.hasItemConformingToTypeIdentifier(NCGlobal.shared.metadataOcIdDataRepresentation) {
  57. let semaphore = DispatchSemaphore(value: 0)
  58. item.itemProvider.loadDataRepresentation(forTypeIdentifier: NCGlobal.shared.metadataOcIdDataRepresentation) { data, error in
  59. if error == nil, let data, let ocId = String(data: data, encoding: .utf8),
  60. let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  61. if !isImageVideo {
  62. metadatas.append(metadata)
  63. } else if isImageVideo, (metadata.isImage || metadata.isVideo) {
  64. metadatas.append(metadata)
  65. }
  66. }
  67. semaphore.signal()
  68. }
  69. semaphore.wait()
  70. } else {
  71. item.itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.data.identifier) { url, error in
  72. if error == nil, let url = url {
  73. if let destinationMetadata = DragDropHover.shared.destinationMetadata, destinationMetadata.directory {
  74. serverUrl = destinationMetadata.serverUrl + "/" + destinationMetadata.fileName
  75. }
  76. self.uploadFile(url: url, serverUrl: serverUrl)
  77. }
  78. }
  79. }
  80. }
  81. if metadatas.isEmpty {
  82. return nil
  83. } else {
  84. return metadatas
  85. }
  86. }
  87. func uploadFile(url: URL, serverUrl: String) {
  88. do {
  89. let data = try Data(contentsOf: url)
  90. Task {
  91. let ocId = NSUUID().uuidString
  92. let fileName = await NCNetworking.shared.createFileName(fileNameBase: url.lastPathComponent, account: appDelegate.account, serverUrl: serverUrl)
  93. let fileNamePath = utilityFileSystem.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)
  94. try data.write(to: URL(fileURLWithPath: fileNamePath))
  95. let metadataForUpload = await NCManageDatabase.shared.createMetadata(account: appDelegate.account, user: appDelegate.user, userId: appDelegate.userId, fileName: fileName, fileNameView: fileName, ocId: ocId, serverUrl: serverUrl, urlBase: self.appDelegate.urlBase, url: "", contentType: "")
  96. metadataForUpload.session = NCNetworking.shared.sessionUploadBackground
  97. metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
  98. metadataForUpload.size = utilityFileSystem.getFileSize(filePath: fileNamePath)
  99. metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
  100. metadataForUpload.sessionDate = Date()
  101. NCManageDatabase.shared.addMetadata(metadataForUpload)
  102. }
  103. } catch {
  104. NCContentPresenter().showError(error: NKError(error: error))
  105. return
  106. }
  107. }
  108. func copyFile(metadatas: [tableMetadata], serverUrl: String) {
  109. Task {
  110. let error = NKError()
  111. var ocId: [String] = []
  112. for metadata in metadatas where error == .success {
  113. let error = await NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrl, overwrite: false)
  114. if error == .success {
  115. ocId.append(metadata.ocId)
  116. }
  117. }
  118. if error != .success {
  119. NCContentPresenter().showError(error: error)
  120. }
  121. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterCopyFile, userInfo: ["ocId": ocId, "error": error, "dragdrop": true])
  122. }
  123. }
  124. func moveFile(metadatas: [tableMetadata], serverUrl: String) {
  125. Task {
  126. var error = NKError()
  127. var ocId: [String] = []
  128. for metadata in metadatas where error == .success {
  129. error = await NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrl, overwrite: false)
  130. if error == .success {
  131. ocId.append(metadata.ocId)
  132. }
  133. }
  134. if error != .success {
  135. NCContentPresenter().showError(error: error)
  136. }
  137. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMoveFile, userInfo: ["ocId": ocId, "error": error, "dragdrop": true])
  138. }
  139. }
  140. }
  141. // MARK: -
  142. class DragDropHover {
  143. static let shared = DragDropHover()
  144. var pushTimerIndexPath: Timer?
  145. var pushCollectionView: UICollectionView?
  146. var pushIndexPath: IndexPath?
  147. var sourceMetadatas: [tableMetadata]?
  148. var destinationMetadata: tableMetadata?
  149. func cleanPushDragDropHover() {
  150. pushTimerIndexPath?.invalidate()
  151. pushTimerIndexPath = nil
  152. pushCollectionView = nil
  153. pushIndexPath = nil
  154. }
  155. }