NCOperationQueue.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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, urlBase: String, view: Any, indexPath: IndexPath) {
  113. if metadata.hasPreview && metadata.status == NCGlobal.shared.metadataStatusNormal && (!CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag)) {
  114. for operation in downloadThumbnailQueue.operations as! [NCOperationDownloadThumbnail] {
  115. if operation.metadata.ocId == metadata.ocId {
  116. return
  117. }
  118. }
  119. downloadThumbnailQueue.addOperation(NCOperationDownloadThumbnail.init(metadata: metadata, urlBase: urlBase, view: view, indexPath: indexPath))
  120. }
  121. }
  122. func cancelDownloadThumbnail(metadata: tableMetadata) {
  123. for operation in downloadThumbnailQueue.operations as! [NCOperationDownloadThumbnail] {
  124. if operation.metadata.ocId == metadata.ocId {
  125. operation.cancel()
  126. }
  127. }
  128. }
  129. @objc func downloadThumbnailCancelAll() {
  130. downloadThumbnailQueue.cancelAll()
  131. }
  132. // Download Avatar
  133. func downloadAvatar(user: String, fileNameLocalPath: String, view: UIView, indexPath: IndexPath) {
  134. var cell: NCCellProtocol?
  135. if view is UICollectionView {
  136. if indexPath.section < (view as! UICollectionView).numberOfSections && indexPath.row < (view as! UICollectionView).numberOfItems(inSection: indexPath.section) {
  137. cell = (view as! UICollectionView).cellForItem(at: indexPath) as? NCCellProtocol
  138. }
  139. } else {
  140. if indexPath.section < (view as! UITableView).numberOfSections && indexPath.row < (view as! UITableView).numberOfRows(inSection: indexPath.section) {
  141. cell = (view as! UITableView).cellForRow(at: indexPath) as? NCCellProtocol
  142. }
  143. }
  144. #if !EXTENSION
  145. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  146. if let image = appDelegate.avatars[user] {
  147. cell?.avatarImageView?.image = image
  148. return
  149. }
  150. #endif
  151. cell?.avatarImageView?.image = UIImage(named: "avatar")
  152. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  153. cell?.avatarImageView?.image = NCUtility.shared.createAvatar(image: image, size: 30)
  154. }
  155. for operation in downloadAvatarQueue.operations as! [NCOperationDownloadAvatar] {
  156. if operation.user == user {
  157. return
  158. }
  159. }
  160. downloadAvatarQueue.addOperation(NCOperationDownloadAvatar.init(user: user, fileNameLocalPath: fileNameLocalPath, view: view, indexPath: indexPath))
  161. }
  162. @objc func downloadAvatarCancelAll() {
  163. downloadAvatarQueue.cancelAll()
  164. }
  165. }
  166. //MARK: -
  167. class NCOperationDownload: ConcurrentOperation {
  168. var metadata: tableMetadata
  169. var selector: String
  170. init(metadata: tableMetadata, selector: String) {
  171. self.metadata = tableMetadata.init(value: metadata)
  172. self.selector = selector
  173. }
  174. override func start() {
  175. if isCancelled {
  176. self.finish()
  177. } else {
  178. NCNetworking.shared.download(metadata: metadata, selector: self.selector) { (_) in
  179. self.finish()
  180. }
  181. }
  182. }
  183. }
  184. //MARK: -
  185. class NCOperationDelete: ConcurrentOperation {
  186. var metadata: tableMetadata
  187. var onlyLocal: Bool
  188. init(metadata: tableMetadata, onlyLocal: Bool) {
  189. self.metadata = tableMetadata.init(value: metadata)
  190. self.onlyLocal = onlyLocal
  191. }
  192. override func start() {
  193. if isCancelled {
  194. self.finish()
  195. } else {
  196. NCNetworking.shared.deleteMetadata(metadata, onlyLocal: onlyLocal) { (errorCode, errorDescription) in
  197. if errorCode != 0 {
  198. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  199. }
  200. self.finish()
  201. }
  202. }
  203. }
  204. }
  205. //MARK: -
  206. class NCOperationCopyMove: ConcurrentOperation {
  207. var metadata: tableMetadata
  208. var serverUrlTo: String
  209. var overwrite: Bool
  210. var move: Bool
  211. init(metadata: tableMetadata, serverUrlTo: String, overwrite: Bool, move: Bool) {
  212. self.metadata = tableMetadata.init(value: metadata)
  213. self.serverUrlTo = serverUrlTo
  214. self.overwrite = overwrite
  215. self.move = move
  216. }
  217. override func start() {
  218. if isCancelled {
  219. self.finish()
  220. } else {
  221. if move {
  222. NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrlTo, overwrite: overwrite) { (errorCode, errorDescription) in
  223. if errorCode != 0 {
  224. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  225. }
  226. self.finish()
  227. }
  228. } else {
  229. NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrlTo, overwrite: overwrite) { (errorCode, errorDescription) in
  230. if errorCode != 0 {
  231. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  232. }
  233. self.finish()
  234. }
  235. }
  236. }
  237. }
  238. }
  239. //MARK: -
  240. class NCOperationSynchronization: ConcurrentOperation {
  241. var metadata: tableMetadata
  242. var selector: String
  243. var download: Bool
  244. init(metadata: tableMetadata, selector: String) {
  245. self.metadata = tableMetadata.init(value: metadata)
  246. self.selector = selector
  247. if selector == NCGlobal.shared.selectorDownloadFile || selector == NCGlobal.shared.selectorDownloadAllFile {
  248. self.download = true
  249. } else {
  250. self.download = false
  251. }
  252. }
  253. override func start() {
  254. if isCancelled {
  255. self.finish()
  256. } else {
  257. if metadata.directory {
  258. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  259. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", metadata.account, serverUrl))
  260. NCCommunication.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "0", showHiddenFiles: CCUtility.getShowHiddenFiles()) { (account, files, responseData, errorCode, errorDescription) in
  261. if (errorCode == 0) && (directory?.etag != files.first?.etag || self.selector == NCGlobal.shared.selectorDownloadAllFile) {
  262. NCCommunication.shared.readFileOrFolder(serverUrlFileName: serverUrl, depth: "1", showHiddenFiles: CCUtility.getShowHiddenFiles()) { (account, files, responseData, errorCode, errorDescription) in
  263. if errorCode == 0 {
  264. NCManageDatabase.shared.convertNCCommunicationFilesToMetadatas(files, useMetadataFolder: true, account: account) { (metadataFolder, metadatasFolder, metadatas) in
  265. let metadatasResult = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND status == %d", account, serverUrl, NCGlobal.shared.metadataStatusNormal))
  266. if self.selector == NCGlobal.shared.selectorDownloadAllFile {
  267. NCManageDatabase.shared.updateMetadatas(metadatas, metadatasResult: metadatasResult)
  268. for metadata in metadatas {
  269. if metadata.directory {
  270. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: self.selector)
  271. } else {
  272. if NCManageDatabase.shared.isDownloadMetadata(metadata, download: true) {
  273. NCOperationQueue.shared.download(metadata: metadata, selector: self.selector)
  274. }
  275. }
  276. }
  277. } else {
  278. let metadatasChanged = NCManageDatabase.shared.updateMetadatas(metadatas, metadatasResult: metadatasResult, addExistsInLocal: self.download, addCompareEtagLocal: true, addDirectorySynchronized: true)
  279. for metadata in metadatasChanged.metadatasUpdate {
  280. if metadata.directory {
  281. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: self.selector)
  282. }
  283. }
  284. for metadata in metadatasChanged.metadatasLocalUpdate {
  285. NCOperationQueue.shared.download(metadata: metadata, selector: self.selector)
  286. }
  287. }
  288. // Update etag directory
  289. 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)
  290. }
  291. } else if errorCode == NCGlobal.shared.errorResourceNotFound && self.metadata.directory {
  292. NCManageDatabase.shared.deleteDirectoryAndSubDirectory(serverUrl: self.metadata.serverUrl, account: self.metadata.account)
  293. }
  294. self.finish()
  295. }
  296. } else {
  297. let metadatas = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", account, serverUrl))
  298. for metadata in metadatas {
  299. if metadata.directory {
  300. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: self.selector)
  301. } else {
  302. if NCManageDatabase.shared.isDownloadMetadata(metadata, download: self.download) {
  303. NCOperationQueue.shared.download(metadata: metadata, selector: self.selector)
  304. }
  305. }
  306. }
  307. self.finish()
  308. }
  309. }
  310. } else {
  311. if NCManageDatabase.shared.isDownloadMetadata(metadata, download: self.download) {
  312. NCOperationQueue.shared.download(metadata: metadata, selector: self.selector)
  313. }
  314. self.finish()
  315. }
  316. }
  317. }
  318. }
  319. //MARK: -
  320. class NCOperationDownloadThumbnail: ConcurrentOperation {
  321. var metadata: tableMetadata
  322. var urlBase: String
  323. var view: Any
  324. var indexPath: IndexPath
  325. var fileNamePath: String = ""
  326. var fileNamePreviewLocalPath: String = ""
  327. var fileNameIconLocalPath: String = ""
  328. init(metadata: tableMetadata, urlBase: String, view: Any, indexPath: IndexPath) {
  329. self.metadata = tableMetadata.init(value: metadata)
  330. self.urlBase = urlBase
  331. self.view = view
  332. self.indexPath = indexPath
  333. self.fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: urlBase, account: metadata.account)!
  334. self.fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)!
  335. self.fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
  336. }
  337. override func start() {
  338. if isCancelled {
  339. self.finish()
  340. } else {
  341. 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
  342. var cell: NCCellProtocol?
  343. if self.view is UICollectionView {
  344. if self.indexPath.section < (self.view as! UICollectionView).numberOfSections && self.indexPath.row < (self.view as! UICollectionView).numberOfItems(inSection: self.indexPath.section) {
  345. cell = (self.view as! UICollectionView).cellForItem(at: self.indexPath) as? NCCellProtocol
  346. }
  347. } else {
  348. if self.indexPath.section < (self.view as! UITableView).numberOfSections && self.indexPath.row < (self.view as! UITableView).numberOfRows(inSection: self.indexPath.section) {
  349. cell = (self.view as! UITableView).cellForRow(at: self.indexPath) as? NCCellProtocol
  350. }
  351. }
  352. if let filePreviewImageView = cell!.filePreviewImageView {
  353. var previewImage: UIImage!
  354. if errorCode == 0 && imageIcon != nil {
  355. previewImage = imageIcon
  356. } else {
  357. if self.metadata.iconName.count > 0 {
  358. previewImage = UIImage(named: self.metadata.iconName)
  359. } else {
  360. previewImage = UIImage(named: "file")
  361. }
  362. }
  363. UIView.transition(with: filePreviewImageView,
  364. duration: 0.75,
  365. options: .transitionCrossDissolve,
  366. animations: { filePreviewImageView.image = previewImage! },
  367. completion: nil)
  368. }
  369. self.finish()
  370. }
  371. }
  372. }
  373. }
  374. //MARK: -
  375. class NCOperationDownloadAvatar: ConcurrentOperation {
  376. var user: String
  377. var fileNameLocalPath: String
  378. var view: Any
  379. var indexPath: IndexPath
  380. init(user: String, fileNameLocalPath: String, view: Any, indexPath: IndexPath) {
  381. self.user = user
  382. self.fileNameLocalPath = fileNameLocalPath
  383. self.view = view
  384. self.indexPath = indexPath
  385. }
  386. override func start() {
  387. if isCancelled {
  388. self.finish()
  389. } else {
  390. NCCommunication.shared.downloadAvatar(user: user, fileNameLocalPath: fileNameLocalPath, size: NCGlobal.shared.avatarSize) { (account, data, errorCode, errorMessage) in
  391. if errorCode == 0 {
  392. if var image = UIImage(contentsOfFile: self.fileNameLocalPath) {
  393. image = NCUtility.shared.createAvatar(image: image, size: 30)
  394. var cell: NCCellProtocol?
  395. if self.view is UICollectionView {
  396. if self.indexPath.section < (self.view as! UICollectionView).numberOfSections && self.indexPath.row < (self.view as! UICollectionView).numberOfItems(inSection: self.indexPath.section) {
  397. cell = (self.view as! UICollectionView).cellForItem(at: self.indexPath) as? NCCellProtocol
  398. }
  399. } else {
  400. if self.indexPath.section < (self.view as! UITableView).numberOfSections && self.indexPath.row < (self.view as! UITableView).numberOfRows(inSection: self.indexPath.section) {
  401. cell = (self.view as! UITableView).cellForRow(at: self.indexPath) as? NCCellProtocol
  402. }
  403. }
  404. cell?.avatarImageView?.image = image
  405. #if !EXTENSION
  406. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  407. appDelegate.avatars[self.user] = image
  408. #endif
  409. }
  410. }
  411. self.finish()
  412. }
  413. }
  414. }
  415. }