NCNetworkingNotificationCenter.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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, UIDocumentInteractionControllerDelegate {
  25. @objc public static let shared: NCNetworkingNotificationCenter = {
  26. let instance = NCNetworkingNotificationCenter()
  27. NotificationCenter.default.addObserver(instance, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  28. NotificationCenter.default.addObserver(instance, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  29. return instance
  30. }()
  31. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. var viewerQuickLook: NCViewerQuickLook?
  33. var documentController: UIDocumentInteractionController?
  34. //MARK: - Download
  35. @objc func downloadedFile(_ notification: NSNotification) {
  36. if let userInfo = notification.userInfo as NSDictionary? {
  37. if let ocId = userInfo["ocId"] as? String, let selector = userInfo["selector"] as? String, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  38. if metadata.account != appDelegate.account { return }
  39. if errorCode == 0 {
  40. let fileURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  41. documentController = UIDocumentInteractionController(url: fileURL)
  42. documentController?.delegate = self
  43. switch selector {
  44. case NCGlobal.shared.selectorLoadFileQuickLook:
  45. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  46. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  47. viewerQuickLook = NCViewerQuickLook.init()
  48. viewerQuickLook?.quickLook(url: URL(fileURLWithPath: fileNamePath))
  49. case NCGlobal.shared.selectorLoadFileView:
  50. if UIApplication.shared.applicationState == UIApplication.State.active {
  51. if metadata.contentType.contains("opendocument") && !NCUtility.shared.isRichDocument(metadata) {
  52. if let view = appDelegate.window?.rootViewController?.view {
  53. documentController?.presentOptionsMenu(from: CGRect.zero, in: view, animated: true)
  54. }
  55. } else if metadata.typeFile == NCGlobal.shared.metadataTypeFileCompress || metadata.typeFile == NCGlobal.shared.metadataTypeFileUnknown {
  56. if let view = appDelegate.window?.rootViewController?.view {
  57. documentController?.presentOptionsMenu(from: CGRect.zero, in: view, animated: true)
  58. }
  59. } else if metadata.typeFile == NCGlobal.shared.metadataTypeFileImagemeter {
  60. if let view = appDelegate.window?.rootViewController?.view {
  61. documentController?.presentOptionsMenu(from: CGRect.zero, in: view, animated: true)
  62. }
  63. } else {
  64. if let viewController = self.appDelegate.activeViewController {
  65. NCViewer.shared.view(viewController: viewController, metadata: metadata, metadatas: [metadata])
  66. }
  67. }
  68. }
  69. case NCGlobal.shared.selectorOpenIn:
  70. if UIApplication.shared.applicationState == UIApplication.State.active {
  71. if let view = appDelegate.window?.rootViewController?.view {
  72. documentController?.presentOptionsMenu(from: CGRect.zero, in: view, animated: true)
  73. }
  74. }
  75. case NCGlobal.shared.selectorLoadCopy:
  76. NCCollectionCommon.shared.copyPasteboard()
  77. case NCGlobal.shared.selectorLoadOffline:
  78. NCManageDatabase.shared.setLocalFile(ocId: metadata.ocId, offline: true)
  79. case NCGlobal.shared.selectorSaveAlbum:
  80. NCCollectionCommon.shared.saveAlbum(metadata: metadata)
  81. case NCGlobal.shared.selectorSaveAlbumLivePhotoIMG, NCGlobal.shared.selectorSaveAlbumLivePhotoMOV:
  82. var metadata = metadata
  83. var metadataMOV = metadata
  84. guard let metadataTMP = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) else { break }
  85. if selector == NCGlobal.shared.selectorSaveAlbumLivePhotoIMG {
  86. metadataMOV = metadataTMP
  87. }
  88. if selector == NCGlobal.shared.selectorSaveAlbumLivePhotoMOV {
  89. metadata = metadataTMP
  90. }
  91. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && CCUtility.fileProviderStorageExists(metadataMOV.ocId, fileNameView: metadataMOV.fileNameView) {
  92. NCCollectionCommon.shared.saveLivePhotoToDisk(metadata: metadata, metadataMov: metadataMOV, progressView: nil, viewActivity: self.appDelegate.window?.rootViewController?.view)
  93. }
  94. default:
  95. break
  96. }
  97. } else {
  98. // File do not exists on server, remove in local
  99. if (errorCode == NCGlobal.shared.ErrorResourceNotFound || errorCode == NCGlobal.shared.ErrorBadServerResponse) {
  100. do {
  101. try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId))
  102. } catch { }
  103. NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  104. NCManageDatabase.shared.deleteLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  105. } else {
  106. NCContentPresenter.shared.messageNotification("_download_file_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  107. }
  108. }
  109. }
  110. }
  111. }
  112. @objc func openShare(ViewController: UIViewController, metadata: tableMetadata, indexPage: Int) {
  113. let shareNavigationController = UIStoryboard(name: "NCShare", bundle: nil).instantiateInitialViewController() as! UINavigationController
  114. let shareViewController = shareNavigationController.topViewController as! NCSharePaging
  115. shareViewController.metadata = metadata
  116. shareViewController.indexPage = indexPage
  117. shareNavigationController.modalPresentationStyle = .formSheet
  118. ViewController.present(shareNavigationController, animated: true, completion: nil)
  119. }
  120. @objc func downloadOpen(metadata: tableMetadata, selector: String) {
  121. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  122. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "selector": selector, "errorCode": 0, "errorDescription": "" ])
  123. } else {
  124. NCNetworking.shared.download(metadata: metadata, selector: selector) { (_) in }
  125. }
  126. }
  127. //MARK: - Upload
  128. @objc func uploadedFile(_ notification: NSNotification) {
  129. if let userInfo = notification.userInfo as NSDictionary? {
  130. if let ocId = userInfo["ocId"] as? String, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  131. if metadata.account == appDelegate.account {
  132. if errorCode != 0 {
  133. if errorCode != -999 && errorCode != 401 && errorDescription != "" {
  134. NCContentPresenter.shared.messageNotification("_upload_file_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }