NCOperationQueue.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 UIKit
  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 deleteQueue = Queuer(name: "deleteQueue", maxConcurrentOperationCount: 1, qualityOfService: .default)
  33. private let copyMoveQueue = Queuer(name: "copyMoveQueue", maxConcurrentOperationCount: 1, qualityOfService: .default)
  34. private let synchronizationQueue = Queuer(name: "synchronizationQueue", maxConcurrentOperationCount: 1, qualityOfService: .default)
  35. private let downloadThumbnailQueue = Queuer(name: "downloadThumbnailQueue", maxConcurrentOperationCount: 10, qualityOfService: .default)
  36. private let downloadAvatarQueue = Queuer(name: "downloadAvatarQueue", maxConcurrentOperationCount: 10, qualityOfService: .default)
  37. private var timerReadFileForMediaQueue: Timer?
  38. @objc func cancelAllQueue() {
  39. downloadCancelAll()
  40. deleteCancelAll()
  41. copyMoveCancelAll()
  42. synchronizationCancelAll()
  43. downloadThumbnailCancelAll()
  44. downloadAvatarCancelAll()
  45. }
  46. // Download file
  47. func download(metadata: tableMetadata, selector: String) {
  48. for operation in downloadQueue.operations as! [NCOperationDownload] {
  49. if operation.metadata.ocId == metadata.ocId {
  50. return
  51. }
  52. }
  53. downloadQueue.addOperation(NCOperationDownload.init(metadata: metadata, selector: selector))
  54. }
  55. @objc func downloadCancelAll() {
  56. downloadQueue.cancelAll()
  57. }
  58. @objc func downloadCount() -> Int {
  59. return downloadQueue.operationCount
  60. }
  61. @objc func downloadExists(metadata: tableMetadata) -> Bool {
  62. for operation in downloadQueue.operations as! [NCOperationDownload] {
  63. if operation.metadata.ocId == metadata.ocId {
  64. return true
  65. }
  66. }
  67. return false
  68. }
  69. // Delete file
  70. @objc func delete(metadata: tableMetadata, onlyLocal: Bool) {
  71. for operation in deleteQueue.operations as! [NCOperationDelete] {
  72. if operation.metadata.ocId == metadata.ocId {
  73. return
  74. }
  75. }
  76. deleteQueue.addOperation(NCOperationDelete.init(metadata: metadata, onlyLocal: onlyLocal))
  77. }
  78. @objc func deleteCancelAll() {
  79. deleteQueue.cancelAll()
  80. }
  81. @objc func deleteCount() -> Int {
  82. return deleteQueue.operationCount
  83. }
  84. // Copy Move file
  85. @objc func copyMove(metadata: tableMetadata, serverUrl: String, overwrite: Bool, move: Bool) {
  86. for operation in copyMoveQueue.operations as! [NCOperationCopyMove] {
  87. if operation.metadata.ocId == metadata.ocId {
  88. return
  89. }
  90. }
  91. copyMoveQueue.addOperation(NCOperationCopyMove.init(metadata: metadata, serverUrlTo: serverUrl, overwrite: overwrite, move: move))
  92. }
  93. @objc func copyMoveCancelAll() {
  94. copyMoveQueue.cancelAll()
  95. }
  96. @objc func copyMoveCount() -> Int {
  97. return copyMoveQueue.operationCount
  98. }
  99. // Synchronization
  100. @objc func synchronizationMetadata(_ metadata: tableMetadata, selector: String) {
  101. for operation in synchronizationQueue.operations as! [NCOperationSynchronization] {
  102. if operation.metadata.ocId == metadata.ocId {
  103. return
  104. }
  105. }
  106. synchronizationQueue.addOperation(NCOperationSynchronization.init(metadata: metadata, selector: selector))
  107. }
  108. @objc func synchronizationCancelAll() {
  109. synchronizationQueue.cancelAll()
  110. }
  111. // Download Thumbnail
  112. @objc func downloadThumbnail(metadata: tableMetadata, placeholder: Bool, cell: UIView, view: UIView?) {
  113. let cell: NCCellProtocol = cell as! NCCellProtocol
  114. if placeholder {
  115. if metadata.iconName.count > 0 {
  116. cell.filePreviewImageView?.image = UIImage.init(named: metadata.iconName)
  117. } else {
  118. cell.filePreviewImageView?.image = NCBrandColor.cacheImages.file
  119. }
  120. }
  121. if metadata.hasPreview && metadata.status == NCGlobal.shared.metadataStatusNormal && (!CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag)) {
  122. for operation in downloadThumbnailQueue.operations as! [NCOperationDownloadThumbnail] {
  123. if operation.metadata.ocId == metadata.ocId {
  124. return
  125. }
  126. }
  127. downloadThumbnailQueue.addOperation(NCOperationDownloadThumbnail.init(metadata: metadata, cell: cell, view: view))
  128. }
  129. }
  130. func cancelDownloadThumbnail(metadata: tableMetadata) {
  131. for operation in downloadThumbnailQueue.operations as! [NCOperationDownloadThumbnail] {
  132. if operation.metadata.ocId == metadata.ocId {
  133. operation.cancel()
  134. }
  135. }
  136. }
  137. @objc func downloadThumbnailCancelAll() {
  138. downloadThumbnailQueue.cancelAll()
  139. }
  140. // Download Avatar
  141. func downloadAvatar(user: String, fileName: String, placeholder: UIImage?, cell: UIView, view: UIView?) {
  142. let cell: NCCellProtocol = cell as! NCCellProtocol
  143. let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
  144. #if !EXTENSION
  145. if let image = (UIApplication.shared.delegate as! AppDelegate).avatars[user] {
  146. cell.fileAvatarImageView?.image = image
  147. return
  148. }
  149. #endif
  150. cell.fileAvatarImageView?.image = placeholder
  151. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  152. cell.fileAvatarImageView?.image = NCUtility.shared.createAvatar(image: image, size: 30)
  153. }
  154. downloadAvatarQueue.addOperation(NCOperationDownloadAvatar.init(user: user, fileName: fileName, fileNameLocalPath: fileNameLocalPath, cell: cell, view: view))
  155. }
  156. func cancelDownloadAvatar(user: String) {
  157. for operation in downloadAvatarQueue.operations as! [NCOperationDownloadAvatar] {
  158. if operation.user == user {
  159. operation.cancel()
  160. }
  161. }
  162. }
  163. @objc func downloadAvatarCancelAll() {
  164. downloadAvatarQueue.cancelAll()
  165. }
  166. }
  167. //MARK: -
  168. class NCOperationDownload: ConcurrentOperation {
  169. var metadata: tableMetadata
  170. var selector: String
  171. init(metadata: tableMetadata, selector: String) {
  172. self.metadata = tableMetadata.init(value: metadata)
  173. self.selector = selector
  174. }
  175. override func start() {
  176. if isCancelled {
  177. self.finish()
  178. } else {
  179. NCNetworking.shared.download(metadata: metadata, selector: self.selector) { (_) in
  180. self.finish()
  181. }
  182. }
  183. }
  184. }
  185. //MARK: -
  186. class NCOperationDelete: ConcurrentOperation {
  187. var metadata: tableMetadata
  188. var onlyLocal: Bool
  189. init(metadata: tableMetadata, onlyLocal: Bool) {
  190. self.metadata = tableMetadata.init(value: metadata)
  191. self.onlyLocal = onlyLocal
  192. }
  193. override func start() {
  194. if isCancelled {
  195. self.finish()
  196. } else {
  197. NCNetworking.shared.deleteMetadata(metadata, onlyLocal: onlyLocal) { (errorCode, errorDescription) in
  198. if errorCode != 0 {
  199. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  200. }
  201. self.finish()
  202. }
  203. }
  204. }
  205. }
  206. //MARK: -
  207. class NCOperationCopyMove: ConcurrentOperation {
  208. var metadata: tableMetadata
  209. var serverUrlTo: String
  210. var overwrite: Bool
  211. var move: Bool
  212. init(metadata: tableMetadata, serverUrlTo: String, overwrite: Bool, move: Bool) {
  213. self.metadata = tableMetadata.init(value: metadata)
  214. self.serverUrlTo = serverUrlTo
  215. self.overwrite = overwrite
  216. self.move = move
  217. }
  218. override func start() {
  219. if isCancelled {
  220. self.finish()
  221. } else {
  222. if move {
  223. NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrlTo, overwrite: overwrite) { (errorCode, errorDescription) in
  224. if errorCode != 0 {
  225. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  226. }
  227. self.finish()
  228. }
  229. } else {
  230. NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrlTo, overwrite: overwrite) { (errorCode, errorDescription) in
  231. if errorCode != 0 {
  232. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  233. }
  234. self.finish()
  235. }
  236. }
  237. }
  238. }
  239. }
  240. //MARK: -
  241. class NCOperationSynchronization: ConcurrentOperation {
  242. var metadata: tableMetadata
  243. var selector: String
  244. var download: Bool
  245. init(metadata: tableMetadata, selector: String) {
  246. self.metadata = tableMetadata.init(value: metadata)
  247. self.selector = selector
  248. if selector == NCGlobal.shared.selectorDownloadFile || selector == NCGlobal.shared.selectorDownloadAllFile {
  249. self.download = true
  250. } else {
  251. self.download = false
  252. }
  253. }
  254. override func start() {
  255. if isCancelled {
  256. self.finish()
  257. } else {
  258. if metadata.directory {
  259. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  260. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, serverUrl))
  261. NCCommunication.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "0", showHiddenFiles: CCUtility.getShowHiddenFiles()) { (account, files, responseData, errorCode, errorDescription) in
  262. if (errorCode == 0) && (directory?.etag != files.first?.etag || self.selector == NCGlobal.shared.selectorDownloadAllFile) {
  263. NCCommunication.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: CCUtility.getShowHiddenFiles()) { (account, files, responseData, errorCode, errorDescription) in
  264. if errorCode == 0 {
  265. NCManageDatabase.shared.convertNCCommunicationFilesToMetadatas(files, useMetadataFolder: true, account: account) { (metadataFolder, metadatasFolder, metadatas) in
  266. let metadatasResult = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND status == %d", account, serverUrl, NCGlobal.shared.metadataStatusNormal))
  267. if self.selector == NCGlobal.shared.selectorDownloadAllFile {
  268. NCManageDatabase.shared.updateMetadatas(metadatas, metadatasResult: metadatasResult)
  269. for metadata in metadatas {
  270. if metadata.directory {
  271. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: self.selector)
  272. } else {
  273. if NCManageDatabase.shared.isDownloadMetadata(metadata, download: true) {
  274. NCOperationQueue.shared.download(metadata: metadata, selector: self.selector)
  275. }
  276. }
  277. }
  278. } else {
  279. let metadatasChanged = NCManageDatabase.shared.updateMetadatas(metadatas, metadatasResult: metadatasResult, addExistsInLocal: self.download, addCompareEtagLocal: true, addDirectorySynchronized: true)
  280. for metadata in metadatasChanged.metadatasUpdate {
  281. if metadata.directory {
  282. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: self.selector)
  283. }
  284. }
  285. for metadata in metadatasChanged.metadatasLocalUpdate {
  286. NCOperationQueue.shared.download(metadata: metadata, selector: self.selector)
  287. }
  288. }
  289. // Update etag directory
  290. NCManageDatabase.shared.addDirectory(encrypted: metadataFolder.e2eEncrypted, favorite: metadataFolder.favorite, ocId: metadataFolder.ocId, fileId: metadataFolder.fileId, etag: metadataFolder.etag, permissions: metadataFolder.permissions, serverUrl: serverUrl, account: metadataFolder.account)
  291. }
  292. } else if errorCode == NCGlobal.shared.errorResourceNotFound && self.metadata.directory {
  293. NCManageDatabase.shared.deleteDirectoryAndSubDirectory(serverUrl: self.metadata.serverUrl, account: self.metadata.account)
  294. }
  295. self.finish()
  296. }
  297. } else {
  298. let metadatas = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl))
  299. for metadata in metadatas {
  300. if metadata.directory {
  301. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: self.selector)
  302. } else {
  303. if NCManageDatabase.shared.isDownloadMetadata(metadata, download: self.download) {
  304. NCOperationQueue.shared.download(metadata: metadata, selector: self.selector)
  305. }
  306. }
  307. }
  308. self.finish()
  309. }
  310. }
  311. } else {
  312. if NCManageDatabase.shared.isDownloadMetadata(metadata, download: self.download) {
  313. NCOperationQueue.shared.download(metadata: metadata, selector: self.selector)
  314. }
  315. self.finish()
  316. }
  317. }
  318. }
  319. }
  320. //MARK: -
  321. class NCOperationDownloadThumbnail: ConcurrentOperation {
  322. var metadata: tableMetadata
  323. var cell: NCCellProtocol!
  324. var view: UIView?
  325. var fileNamePath: String = ""
  326. var fileNamePreviewLocalPath: String = ""
  327. var fileNameIconLocalPath: String = ""
  328. init(metadata: tableMetadata, cell: NCCellProtocol, view: UIView?) {
  329. self.metadata = tableMetadata.init(value: metadata)
  330. self.cell = cell
  331. self.view = view
  332. self.fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
  333. self.fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  334. self.fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
  335. }
  336. override func start() {
  337. if isCancelled {
  338. self.finish()
  339. } else {
  340. NCCommunication.shared.downloadPreview(fileNamePathOrFileId: fileNamePath, fileNamePreviewLocalPath: fileNamePreviewLocalPath , widthPreview: NCGlobal.shared.sizePreview, heightPreview: NCGlobal.shared.sizePreview, fileNameIconLocalPath: fileNameIconLocalPath, sizeIcon: NCGlobal.shared.sizeIcon) { (account, imagePreview, imageIcon, errorCode, errorDescription) in
  341. if errorCode == 0 && imageIcon != nil {
  342. if self.metadata.ocId == self.cell.fileObjectId {
  343. if let filePreviewImageView = self.cell?.filePreviewImageView {
  344. UIView.transition(with: filePreviewImageView,
  345. duration: 0.75,
  346. options: .transitionCrossDissolve,
  347. animations: { filePreviewImageView.image = imageIcon! },
  348. completion: nil)
  349. }
  350. } else {
  351. if self.view is UICollectionView {
  352. (self.view as? UICollectionView)?.reloadData()
  353. } else if self.view is UITableView{
  354. (self.view as? UITableView)?.reloadData()
  355. }
  356. }
  357. }
  358. self.finish()
  359. }
  360. }
  361. }
  362. }
  363. //MARK: -
  364. class NCOperationDownloadAvatar: ConcurrentOperation {
  365. var user: String
  366. var fileName: String
  367. var etag: String?
  368. var fileNameLocalPath: String
  369. var cell: NCCellProtocol!
  370. var view: UIView?
  371. init(user: String, fileName: String, fileNameLocalPath: String, cell: NCCellProtocol, view: UIView?) {
  372. self.user = user
  373. self.fileName = fileName
  374. self.fileNameLocalPath = fileNameLocalPath
  375. self.cell = cell
  376. self.view = view
  377. self.etag = NCManageDatabase.shared.getTableAvatar(fileName: fileName)?.etag
  378. }
  379. override func start() {
  380. if isCancelled {
  381. self.finish()
  382. } else {
  383. NCCommunication.shared.downloadAvatar(user: user, fileNameLocalPath: fileNameLocalPath, size: NCGlobal.shared.avatarSize, etag: self.etag) { (account, data, etag, errorCode, errorMessage) in
  384. if errorCode == 0, let data = data, let etag = etag, var image = UIImage.init(data: data) {
  385. image = NCUtility.shared.createAvatar(image: image, size: 30)
  386. NCManageDatabase.shared.addAvatar(fileName: self.fileName, etag: etag)
  387. #if !EXTENSION
  388. (UIApplication.shared.delegate as! AppDelegate).avatars[self.user] = image
  389. #endif
  390. if self.user == self.cell.fileUser {
  391. if let avatarImageView = self.cell?.fileAvatarImageView {
  392. UIView.transition(with: avatarImageView, duration: 0.75, options: .transitionCrossDissolve) {
  393. avatarImageView.image = image
  394. } completion: { _ in
  395. if self.view is UICollectionView {
  396. (self.view as? UICollectionView)?.reloadData()
  397. } else if self.view is UITableView{
  398. (self.view as? UITableView)?.reloadData()
  399. }
  400. }
  401. }
  402. } else {
  403. if self.view is UICollectionView {
  404. (self.view as? UICollectionView)?.reloadData()
  405. } else if self.view is UITableView{
  406. (self.view as? UITableView)?.reloadData()
  407. }
  408. }
  409. }
  410. self.finish()
  411. }
  412. }
  413. }
  414. }