NCNetworkingNotificationCenter.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // NCNetworkingNotificationCenter.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 19/04/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. @objc class NCNetworkingNotificationCenter: NSObject {
  25. @objc public static let shared: NCNetworkingNotificationCenter = {
  26. let instance = NCNetworkingNotificationCenter()
  27. NotificationCenter.default.addObserver(instance, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
  28. NotificationCenter.default.addObserver(instance, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadedFile), object: nil)
  29. return instance
  30. }()
  31. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. var viewerQuickLook: NCViewerQuickLook?
  33. //MARK: - Download
  34. @objc func downloadedFile(_ notification: NSNotification) {
  35. if let userInfo = notification.userInfo as NSDictionary? {
  36. if let metadata = userInfo["metadata"] as? tableMetadata, let selector = userInfo["selector"] as? String, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String {
  37. if metadata.account != appDelegate.account { return }
  38. if errorCode == 0 {
  39. let fileURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  40. switch selector {
  41. case selectorLoadFileQuickLook:
  42. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  43. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  44. viewerQuickLook = NCViewerQuickLook.init()
  45. viewerQuickLook?.quickLook(url: URL(fileURLWithPath: fileNamePath), viewController: appDelegate.activeMain)
  46. case selectorLoadFileView, selectorLoadFileViewFavorite:
  47. if UIApplication.shared.applicationState == UIApplication.State.active {
  48. if metadata.contentType.contains("opendocument") && !NCUtility.shared.isRichDocument(metadata) {
  49. NCMainCommon.shared.openIn(fileURL: fileURL, selector: selector)
  50. } else if metadata.typeFile == k_metadataTypeFile_compress || metadata.typeFile == k_metadataTypeFile_unknown {
  51. NCMainCommon.shared.openIn(fileURL: fileURL, selector: selector)
  52. } else if metadata.typeFile == k_metadataTypeFile_imagemeter {
  53. NCMainCommon.shared.openIn(fileURL: fileURL, selector: selector)
  54. } else {
  55. if appDelegate.activeMain.view.window != nil && selector == selectorLoadFileView{
  56. appDelegate.activeMain.shouldPerformSegue(metadata, selector: selector)
  57. } else if appDelegate.activeFavorite.view.window != nil && selector == selectorLoadFileViewFavorite{
  58. appDelegate.activeFavorite.segue(metadata: metadata)
  59. }
  60. }
  61. }
  62. case selectorOpenIn, selectorOpenInDetail:
  63. if UIApplication.shared.applicationState == UIApplication.State.active {
  64. NCMainCommon.shared.openIn(fileURL: fileURL, selector: selector)
  65. }
  66. case selectorSaveAlbum:
  67. appDelegate.activeMain.save(toPhotoAlbum: metadata)
  68. case selectorLoadCopy:
  69. appDelegate.activeMain.copyFile(toPasteboard: metadata)
  70. case selectorLoadOffline:
  71. NCManageDatabase.sharedInstance.setLocalFile(ocId: metadata.ocId, offline: true)
  72. default:
  73. break
  74. }
  75. } else {
  76. // File do not exists on server, remove in local
  77. if (errorCode == 404 || errorCode == -1011) { // - 1011 = kCFURLErrorBadServerResponse
  78. do {
  79. try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId))
  80. } catch { }
  81. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  82. NCManageDatabase.sharedInstance.deleteLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  83. } else {
  84. NCContentPresenter.shared.messageNotification("_download_file_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  85. }
  86. }
  87. }
  88. }
  89. }
  90. //MARK: - Upload
  91. @objc func uploadedFile(_ notification: NSNotification) {
  92. if let userInfo = notification.userInfo as NSDictionary? {
  93. if let metadata = userInfo["metadata"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String {
  94. if metadata.account == appDelegate.account {
  95. if errorCode != 0 {
  96. if errorCode != -999 && errorCode != 401 && errorDescription != "" {
  97. NCContentPresenter.shared.messageNotification("_upload_file_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }