NCOperationQueue.swift 10 KB

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