NCDragDrop.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. if metadatas.isEmpty {
  83. return nil
  84. } else {
  85. return metadatas
  86. }
  87. }
  88. func uploadFile(url: URL, serverUrl: String, controller: NCMainTabBarController?) {
  89. do {
  90. let data = try Data(contentsOf: url)
  91. Task {
  92. let ocId = NSUUID().uuidString
  93. let session = NCSession.shared.getSession(controller: controller)
  94. let fileName = await NCNetworking.shared.createFileName(fileNameBase: url.lastPathComponent, account: session.account, serverUrl: serverUrl)
  95. let fileNamePath = utilityFileSystem.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)
  96. try data.write(to: URL(fileURLWithPath: fileNamePath))
  97. let metadataForUpload = await database.createMetadata(fileName: fileName,
  98. fileNameView: fileName,
  99. ocId: ocId,
  100. serverUrl: serverUrl,
  101. url: "",
  102. contentType: "",
  103. session: session,
  104. sceneIdentifier: controller?.sceneIdentifier)
  105. metadataForUpload.session = NCNetworking.shared.sessionUploadBackground
  106. metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
  107. metadataForUpload.size = utilityFileSystem.getFileSize(filePath: fileNamePath)
  108. metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
  109. metadataForUpload.sessionDate = Date()
  110. database.addMetadata(metadataForUpload)
  111. }
  112. } catch {
  113. NCContentPresenter().showError(error: NKError(error: error))
  114. return
  115. }
  116. }
  117. func copyFile(metadatas: [tableMetadata], serverUrl: String) {
  118. for metadata in metadatas {
  119. NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrl, overwrite: false)
  120. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterCopyMoveFile, userInfo: ["ocId": [metadata.ocId], "serverUrl": metadata.serverUrl, "account": metadata.account, "dragdrop": true, "type": "copy"])
  121. }
  122. }
  123. func moveFile(metadatas: [tableMetadata], serverUrl: String) {
  124. for metadata in metadatas {
  125. NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrl, overwrite: false)
  126. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterCopyMoveFile, userInfo: ["ocId": [metadata.ocId], "serverUrl": metadata.serverUrl, "account": metadata.account, "dragdrop": true, "type": "move"])
  127. }
  128. }
  129. }
  130. // MARK: -
  131. class DragDropHover {
  132. static let shared = DragDropHover()
  133. var pushTimerIndexPath: Timer?
  134. var pushCollectionView: UICollectionView?
  135. var pushIndexPath: IndexPath?
  136. var sourceMetadatas: [tableMetadata]?
  137. var destinationMetadata: tableMetadata?
  138. func cleanPushDragDropHover() {
  139. pushTimerIndexPath?.invalidate()
  140. pushTimerIndexPath = nil
  141. pushCollectionView = nil
  142. pushIndexPath = nil
  143. }
  144. }