Browse Source

viewerFile

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 1 năm trước cách đây
mục cha
commit
44f9158f8e

+ 3 - 54
iOSClient/Activity/NCActivityTableViewCell.swift

@@ -53,7 +53,7 @@ class NCActivityTableViewCell: UITableViewCell, NCCellProtocol {
     var idActivity: Int = 0
     var activityPreviews: [tableActivityPreview] = []
     var didSelectItemEnable: Bool = true
-    var viewController: UIViewController?
+    var viewController = UIViewController()
 
     var fileAvatarImageView: UIImageView? {
         get { return avatar }
@@ -74,7 +74,7 @@ class NCActivityTableViewCell: UITableViewCell, NCCellProtocol {
 
     @objc func tapAvatarImage() {
         guard let fileUser = fileUser else { return }
-        viewController?.showProfileMenu(userId: fileUser)
+        viewController.showProfileMenu(userId: fileUser)
     }
 }
 
@@ -124,58 +124,7 @@ extension NCActivityTableViewCell: UICollectionViewDelegate {
                 return
             }
 
-            if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "fileId == %@", activitySubjectRich.id)) {
-                if let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView) {
-                    do {
-                        let attr = try FileManager.default.attributesOfItem(atPath: filePath)
-                        let fileSize = attr[FileAttributeKey.size] as! UInt64
-                        if fileSize > 0 {
-                            if let viewController = self.viewController {
-                                NCViewer.shared.view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: cell?.imageView.image)
-                            }
-                            return
-                        }
-                    } catch {
-                        print("Error: \(error)")
-                    }
-                }
-            }
-
-            let hud = JGProgressHUD()
-            hud.indicatorView = JGProgressHUDRingIndicatorView()
-            if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView {
-                indicatorView.ringWidth = 1.5
-            }
-            guard let view = appDelegate.window?.rootViewController?.view else { return }
-            hud.show(in: view)
-
-            NextcloudKit.shared.getFileFromFileId(fileId: String(activityPreview.fileId)) { account, file, data, error in
-                if let file = file {
-                    let isDirectoryE2EE = NCUtility.shared.isDirectoryE2EE(file: file)
-                    let metadata = NCManageDatabase.shared.convertFileToMetadata(file, isDirectoryE2EE: isDirectoryE2EE)
-                    NCManageDatabase.shared.addMetadata(metadata)
-
-                    let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
-                    let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
-
-                    NextcloudKit.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, requestHandler: { _ in
-                    }, taskHandler: { _ in
-                    }, progressHandler: { progress in
-                        hud.progress = Float(progress.fractionCompleted)
-                    }) { account, _, _, _, _, _, error in
-                        hud.dismiss()
-                        if account == self.appDelegate.account && error == .success {
-                            NCManageDatabase.shared.addLocalFile(metadata: metadata)
-                            if let viewController = self.viewController {
-                                NCViewer.shared.view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: cell?.imageView.image)
-                            }
-                        }
-                    }
-                } else {
-                    hud.dismiss()
-                    NCContentPresenter.shared.showError(error: error)
-                }
-            }
+            NCActionCenter.shared.viewerFile(account: appDelegate.account, fileId: activitySubjectRich.id, viewController: viewController)
         }
     }
 }

+ 71 - 0
iOSClient/Main/NCActionCenter.swift

@@ -27,6 +27,7 @@ import Queuer
 import JGProgressHUD
 import SVGKit
 import Photos
+import Alamofire
 
 class NCActionCenter: NSObject, UIDocumentInteractionControllerDelegate, NCSelectDelegate {
     public static let shared: NCActionCenter = {
@@ -170,6 +171,76 @@ class NCActionCenter: NSObject, UIDocumentInteractionControllerDelegate, NCSelec
         }
     }
 
+    func viewerFile(account: String, fileId: String, viewController: UIViewController) {
+
+        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate, let hudView = appDelegate.window?.rootViewController?.view else { return }
+        var downloadRequest: DownloadRequest?
+
+        if let metadata = NCManageDatabase.shared.getMetadataFromFileId(fileId) {
+            if let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView) {
+                do {
+                    let attr = try FileManager.default.attributesOfItem(atPath: filePath)
+                    let fileSize = attr[FileAttributeKey.size] as? UInt64 ?? 0
+                    if fileSize > 0 {
+                        NCViewer.shared.view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: nil)
+                        return
+                    }
+                } catch {
+                    print("Error: \(error)")
+                }
+            }
+        }
+
+        let hud = JGProgressHUD()
+        hud.indicatorView = JGProgressHUDRingIndicatorView()
+        if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView {
+            indicatorView.ringWidth = 1.5
+        }
+        hud.tapOnHUDViewBlock = { _ in
+            if let request = downloadRequest {
+                request.cancel()
+            }
+        }
+        hud.show(in: hudView)
+
+        NextcloudKit.shared.getFileFromFileId(fileId: fileId) { account, file, _, error in
+
+            if error != .success {
+                hud.dismiss()
+                NCContentPresenter.shared.showError(error: error)
+            } else if let file = file {
+
+                let isDirectoryE2EE = NCUtility.shared.isDirectoryE2EE(file: file)
+                let metadata = NCManageDatabase.shared.convertFileToMetadata(file, isDirectoryE2EE: isDirectoryE2EE)
+                NCManageDatabase.shared.addMetadata(metadata)
+
+                let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
+                let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
+
+                if metadata.isMovie {
+                    NCViewer.shared.view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: nil)
+                } else {
+                    NextcloudKit.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, requestHandler: { request in
+                        downloadRequest = request
+                    }, taskHandler: { _ in
+                    }, progressHandler: { progress in
+                        hud.progress = Float(progress.fractionCompleted)
+                    }) { accountDownload, _, _, _, _, _, error in
+                        hud.dismiss()
+                        if account == accountDownload && error == .success {
+                            NCManageDatabase.shared.addLocalFile(metadata: metadata)
+                            NCViewer.shared.view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: nil)
+                        }
+                    }
+                }
+            } else {
+                hud.dismiss()
+                let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_file_not_found_")
+                NCContentPresenter.shared.showError(error: error)
+            }
+        }
+    }
+
     // MARK: - Upload
 
     @objc func uploadedFile(_ notification: NSNotification) {

+ 1 - 48
iOSClient/Notification/NCNotification.swift

@@ -110,54 +110,7 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate, NCEmpty
         let notification = notifications[indexPath.row]
 
         if notification.app == "files_sharing" {
-            if let metadata = NCManageDatabase.shared.getMetadataFromFileId(notification.objectId) {
-                if let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView) {
-                    do {
-                        let attr = try FileManager.default.attributesOfItem(atPath: filePath)
-                        let fileSize = attr[FileAttributeKey.size] as! UInt64
-                        if fileSize > 0 {
-                            NCViewer.shared.view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: nil)
-                            return
-                        }
-                    } catch {
-                        print("Error: \(error)")
-                    }
-                }
-            }
-
-            let hud = JGProgressHUD()
-            hud.indicatorView = JGProgressHUDRingIndicatorView()
-            if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView {
-                indicatorView.ringWidth = 1.5
-            }
-            guard let view = appDelegate.window?.rootViewController?.view else { return }
-            hud.show(in: view)
-
-            NextcloudKit.shared.getFileFromFileId(fileId: notification.objectId) { account, file, data, error in
-                if let file = file {
-                    let isDirectoryE2EE = NCUtility.shared.isDirectoryE2EE(file: file)
-                    let metadata = NCManageDatabase.shared.convertFileToMetadata(file, isDirectoryE2EE: isDirectoryE2EE)
-                    NCManageDatabase.shared.addMetadata(metadata)
-
-                    let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
-                    let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
-
-                    NextcloudKit.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, requestHandler: { _ in
-                    }, taskHandler: { _ in
-                    }, progressHandler: { progress in
-                        hud.progress = Float(progress.fractionCompleted)
-                    }) { account, _, _, _, _, _, error in
-                        hud.dismiss()
-                        if account == self.appDelegate.account && error == .success {
-                            NCManageDatabase.shared.addLocalFile(metadata: metadata)
-                            NCViewer.shared.view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: nil)
-                        }
-                    }
-                } else {
-                    hud.dismiss()
-                    NCContentPresenter.shared.showError(error: error)
-                }
-            }
+            NCActionCenter.shared.viewerFile(account: appDelegate.account, fileId: notification.objectId, viewController: self)
         } else {
             NCApplicationHandle().didSelectNotification(notification, viewController: self)
         }