NCOperationQueue.swift 7.5 KB

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