NCOperationQueue.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. var downloadQueue = Queuer(name: "downloadQueue", maxConcurrentOperationCount: 5, qualityOfService: .default)
  32. let readFolderSyncQueue = Queuer(name: "readFolderSyncQueue", maxConcurrentOperationCount: 1, qualityOfService: .default)
  33. let downloadThumbnailQueue = Queuer(name: "downloadThumbnailQueue", maxConcurrentOperationCount: 10, qualityOfService: .default)
  34. // Download
  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. //
  45. @objc func readFolderSync(serverUrl: String, selector: String ,account: String) {
  46. readFolderSyncQueue.addOperation(NCOperationReadFolderSync.init(serverUrl: serverUrl, selector: selector, account: account))
  47. }
  48. //
  49. @objc func downloadThumbnail(metadata: tableMetadata, activeUrl: String, view: Any, indexPath: IndexPath) {
  50. if metadata.hasPreview && (!CCUtility.fileProviderStorageIconExists(metadata.ocId, fileNameView: metadata.fileName) || metadata.typeFile == k_metadataTypeFile_document) {
  51. downloadThumbnailQueue.addOperation(NCOperationDownloadThumbnail.init(metadata: metadata, activeUrl: activeUrl, view: view, indexPath: indexPath))
  52. }
  53. }
  54. }
  55. //MARK: -
  56. class NCOperationDownload: ConcurrentOperation {
  57. private var metadata: tableMetadata
  58. private var selector: String
  59. private var setFavorite: Bool
  60. init(metadata: tableMetadata, selector: String, setFavorite: Bool) {
  61. self.metadata = metadata
  62. self.selector = selector
  63. self.setFavorite = setFavorite
  64. }
  65. override func start() {
  66. if isCancelled {
  67. self.finish()
  68. } else {
  69. NCNetworking.shared.download(metadata: self.metadata, selector: self.selector, setFavorite: self.setFavorite) { (_) in
  70. self.finish()
  71. }
  72. }
  73. }
  74. }
  75. //MARK: -
  76. class NCOperationReadFolderSync: ConcurrentOperation {
  77. private var serverUrl: String
  78. private var selector: String
  79. private var account: String
  80. init(serverUrl: String, selector: String, account: String) {
  81. self.serverUrl = serverUrl
  82. self.selector = selector
  83. self.account = account
  84. }
  85. override func start() {
  86. NCCommunication.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: CCUtility.getShowHiddenFiles()) { (account, files, errorCode, errorDescription) in
  87. if errorCode == 0 && files != nil {
  88. NCManageDatabase.sharedInstance.convertNCCommunicationFilesToMetadatas(files!, useMetadataFolder: true, account: account) { (metadataFolder, metadatasFolder, metadatas) in
  89. if metadatas.count > 0 {
  90. CCSynchronize.shared()?.readFolder(withAccount: account, serverUrl: self.serverUrl, metadataFolder: metadataFolder, metadatas: metadatas, selector: self.selector)
  91. }
  92. }
  93. } else if errorCode == 404 {
  94. NCManageDatabase.sharedInstance.deleteDirectoryAndSubDirectory(serverUrl: self.serverUrl, account: account)
  95. }
  96. self.finish()
  97. }
  98. }
  99. }
  100. //MARK: -
  101. class NCOperationDownloadThumbnail: ConcurrentOperation {
  102. private var metadata: tableMetadata
  103. private var activeUrl: String
  104. private var view: Any
  105. private var indexPath: IndexPath
  106. init(metadata: tableMetadata, activeUrl: String, view: Any, indexPath: IndexPath) {
  107. self.metadata = metadata
  108. self.activeUrl = activeUrl
  109. self.view = view
  110. self.indexPath = indexPath
  111. }
  112. override func start() {
  113. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: activeUrl)!
  114. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  115. NCCommunication.shared.downloadPreview(fileNamePathOrFileId: fileNamePath, fileNameLocalPath: fileNameLocalPath, width: Int(k_sizePreview), height: Int(k_sizePreview)) { (account, data, errorCode, errorMessage) in
  116. if errorCode == 0 && data != nil {
  117. if let image = UIImage.init(data: data!) {
  118. if self.view is UICollectionView && NCMainCommon.sharedInstance.isValidIndexPath(self.indexPath, view: self.view) {
  119. if let cell = (self.view as! UICollectionView).cellForItem(at: self.indexPath) {
  120. if cell is NCListCell {
  121. (cell as! NCListCell).imageItem.image = image
  122. } else if cell is NCGridCell {
  123. (cell as! NCGridCell).imageItem.image = image
  124. } else if cell is NCGridMediaCell {
  125. (cell as! NCGridMediaCell).imageItem.image = image
  126. }
  127. }
  128. }
  129. if self.view is UITableView && CCUtility.fileProviderStorageIconExists(self.metadata.ocId, fileNameView: self.metadata.fileName) && NCMainCommon.sharedInstance.isValidIndexPath(self.indexPath, view: self.view) {
  130. if let cell = (self.view as! UITableView).cellForRow(at: self.indexPath) {
  131. if cell is CCCellMainTransfer {
  132. (cell as! CCCellMainTransfer).file.image = image
  133. } else if cell is CCCellMain {
  134. (cell as! CCCellMain).file.image = image
  135. }
  136. }
  137. }
  138. }
  139. }
  140. self.finish()
  141. }
  142. }
  143. }