NCOperationQueue.swift 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // NCOperationQueue.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/06/2020.
  6. // Copyright © 2020 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 Queuer
  25. import NCCommunication
  26. @objc class NCOperationQueue: NSObject {
  27. @objc public static let shared: NCOperationQueue = {
  28. let instance = NCOperationQueue()
  29. return instance
  30. }()
  31. private var downloadQueue = Queuer(name: "downloadQueue", maxConcurrentOperationCount: 5, qualityOfService: .default)
  32. private let readFolderSyncQueue = Queuer(name: "readFolderSyncQueue", maxConcurrentOperationCount: 1, qualityOfService: .default)
  33. private let downloadThumbnailQueue = Queuer(name: "downloadThumbnailQueue", maxConcurrentOperationCount: 10, qualityOfService: .default)
  34. private let removeDeletedFileQueue = Queuer(name: "removeDeletedFileQueue", maxConcurrentOperationCount: 10, qualityOfService: .default)
  35. @objc func cancelAllQueue() {
  36. downloadCancelAll()
  37. readFolderSyncCancelAll()
  38. downloadThumbnailCancelAll()
  39. removeDeletedFileCancelAll()
  40. }
  41. // Download file
  42. @objc func download(metadata: tableMetadata, selector: String, setFavorite: Bool) {
  43. downloadQueue.addOperation(NCOperationDownload.init(metadata: metadata, selector: selector, setFavorite: setFavorite))
  44. }
  45. @objc func downloadCancelAll() {
  46. downloadQueue.cancelAll()
  47. }
  48. @objc func downloadCount() -> Int {
  49. return downloadQueue.operationCount
  50. }
  51. // Read Folder Synchronize
  52. @objc func readFolderSync(serverUrl: String, selector: String ,account: String) {
  53. readFolderSyncQueue.addOperation(NCOperationReadFolderSync.init(serverUrl: serverUrl, selector: selector, account: account))
  54. }
  55. @objc func readFolderSyncCancelAll() {
  56. readFolderSyncQueue.cancelAll()
  57. }
  58. // Download Thumbnail
  59. @objc func downloadThumbnail(metadata: tableMetadata, activeUrl: String, view: Any, indexPath: IndexPath) {
  60. if metadata.hasPreview && (!CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, fileNameView: metadata.fileName) || metadata.typeFile == k_metadataTypeFile_document) {
  61. for operation in downloadThumbnailQueue.operations {
  62. if (operation as! NCOperationDownloadThumbnail).metadata.ocId == metadata.ocId { return }
  63. }
  64. downloadThumbnailQueue.addOperation(NCOperationDownloadThumbnail.init(metadata: metadata, activeUrl: activeUrl, view: view, indexPath: indexPath))
  65. }
  66. }
  67. @objc func downloadThumbnailCancelAll() {
  68. downloadThumbnailQueue.cancelAll()
  69. }
  70. // Remove deleted file
  71. @objc func removeDeletedFile(metadata: tableMetadata) {
  72. for operation in removeDeletedFileQueue.operations {
  73. if (operation as! NCOperationRemoveDeletedFileQueue).metadata.ocId == metadata.ocId { return }
  74. }
  75. removeDeletedFileQueue.addOperation(NCOperationRemoveDeletedFileQueue.init(metadata: metadata))
  76. }
  77. @objc func removeDeletedFileCancelAll() {
  78. removeDeletedFileQueue.cancelAll()
  79. }
  80. }
  81. //MARK: -
  82. class NCOperationDownload: ConcurrentOperation {
  83. private var metadata: tableMetadata
  84. private var selector: String
  85. private var setFavorite: Bool
  86. init(metadata: tableMetadata, selector: String, setFavorite: Bool) {
  87. self.metadata = metadata
  88. self.selector = selector
  89. self.setFavorite = setFavorite
  90. }
  91. override func start() {
  92. if isCancelled {
  93. self.finish()
  94. } else {
  95. NCNetworking.shared.download(metadata: self.metadata, selector: self.selector, setFavorite: self.setFavorite) { (_) in
  96. self.finish()
  97. }
  98. }
  99. }
  100. }
  101. //MARK: -
  102. class NCOperationReadFolderSync: ConcurrentOperation {
  103. private var serverUrl: String
  104. private var selector: String
  105. private var account: String
  106. init(serverUrl: String, selector: String, account: String) {
  107. self.serverUrl = serverUrl
  108. self.selector = selector
  109. self.account = account
  110. }
  111. override func start() {
  112. if isCancelled {
  113. self.finish()
  114. } else {
  115. NCCommunication.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: CCUtility.getShowHiddenFiles()) { (account, files, responseData, errorCode, errorDescription) in
  116. if errorCode == 0 && files != nil {
  117. NCManageDatabase.sharedInstance.convertNCCommunicationFilesToMetadatas(files!, useMetadataFolder: true, account: account) { (metadataFolder, metadatasFolder, metadatas) in
  118. if metadatas.count > 0 {
  119. CCSynchronize.shared()?.readFolder(withAccount: account, serverUrl: self.serverUrl, metadataFolder: metadataFolder, metadatas: metadatas, selector: self.selector)
  120. }
  121. }
  122. } else if errorCode == 404 {
  123. NCManageDatabase.sharedInstance.deleteDirectoryAndSubDirectory(serverUrl: self.serverUrl, account: account)
  124. }
  125. self.finish()
  126. }
  127. }
  128. }
  129. }
  130. //MARK: -
  131. class NCOperationDownloadThumbnail: ConcurrentOperation {
  132. var metadata: tableMetadata
  133. var activeUrl: String
  134. var view: Any
  135. var indexPath: IndexPath
  136. init(metadata: tableMetadata, activeUrl: String, view: Any, indexPath: IndexPath) {
  137. self.metadata = metadata
  138. self.activeUrl = activeUrl
  139. self.view = view
  140. self.indexPath = indexPath
  141. }
  142. override func start() {
  143. if isCancelled {
  144. self.finish()
  145. } else {
  146. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: activeUrl)!
  147. let fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  148. let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  149. NCCommunication.shared.downloadPreview(fileNamePathOrFileId: fileNamePath, fileNamePreviewLocalPath: fileNamePreviewLocalPath , widthPreview: Int(k_sizePreview), heightPreview: Int(k_sizePreview), fileNameIconLocalPath: fileNameIconLocalPath, sizeIcon: Int(k_sizeIcon)) { (account, imagePreview, imageIcon, errorCode, errorDescription) in
  150. var cell: NCImageCellProtocol?
  151. if self.view is UICollectionView && NCMainCommon.sharedInstance.isValidIndexPath(self.indexPath, view: self.view) {
  152. cell = (self.view as! UICollectionView).cellForItem(at: self.indexPath) as? NCImageCellProtocol
  153. } else if self.view is UITableView && NCMainCommon.sharedInstance.isValidIndexPath(self.indexPath, view: self.view) {
  154. cell = (self.view as! UITableView).cellForRow(at: self.indexPath) as? NCImageCellProtocol
  155. }
  156. if (cell != nil) {
  157. var previewImage: UIImage!
  158. if errorCode == 0 && imageIcon != nil {
  159. previewImage = imageIcon
  160. } else {
  161. if self.metadata.iconName.count > 0 {
  162. previewImage = UIImage(named: self.metadata.iconName)
  163. } else {
  164. previewImage = UIImage(named: "file")
  165. }
  166. }
  167. cell!.filePreviewImageView.backgroundColor = nil
  168. UIView.transition(with: cell!.filePreviewImageView,
  169. duration: 0.75,
  170. options: .transitionCrossDissolve,
  171. animations: { cell!.filePreviewImageView.image = previewImage! },
  172. completion: nil)
  173. }
  174. self.finish()
  175. }
  176. }
  177. }
  178. }
  179. //MARK: -
  180. class NCOperationRemoveDeletedFileQueue: ConcurrentOperation {
  181. var metadata: tableMetadata
  182. init(metadata: tableMetadata) {
  183. self.metadata = metadata
  184. }
  185. override func start() {
  186. if isCancelled {
  187. self.finish()
  188. } else {
  189. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
  190. let requestBody =
  191. """
  192. <?xml version=\"1.0\" encoding=\"UTF-8\"?>
  193. <d:propfind xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">
  194. <d:prop>
  195. </d:prop>
  196. </d:propfind>
  197. """
  198. NCCommunication.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0", requestBody: requestBody.data(using: .utf8)) { (account, files, responseData, errorCode, errorDescription) in
  199. if errorCode == 404 {
  200. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", self.metadata.ocId))
  201. NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_deleteFile), object: nil, userInfo: ["metadata": self.metadata, "errorCode": errorCode])
  202. }
  203. self.finish()
  204. }
  205. }
  206. }
  207. }