Pārlūkot izejas kodu

fix(ios): sync encryption and metadata

Andelf 3 gadi atpakaļ
vecāks
revīzija
5db5692340

+ 9 - 3
ios/App/App/FileSync/FileSync.swift

@@ -74,7 +74,7 @@ func maybeEncrypt(_ plaindata: Data) -> Data! {
     }
     if let publicKey = ENCRYPTION_PUBLIC_KEY {
         // use armor = false, for smaller size
-        if let cipherdata = AgeEncryption.encryptWithPassphrase(plaindata, publicKey, armor: false) {
+        if let cipherdata = AgeEncryption.encryptWithX25519(plaindata, publicKey, armor: true) {
             return cipherdata
         }
         return nil // encryption fail
@@ -362,7 +362,7 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
             }
 
             // 2. upload_temp_file
-            client.uploadTempFiles(files, credentials: credentials!) { (uploadedFileKeyDict, error) in
+            client.uploadTempFiles(files, credentials: credentials!) { (uploadedFileKeyDict, fileMd5Dict, error) in
                 guard error == nil else {
                     self.debugNotification(["event": "upload:error", "data": ["message": "error while uploading temp files: \(error!)"]])
                     call.reject("error(uploadTempFiles): \(error!)")
@@ -374,7 +374,13 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
                     call.reject("no file to update")
                     return
                 }
-                client.updateFiles(uploadedFileKeyDict) { (txid, error) in
+
+                var uploadedFileKeyMd5Dict: [String: [String]] = [:]
+
+                for (filePath, fileKey) in uploadedFileKeyDict {
+                    uploadedFileKeyMd5Dict[filePath] = [fileKey, fileMd5Dict[filePath]!]
+                }
+                client.updateFiles(uploadedFileKeyMd5Dict) { (txid, error) in
                     guard error == nil else {
                         self.debugNotification(["event": "upload:error", "data": ["message": "error while updating files: \(error!)"]])
                         call.reject("error updateFiles: \(error!)")

+ 8 - 5
ios/App/App/FileSync/SyncClient.swift

@@ -148,7 +148,8 @@ public class SyncClient {
     }
     
     // (txid, error)
-    public func updateFiles(_ fileKeyDict: [String: String], completionHandler: @escaping  (Int?, Error?) -> Void) {
+    // filePath => [S3Key, md5]
+    public func updateFiles(_ fileKeyDict: [String: [String]], completionHandler: @escaping  (Int?, Error?) -> Void) {
         let url = URL_BASE.appendingPathComponent("update_files")
         
         var request = URLRequest(url: url)
@@ -158,7 +159,7 @@ public class SyncClient {
         
         let payload = [
             "GraphUUID": self.graphUUID ?? "",
-            "Files": Dictionary(uniqueKeysWithValues: fileKeyDict.map { ($0, $1) }) as [String: String] as Any,
+            "Files": Dictionary(uniqueKeysWithValues: fileKeyDict.map { ($0, $1) }) as [String: [String]] as Any,
             "TXId": self.txid,
         ] as [String : Any]
         let bodyData = try? JSONSerialization.data(
@@ -252,7 +253,7 @@ public class SyncClient {
     }
     
     // [filePath, Key]
-    public func uploadTempFiles(_ files: [String: URL], credentials: S3Credential, completionHandler: @escaping ([String: String], Error?) -> Void) {
+    public func uploadTempFiles(_ files: [String: URL], credentials: S3Credential, completionHandler: @escaping ([String: String], [String: String], Error?) -> Void) {
         let credentialsProvider = AWSBasicSessionCredentialsProvider(
             accessKey: credentials.AccessKeyId, secretKey: credentials.SecretKey, sessionToken: credentials.SessionToken)
         let configuration = AWSServiceConfiguration(region: .USEast2, credentialsProvider: credentialsProvider)
@@ -280,6 +281,7 @@ public class SyncClient {
         let group = DispatchGroup()
         var keyFileDict: [String: String] = [:]
         var fileKeyDict: [String: String] = [:]
+        var fileMd5Dict: [String: String] = [:]
         
         let uploadCompletionHandler = { (task: AWSS3TransferUtilityUploadTask, error: Error?) -> Void in
             // ignore any errors in first level of handler
@@ -310,10 +312,11 @@ public class SyncClient {
             let key = "\(self.s3prefix!)/ios\(randFileName)"
 
             keyFileDict[key] = filePath
+            fileMd5Dict[filePath] = rawData.MD5
             transferUtility?.uploadData(encryptedRawDat, key: key, contentType: "application/octet-stream", expression: uploadExpression, completionHandler: uploadCompletionHandler)
                 .continueWith(block: { (task) in
                     if let error = task.error {
-                        completionHandler([:], error)
+                        completionHandler([:], [:], error)
                     }
                     return nil
                 })
@@ -321,7 +324,7 @@ public class SyncClient {
         
         group.notify(queue: .main) {
             AWSS3TransferUtility.remove(forKey: transferKey)
-            completionHandler(fileKeyDict, nil)
+            completionHandler(fileKeyDict, fileMd5Dict, nil)
         }
     }
 }