NCOperationQueue.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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(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, onlyLocalCache: Bool) {
  71. for operation in deleteQueue.operations as! [NCOperationDelete] {
  72. if operation.metadata.ocId == metadata.ocId {
  73. return
  74. }
  75. }
  76. deleteQueue.addOperation(NCOperationDelete(metadata: metadata, onlyLocalCache: onlyLocalCache))
  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(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(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(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(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, dispalyName: String?, fileName: String, cell: NCCellProtocol, view: UIView?, cellImageView: UIImageView?) {
  142. let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
  143. if let image = NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName) {
  144. cellImageView?.image = image
  145. cell.fileAvatarImageView?.image = image
  146. return
  147. }
  148. if let account = NCManageDatabase.shared.getActiveAccount() {
  149. cellImageView?.image = NCUtility.shared.loadUserImage(
  150. for: user,
  151. displayName: dispalyName,
  152. userBaseUrl: account)
  153. }
  154. for operation in downloadAvatarQueue.operations as! [NCOperationDownloadAvatar] {
  155. if operation.fileName == fileName {
  156. return
  157. }
  158. }
  159. downloadAvatarQueue.addOperation(NCOperationDownloadAvatar(user: user, fileName: fileName, fileNameLocalPath: fileNameLocalPath, cell: cell, view: view, cellImageView: cellImageView))
  160. }
  161. func cancelDownloadAvatar(user: String) {
  162. for operation in downloadAvatarQueue.operations as! [NCOperationDownloadAvatar] {
  163. if operation.user == user {
  164. operation.cancel()
  165. }
  166. }
  167. }
  168. @objc func downloadAvatarCancelAll() {
  169. downloadAvatarQueue.cancelAll()
  170. }
  171. }
  172. // MARK: -
  173. class NCOperationDownload: ConcurrentOperation {
  174. var metadata: tableMetadata
  175. var selector: String
  176. init(metadata: tableMetadata, selector: String) {
  177. self.metadata = tableMetadata.init(value: metadata)
  178. self.selector = selector
  179. }
  180. override func start() {
  181. if isCancelled {
  182. self.finish()
  183. } else {
  184. NCNetworking.shared.download(metadata: metadata, selector: self.selector) { _ in
  185. self.finish()
  186. }
  187. }
  188. }
  189. }
  190. // MARK: -
  191. class NCOperationDelete: ConcurrentOperation {
  192. var metadata: tableMetadata
  193. var onlyLocalCache: Bool
  194. init(metadata: tableMetadata, onlyLocalCache: Bool) {
  195. self.metadata = tableMetadata.init(value: metadata)
  196. self.onlyLocalCache = onlyLocalCache
  197. }
  198. override func start() {
  199. if isCancelled {
  200. self.finish()
  201. } else {
  202. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: onlyLocalCache) { errorCode, errorDescription in
  203. if errorCode != 0 {
  204. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  205. }
  206. self.finish()
  207. }
  208. }
  209. }
  210. }
  211. // MARK: -
  212. class NCOperationCopyMove: ConcurrentOperation {
  213. var metadata: tableMetadata
  214. var serverUrlTo: String
  215. var overwrite: Bool
  216. var move: Bool
  217. init(metadata: tableMetadata, serverUrlTo: String, overwrite: Bool, move: Bool) {
  218. self.metadata = tableMetadata.init(value: metadata)
  219. self.serverUrlTo = serverUrlTo
  220. self.overwrite = overwrite
  221. self.move = move
  222. }
  223. override func start() {
  224. if isCancelled {
  225. self.finish()
  226. } else {
  227. if move {
  228. NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrlTo, overwrite: overwrite) { errorCode, errorDescription in
  229. if errorCode != 0 {
  230. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  231. }
  232. self.finish()
  233. }
  234. } else {
  235. NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrlTo, overwrite: overwrite) { errorCode, errorDescription in
  236. if errorCode != 0 {
  237. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  238. }
  239. self.finish()
  240. }
  241. }
  242. }
  243. }
  244. }
  245. // MARK: -
  246. class NCOperationSynchronization: ConcurrentOperation {
  247. var metadata: tableMetadata
  248. var selector: String
  249. var download: Bool
  250. init(metadata: tableMetadata, selector: String) {
  251. self.metadata = tableMetadata.init(value: metadata)
  252. self.selector = selector
  253. if selector == NCGlobal.shared.selectorDownloadFile || selector == NCGlobal.shared.selectorDownloadAllFile {
  254. self.download = true
  255. } else {
  256. self.download = false
  257. }
  258. }
  259. override func start() {
  260. if isCancelled {
  261. self.finish()
  262. } else {
  263. if metadata.directory {
  264. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  265. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, serverUrl))
  266. NCCommunication.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "0", showHiddenFiles: CCUtility.getShowHiddenFiles()) { account, files, _, errorCode, _ in
  267. if (errorCode == 0) && (directory?.etag != files.first?.etag || self.selector == NCGlobal.shared.selectorDownloadAllFile) {
  268. NCCommunication.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: CCUtility.getShowHiddenFiles(), queue: NCCommunicationCommon.shared.backgroundQueue) { account, files, _, errorCode, _ in
  269. if errorCode == 0 {
  270. NCManageDatabase.shared.convertNCCommunicationFilesToMetadatas(files, useMetadataFolder: true, account: account) { metadataFolder, _, metadatas in
  271. let metadatasResult = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND status == %d", account, serverUrl, NCGlobal.shared.metadataStatusNormal))
  272. if self.selector == NCGlobal.shared.selectorDownloadAllFile {
  273. NCManageDatabase.shared.updateMetadatas(metadatas, metadatasResult: metadatasResult)
  274. for metadata in metadatas {
  275. if metadata.directory {
  276. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: self.selector)
  277. } else {
  278. if NCManageDatabase.shared.isDownloadMetadata(metadata, download: true) {
  279. NCOperationQueue.shared.download(metadata: metadata, selector: self.selector)
  280. }
  281. }
  282. }
  283. } else {
  284. let metadatasChanged = NCManageDatabase.shared.updateMetadatas(metadatas, metadatasResult: metadatasResult, addExistsInLocal: self.download, addCompareEtagLocal: true, addDirectorySynchronized: true)
  285. for metadata in metadatasChanged.metadatasUpdate {
  286. if metadata.directory {
  287. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: self.selector)
  288. }
  289. }
  290. for metadata in metadatasChanged.metadatasLocalUpdate {
  291. NCOperationQueue.shared.download(metadata: metadata, selector: self.selector)
  292. }
  293. }
  294. // Update etag directory
  295. 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)
  296. }
  297. } else if errorCode == NCGlobal.shared.errorResourceNotFound && self.metadata.directory {
  298. NCManageDatabase.shared.deleteDirectoryAndSubDirectory(serverUrl: self.metadata.serverUrl, account: self.metadata.account)
  299. }
  300. self.finish()
  301. }
  302. } else {
  303. let metadatas = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl))
  304. for metadata in metadatas {
  305. if metadata.directory {
  306. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: self.selector)
  307. } else {
  308. if NCManageDatabase.shared.isDownloadMetadata(metadata, download: self.download) {
  309. NCOperationQueue.shared.download(metadata: metadata, selector: self.selector)
  310. }
  311. }
  312. }
  313. self.finish()
  314. }
  315. }
  316. } else {
  317. if NCManageDatabase.shared.isDownloadMetadata(metadata, download: self.download) {
  318. NCOperationQueue.shared.download(metadata: metadata, selector: self.selector)
  319. }
  320. self.finish()
  321. }
  322. }
  323. }
  324. }
  325. // MARK: -
  326. class NCOperationDownloadThumbnail: ConcurrentOperation {
  327. var metadata: tableMetadata
  328. var cell: NCCellProtocol?
  329. var view: UIView?
  330. var fileNamePath: String = ""
  331. var fileNamePreviewLocalPath: String = ""
  332. var fileNameIconLocalPath: String = ""
  333. init(metadata: tableMetadata, cell: NCCellProtocol?, view: UIView?) {
  334. self.metadata = tableMetadata.init(value: metadata)
  335. self.cell = cell
  336. self.view = view
  337. self.fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
  338. self.fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  339. self.fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
  340. }
  341. override func start() {
  342. if isCancelled {
  343. self.finish()
  344. } else {
  345. var etagResource: String?
  346. if FileManager.default.fileExists(atPath: fileNameIconLocalPath) && FileManager.default.fileExists(atPath: fileNamePreviewLocalPath) {
  347. etagResource = metadata.etagResource
  348. }
  349. NCCommunication.shared.downloadPreview(
  350. fileNamePathOrFileId: fileNamePath,
  351. fileNamePreviewLocalPath: fileNamePreviewLocalPath,
  352. widthPreview: NCGlobal.shared.sizePreview,
  353. heightPreview: NCGlobal.shared.sizePreview,
  354. fileNameIconLocalPath: fileNameIconLocalPath,
  355. sizeIcon: NCGlobal.shared.sizeIcon,
  356. etag: etagResource,
  357. queue: NCCommunicationCommon.shared.backgroundQueue) { _, _, imageIcon, _, etag, errorCode, _ in
  358. if errorCode == 0 && imageIcon != nil {
  359. NCManageDatabase.shared.setMetadataEtagResource(ocId: self.metadata.ocId, etagResource: etag)
  360. DispatchQueue.main.async {
  361. if self.metadata.ocId == self.cell?.fileObjectId {
  362. if let filePreviewImageView = self.cell?.filePreviewImageView {
  363. UIView.transition(with: filePreviewImageView,
  364. duration: 0.75,
  365. options: .transitionCrossDissolve,
  366. animations: { filePreviewImageView.image = imageIcon! },
  367. completion: nil)
  368. }
  369. } else {
  370. if self.view is UICollectionView {
  371. (self.view as? UICollectionView)?.reloadData()
  372. } else if self.view is UITableView {
  373. (self.view as? UITableView)?.reloadData()
  374. }
  375. }
  376. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedThumbnail, userInfo: ["ocId": self.metadata.ocId])
  377. }
  378. }
  379. self.finish()
  380. }
  381. }
  382. }
  383. }
  384. // MARK: -
  385. class NCOperationDownloadAvatar: ConcurrentOperation {
  386. var user: String
  387. var fileName: String
  388. var etag: String?
  389. var fileNameLocalPath: String
  390. var cell: NCCellProtocol!
  391. var view: UIView?
  392. var cellImageView: UIImageView?
  393. init(user: String, fileName: String, fileNameLocalPath: String, cell: NCCellProtocol, view: UIView?, cellImageView: UIImageView?) {
  394. self.user = user
  395. self.fileName = fileName
  396. self.fileNameLocalPath = fileNameLocalPath
  397. self.cell = cell
  398. self.view = view
  399. self.etag = NCManageDatabase.shared.getTableAvatar(fileName: fileName)?.etag
  400. self.cellImageView = cellImageView
  401. }
  402. override func start() {
  403. if isCancelled {
  404. self.finish()
  405. } else {
  406. NCCommunication.shared.downloadAvatar(user: user, fileNameLocalPath: fileNameLocalPath, sizeImage: NCGlobal.shared.avatarSize, avatarSizeRounded: NCGlobal.shared.avatarSizeRounded, etag: self.etag, queue: NCCommunicationCommon.shared.backgroundQueue) { _, imageAvatar, _, etag, errorCode, _ in
  407. if errorCode == 0, let imageAvatar = imageAvatar, let etag = etag {
  408. NCManageDatabase.shared.addAvatar(fileName: self.fileName, etag: etag)
  409. DispatchQueue.main.async {
  410. if self.user == self.cell.fileUser {
  411. if let avatarImageView = self.cellImageView {
  412. UIView.transition(with: avatarImageView, duration: 0.75, options: .transitionCrossDissolve) {
  413. avatarImageView.image = imageAvatar
  414. } completion: { _ in
  415. if self.view is UICollectionView {
  416. (self.view as? UICollectionView)?.reloadData()
  417. } else if self.view is UITableView {
  418. (self.view as? UITableView)?.reloadData()
  419. }
  420. }
  421. }
  422. } else {
  423. if self.view is UICollectionView {
  424. (self.view as? UICollectionView)?.reloadData()
  425. } else if self.view is UITableView {
  426. (self.view as? UITableView)?.reloadData()
  427. }
  428. }
  429. }
  430. } else if errorCode == NCGlobal.shared.errorNotModified {
  431. NCManageDatabase.shared.setAvatarLoaded(fileName: self.fileName)
  432. }
  433. self.finish()
  434. }
  435. }
  436. }
  437. }