NCOperationQueue.swift 7.7 KB

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