NCDragDrop.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  28. let utilityFileSystem = NCUtilityFileSystem()
  29. func performDrag(metadata: tableMetadata? = nil, selectOcId: [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 selectOcId {
  34. for ocId in selectOcId {
  35. if let metadata = NCManageDatabase.shared.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) -> [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 = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  62. if !isImageVideo {
  63. metadatas.append(metadata)
  64. } else if isImageVideo, (metadata.isImage || metadata.isVideo) {
  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)
  78. }
  79. }
  80. }
  81. }
  82. if metadatas.isEmpty {
  83. return nil
  84. } else {
  85. return metadatas
  86. }
  87. }
  88. func uploadFile(url: URL, serverUrl: String) {
  89. do {
  90. let data = try Data(contentsOf: url)
  91. Task {
  92. let ocId = NSUUID().uuidString
  93. let fileName = await NCNetworking.shared.createFileName(fileNameBase: url.lastPathComponent, account: appDelegate.account, serverUrl: serverUrl)
  94. let fileNamePath = utilityFileSystem.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)
  95. try data.write(to: URL(fileURLWithPath: fileNamePath))
  96. 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: "")
  97. metadataForUpload.session = NCNetworking.shared.sessionUploadBackground
  98. metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
  99. metadataForUpload.size = utilityFileSystem.getFileSize(filePath: fileNamePath)
  100. metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
  101. metadataForUpload.sessionDate = Date()
  102. NCManageDatabase.shared.addMetadata(metadataForUpload)
  103. }
  104. } catch {
  105. NCContentPresenter().showError(error: NKError(error: error))
  106. return
  107. }
  108. }
  109. func copyFile(metadatas: [tableMetadata], serverUrl: String) {
  110. Task {
  111. let error = NKError()
  112. var ocId: [String] = []
  113. for metadata in metadatas where error == .success {
  114. let error = await NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrl, overwrite: false)
  115. if error == .success {
  116. ocId.append(metadata.ocId)
  117. }
  118. }
  119. if error != .success {
  120. NCContentPresenter().showError(error: error)
  121. }
  122. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterCopyFile, userInfo: ["ocId": ocId, "error": error, "dragdrop": true])
  123. }
  124. }
  125. func moveFile(metadatas: [tableMetadata], serverUrl: String) {
  126. Task {
  127. var error = NKError()
  128. var ocId: [String] = []
  129. for metadata in metadatas where error == .success {
  130. error = await NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrl, overwrite: false)
  131. if error == .success {
  132. ocId.append(metadata.ocId)
  133. }
  134. }
  135. if error != .success {
  136. NCContentPresenter().showError(error: error)
  137. }
  138. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMoveFile, userInfo: ["ocId": ocId, "error": error, "dragdrop": true])
  139. }
  140. }
  141. }
  142. // MARK: -
  143. class DragDropHover {
  144. static let shared = DragDropHover()
  145. var pushTimerIndexPath: Timer?
  146. var pushCollectionView: UICollectionView?
  147. var pushIndexPath: IndexPath?
  148. var sourceMetadatas: [tableMetadata]?
  149. var destinationMetadata: tableMetadata?
  150. func cleanPushDragDropHover() {
  151. pushTimerIndexPath?.invalidate()
  152. pushTimerIndexPath = nil
  153. pushCollectionView = nil
  154. pushIndexPath = nil
  155. }
  156. }