NCOperationQueue.swift 6.8 KB

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