marinofaggiana 4 years ago
parent
commit
b603f11e28

+ 24 - 26
File Provider Extension/FileProviderData.swift

@@ -59,56 +59,54 @@ class fileProviderData: NSObject {
     
     // MARK: - 
     
-    func setupAccount(domain: String?, providerExtension: NSFileProviderExtension) -> Bool {
-        
-        var foundAccount: Bool = false
-        
+    func setupAccount(domain: String?, providerExtension: NSFileProviderExtension) -> tableAccount? {
+                
         if CCUtility.getDisableFilesApp() || NCBrandOptions.sharedInstance.disable_openin_file {
-            return false
+            return nil
         }
                 
         // NO DOMAIN -> Set default account
         if domain == nil {
             
-            guard let tableAccount = NCManageDatabase.sharedInstance.getAccountActive() else { return false }
-            let serverVersionMajor = NCManageDatabase.sharedInstance.getCapabilitiesServerInt(account: tableAccount.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
-            let webDav = NCUtility.shared.getWebDAV(account: tableAccount.account)
+            guard let accountActive = NCManageDatabase.sharedInstance.getAccountActive() else { return nil }
+            let serverVersionMajor = NCManageDatabase.sharedInstance.getCapabilitiesServerInt(account: accountActive.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
+            let webDav = NCUtility.shared.getWebDAV(account: accountActive.account)
             
-            account = tableAccount.account
-            accountUrlBase = tableAccount.urlBase
-            homeServerUrl = NCUtility.shared.getHomeServer(urlBase: tableAccount.urlBase, account: tableAccount.account)
+            account = accountActive.account
+            accountUrlBase = accountActive.urlBase
+            homeServerUrl = NCUtility.shared.getHomeServer(urlBase: accountActive.urlBase, account: accountActive.account)
                         
-            NCCommunicationCommon.shared.setup(account: tableAccount.account, user: tableAccount.user, userId: tableAccount.userID, password: CCUtility.getPassword(tableAccount.account), urlBase: tableAccount.urlBase, userAgent: CCUtility.getUserAgent(), webDav: webDav, dav: nil, nextcloudVersion: serverVersionMajor, delegate: NCNetworking.shared)
+            NCCommunicationCommon.shared.setup(account: accountActive.account, user: accountActive.user, userId: accountActive.userID, password: CCUtility.getPassword(accountActive.account), urlBase: accountActive.urlBase, userAgent: CCUtility.getUserAgent(), webDav: webDav, dav: nil, nextcloudVersion: serverVersionMajor, delegate: NCNetworking.shared)
             NCNetworking.shared.delegate = providerExtension as? NCNetworkingDelegate
             
-            return true
+            return tableAccount.init(value: accountActive)
         }
         
         // DOMAIN
-        let tableAccounts = NCManageDatabase.sharedInstance.getAllAccount()
-        if tableAccounts.count == 0 { return false }
+        let accounts = NCManageDatabase.sharedInstance.getAllAccount()
+        if accounts.count == 0 { return nil }
         
-        for tableAccount in tableAccounts {
-            guard let url = NSURL(string: tableAccount.urlBase) else { continue }
+        for accountActive in accounts {
+            guard let url = NSURL(string: accountActive.urlBase) else { continue }
             guard let host = url.host else { continue }
-            let accountDomain = tableAccount.userID + " (" + host + ")"
+            let accountDomain = accountActive.userID + " (" + host + ")"
             if accountDomain == domain {
                 
-                let serverVersionMajor = NCManageDatabase.sharedInstance.getCapabilitiesServerInt(account: tableAccount.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
-                let webDav = NCUtility.shared.getWebDAV(account: tableAccount.account)
+                let serverVersionMajor = NCManageDatabase.sharedInstance.getCapabilitiesServerInt(account: accountActive.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
+                let webDav = NCUtility.shared.getWebDAV(account: accountActive.account)
                 
-                account = tableAccount.account
-                accountUrlBase = tableAccount.urlBase
-                homeServerUrl = NCUtility.shared.getHomeServer(urlBase: tableAccount.urlBase, account: tableAccount.account)
+                account = accountActive.account
+                accountUrlBase = accountActive.urlBase
+                homeServerUrl = NCUtility.shared.getHomeServer(urlBase: accountActive.urlBase, account: accountActive.account)
                 
-                NCCommunicationCommon.shared.setup(account: tableAccount.account, user: tableAccount.user, userId: tableAccount.userID, password: CCUtility.getPassword(tableAccount.account), urlBase: tableAccount.urlBase, userAgent: CCUtility.getUserAgent(), webDav: webDav, dav: nil, nextcloudVersion: serverVersionMajor, delegate: NCNetworking.shared)
+                NCCommunicationCommon.shared.setup(account: accountActive.account, user: accountActive.user, userId: accountActive.userID, password: CCUtility.getPassword(accountActive.account), urlBase: accountActive.urlBase, userAgent: CCUtility.getUserAgent(), webDav: webDav, dav: nil, nextcloudVersion: serverVersionMajor, delegate: NCNetworking.shared)
                 NCNetworking.shared.delegate = providerExtension as? NCNetworkingDelegate
 
-                foundAccount = true
+                return tableAccount.init(value: accountActive)
             }
         }
         
-        return foundAccount
+        return nil
     }
         
     // MARK: -

+ 5 - 5
File Provider Extension/FileProviderExtension+Actions.swift

@@ -37,7 +37,7 @@ extension FileProviderExtension {
         let serverUrlFileName = tableDirectory.serverUrl + "/" + directoryName
         
         // NCCommunication
-        if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == false {
+        if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == nil {
             completionHandler(nil, NSFileProviderError(.noSuchItem))
             return
         }
@@ -93,7 +93,7 @@ extension FileProviderExtension {
         let fileName = metadata.fileName;
         
         // NCCommunication
-        if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == false {
+        if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == nil {
             completionHandler(NSFileProviderError(.noSuchItem))
             return
         }
@@ -148,7 +148,7 @@ extension FileProviderExtension {
         let fileNameTo = serverUrlTo + "/" + itemFrom.filename
         
         // NCCommunication
-        if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == false {
+        if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == nil {
             completionHandler(nil, NSFileProviderError(.noSuchItem))
             return
         }
@@ -195,7 +195,7 @@ extension FileProviderExtension {
         let ocId = metadata.ocId
         
         // NCCommunication
-        if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == false {
+        if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == nil {
             completionHandler(nil, NSFileProviderError(.noSuchItem))
             return
         }
@@ -267,7 +267,7 @@ extension FileProviderExtension {
             let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
             
             // NCCommunication
-            if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == false {
+            if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == nil {
                 completionHandler(nil, NSFileProviderError(.noSuchItem))
                 return
             }

+ 1 - 1
File Provider Extension/FileProviderExtension+Thumbnail.swift

@@ -45,7 +45,7 @@ extension FileProviderExtension {
                 let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)!
                     
                 // NCCommunication
-                if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == false {
+                if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == nil {
                     completionHandler(nil)
                 }
                 NCCommunication.shared.getPreview(fileNamePath: fileNamePath, widthPreview: Int(k_sizeIcon), heightPreview: Int(k_sizeIcon)) { (account, data, errorCode, errorDescription) in

+ 8 - 2
File Provider Extension/FileProviderExtension.swift

@@ -71,7 +71,7 @@ class FileProviderExtension: NSFileProviderExtension {
         var maybeEnumerator: NSFileProviderEnumerator? = nil
         
         if (containerItemIdentifier != NSFileProviderItemIdentifier.workingSet) {
-            if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == false {
+            if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == nil {
                 throw NSError(domain: NSFileProviderErrorDomain, code: NSFileProviderError.notAuthenticated.rawValue, userInfo:[:])
             }
         }
@@ -213,7 +213,7 @@ class FileProviderExtension: NSFileProviderExtension {
         fileProviderData.sharedInstance.signalEnumerator(ocId: metadata.ocId, update: true)
         
         // NCCommunication
-        if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == false {
+        if fileProviderData.sharedInstance.setupAccount(domain: domain?.identifier.rawValue, providerExtension: self) == nil {
             completionHandler(NSFileProviderError(.noSuchItem))
             return
         }
@@ -369,6 +369,12 @@ class FileProviderExtension: NSFileProviderExtension {
                 let serverUrlFileName = tableDirectory.serverUrl + "/" + fileName
                 let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(ocIdTemp, fileNameView: fileName)!
                 
+                // NCCommunication
+                let account = fileProviderData.sharedInstance.setupAccount(domain: self.domain?.identifier.rawValue, providerExtension: self)
+                if account == nil {
+                    completionHandler(nil, NSFileProviderError(.noSuchItem))
+                    return
+                }
                 if let task = NCCommunicationBackground.shared.upload(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, dateCreationFile: nil, dateModificationFile: nil, description: ocIdTemp, session: NCNetworking.shared.sessionManagerBackgroundExtension) {
                     
                     self.outstandingSessionTasks[URL(fileURLWithPath: fileNameLocalPath)] = task as URLSessionTask