NCOperationQueue.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. // Download file
  36. @objc func download(metadata: tableMetadata, selector: String, setFavorite: Bool) {
  37. downloadQueue.addOperation(NCOperationDownload.init(metadata: metadata, selector: selector, setFavorite: setFavorite))
  38. }
  39. @objc func downloadCancelAll() {
  40. downloadQueue.cancelAll()
  41. }
  42. @objc func downloadCount() -> Int {
  43. return downloadQueue.operationCount
  44. }
  45. // Read Folder Synchronize
  46. @objc func readFolderSync(serverUrl: String, selector: String ,account: String) {
  47. readFolderSyncQueue.addOperation(NCOperationReadFolderSync.init(serverUrl: serverUrl, selector: selector, account: account))
  48. }
  49. @objc func readFolderSyncCancelAll() {
  50. readFolderSyncQueue.cancelAll()
  51. }
  52. // Download Thumbnail
  53. @objc func downloadThumbnail(metadata: tableMetadata, activeUrl: String, view: Any, indexPath: IndexPath) {
  54. if metadata.hasPreview && (!CCUtility.fileProviderStorageIconExists(metadata.ocId, fileNameView: metadata.fileName) || metadata.typeFile == k_metadataTypeFile_document) {
  55. for operation in downloadThumbnailQueue.operations {
  56. if (operation as! NCOperationDownloadThumbnail).metadata.ocId == metadata.ocId { return }
  57. }
  58. downloadThumbnailQueue.addOperation(NCOperationDownloadThumbnail.init(metadata: metadata, activeUrl: activeUrl, view: view, indexPath: indexPath))
  59. }
  60. }
  61. @objc func downloadThumbnailCancelAll() {
  62. downloadThumbnailQueue.cancelAll()
  63. }
  64. // Remove deleted file
  65. @objc func removeDeletedFile(metadata: tableMetadata) {
  66. for operation in removeDeletedFileQueue.operations {
  67. if (operation as! NCOperationRemoveDeletedFileQueue).metadata.ocId == metadata.ocId { return }
  68. }
  69. removeDeletedFileQueue.addOperation(NCOperationRemoveDeletedFileQueue.init(metadata: metadata))
  70. }
  71. }
  72. //MARK: -
  73. class NCOperationDownload: ConcurrentOperation {
  74. private var metadata: tableMetadata
  75. private var selector: String
  76. private var setFavorite: Bool
  77. init(metadata: tableMetadata, selector: String, setFavorite: Bool) {
  78. self.metadata = metadata
  79. self.selector = selector
  80. self.setFavorite = setFavorite
  81. }
  82. override func start() {
  83. if isCancelled {
  84. self.finish()
  85. } else {
  86. NCNetworking.shared.download(metadata: self.metadata, selector: self.selector, setFavorite: self.setFavorite) { (_) in
  87. self.finish()
  88. }
  89. }
  90. }
  91. }
  92. //MARK: -
  93. class NCOperationReadFolderSync: ConcurrentOperation {
  94. private var serverUrl: String
  95. private var selector: String
  96. private var account: String
  97. init(serverUrl: String, selector: String, account: String) {
  98. self.serverUrl = serverUrl
  99. self.selector = selector
  100. self.account = account
  101. }
  102. override func start() {
  103. if isCancelled {
  104. self.finish()
  105. } else {
  106. NCCommunication.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: CCUtility.getShowHiddenFiles()) { (account, files, responseData, errorCode, errorDescription) in
  107. if errorCode == 0 && files != nil {
  108. NCManageDatabase.sharedInstance.convertNCCommunicationFilesToMetadatas(files!, useMetadataFolder: true, account: account) { (metadataFolder, metadatasFolder, metadatas) in
  109. if metadatas.count > 0 {
  110. CCSynchronize.shared()?.readFolder(withAccount: account, serverUrl: self.serverUrl, metadataFolder: metadataFolder, metadatas: metadatas, selector: self.selector)
  111. }
  112. }
  113. } else if errorCode == 404 {
  114. NCManageDatabase.sharedInstance.deleteDirectoryAndSubDirectory(serverUrl: self.serverUrl, account: account)
  115. }
  116. self.finish()
  117. }
  118. }
  119. }
  120. }
  121. //MARK: -
  122. class NCOperationDownloadThumbnail: ConcurrentOperation {
  123. var metadata: tableMetadata
  124. var activeUrl: String
  125. var view: Any
  126. var indexPath: IndexPath
  127. init(metadata: tableMetadata, activeUrl: String, view: Any, indexPath: IndexPath) {
  128. self.metadata = metadata
  129. self.activeUrl = activeUrl
  130. self.view = view
  131. self.indexPath = indexPath
  132. }
  133. override func start() {
  134. if isCancelled {
  135. self.finish()
  136. } else {
  137. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: activeUrl)!
  138. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  139. NCCommunication.shared.downloadPreview(fileNamePathOrFileId: fileNamePath, fileNameLocalPath: fileNameLocalPath, width: Int(k_sizePreview), height: Int(k_sizePreview)) { (account, data, errorCode, errorMessage) in
  140. var cell: NCImageCellProtocol?
  141. if self.view is UICollectionView && NCMainCommon.sharedInstance.isValidIndexPath(self.indexPath, view: self.view) {
  142. cell = (self.view as! UICollectionView).cellForItem(at: self.indexPath) as? NCImageCellProtocol
  143. } else if self.view is UITableView && NCMainCommon.sharedInstance.isValidIndexPath(self.indexPath, view: self.view) {
  144. cell = (self.view as! UITableView).cellForRow(at: self.indexPath) as? NCImageCellProtocol
  145. }
  146. if (cell != nil) {
  147. var previewImage: UIImage!
  148. if errorCode == 0 && data != nil {
  149. if let image = UIImage(data: data!) {
  150. previewImage = image
  151. }
  152. } else {
  153. if self.metadata.iconName.count > 0 {
  154. previewImage = UIImage(named: self.metadata.iconName)
  155. } else {
  156. previewImage = UIImage(named: "file")
  157. }
  158. }
  159. cell!.filePreviewImageView.backgroundColor = nil
  160. UIView.transition(with: cell!.filePreviewImageView,
  161. duration: 0.75,
  162. options: .transitionCrossDissolve,
  163. animations: { cell!.filePreviewImageView.image = previewImage! },
  164. completion: nil)
  165. }
  166. self.finish()
  167. }
  168. }
  169. }
  170. }
  171. //MARK: -
  172. class NCOperationRemoveDeletedFileQueue: ConcurrentOperation {
  173. var metadata: tableMetadata
  174. init(metadata: tableMetadata) {
  175. self.metadata = metadata
  176. }
  177. override func start() {
  178. if isCancelled {
  179. self.finish()
  180. } else {
  181. let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
  182. NCCommunication.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0", showHiddenFiles: CCUtility.getShowHiddenFiles()) { (account, files, responseData, errorCode, errorDescription) in
  183. if errorCode == 404 {
  184. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", self.metadata.ocId))
  185. NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_deleteFile), object: nil, userInfo: ["metadata": self.metadata, "errorCode": errorCode])
  186. }
  187. self.finish()
  188. }
  189. }
  190. }
  191. }