|
@@ -10,28 +10,80 @@ import Foundation
|
|
|
import AWSMobileClient
|
|
import AWSMobileClient
|
|
|
import CryptoKit
|
|
import CryptoKit
|
|
|
|
|
|
|
|
-// MARK: Global variables
|
|
|
|
|
|
|
+
|
|
|
|
|
+// MARK: Global variable
|
|
|
|
|
|
|
|
// Defualts to dev
|
|
// Defualts to dev
|
|
|
-var URL_BASE = URL(string: "https://api.logseq.com/file-sync/")!
|
|
|
|
|
|
|
+var URL_BASE = URL(string: "https://api-dev.logseq.com/file-sync/")!
|
|
|
var BUCKET: String = "logseq-file-sync-bucket"
|
|
var BUCKET: String = "logseq-file-sync-bucket"
|
|
|
var REGION: String = "us-east-2"
|
|
var REGION: String = "us-east-2"
|
|
|
|
|
|
|
|
|
|
+var ENCRYPTION_SECRET_KEY: String? = nil
|
|
|
|
|
+var ENCRYPTION_PUBLIC_KEY: String? = nil
|
|
|
|
|
+var FNAME_ENCRYPTION_KEY: Data? = nil
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+// MARK: Helpers
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+@inline(__always) func fnameEncryptionEnabled() -> Bool {
|
|
|
|
|
+ guard let _ = FNAME_ENCRYPTION_KEY else {
|
|
|
|
|
+ return false
|
|
|
|
|
+ }
|
|
|
|
|
+ return true
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// MARK: encryption helper
|
|
|
|
|
+
|
|
|
|
|
+func maybeEncrypt(_ plaindata: Data) -> Data! {
|
|
|
|
|
+ // avoid encryption twice
|
|
|
|
|
+ if plaindata.starts(with: "-----BEGIN AGE ENCRYPTED FILE-----".data(using: .utf8)!) ||
|
|
|
|
|
+ plaindata.starts(with: "age-encryption.org/v1\n".data(using: .utf8)!) {
|
|
|
|
|
+ return plaindata
|
|
|
|
|
+ }
|
|
|
|
|
+ if let publicKey = ENCRYPTION_PUBLIC_KEY {
|
|
|
|
|
+ // use armor = false, for smaller size
|
|
|
|
|
+ if let cipherdata = AgeEncryption.encryptWithX25519(plaindata, publicKey, armor: true) {
|
|
|
|
|
+ return cipherdata
|
|
|
|
|
+ }
|
|
|
|
|
+ return nil // encryption fail
|
|
|
|
|
+ }
|
|
|
|
|
+ return plaindata
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func maybeDecrypt(_ cipherdata: Data) -> Data! {
|
|
|
|
|
+ if let secretKey = ENCRYPTION_SECRET_KEY {
|
|
|
|
|
+ if cipherdata.starts(with: "-----BEGIN AGE ENCRYPTED FILE-----".data(using: .utf8)!) ||
|
|
|
|
|
+ cipherdata.starts(with: "age-encryption.org/v1\n".data(using: .utf8)!) {
|
|
|
|
|
+ if let plaindata = AgeEncryption.decryptWithX25519(cipherdata, secretKey) {
|
|
|
|
|
+ return plaindata
|
|
|
|
|
+ }
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ // not an encrypted file
|
|
|
|
|
+ return cipherdata
|
|
|
|
|
+ }
|
|
|
|
|
+ return cipherdata
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// MARK: Metadata type
|
|
|
|
|
|
|
|
public struct SyncMetadata: CustomStringConvertible, Equatable {
|
|
public struct SyncMetadata: CustomStringConvertible, Equatable {
|
|
|
var md5: String
|
|
var md5: String
|
|
|
var size: Int
|
|
var size: Int
|
|
|
|
|
+ var ctime: Int64
|
|
|
|
|
|
|
|
public init?(of fileURL: URL) {
|
|
public init?(of fileURL: URL) {
|
|
|
do {
|
|
do {
|
|
|
- let fileAttributes = try fileURL.resourceValues(forKeys:[.isRegularFileKey, .fileSizeKey])
|
|
|
|
|
|
|
+ let fileAttributes = try fileURL.resourceValues(forKeys:[.isRegularFileKey, .fileSizeKey, .contentModificationDateKey])
|
|
|
guard fileAttributes.isRegularFile! else {
|
|
guard fileAttributes.isRegularFile! else {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
size = fileAttributes.fileSize ?? 0
|
|
size = fileAttributes.fileSize ?? 0
|
|
|
-
|
|
|
|
|
- // incremental MD5sum
|
|
|
|
|
- let bufferSize = 1024 * 1024
|
|
|
|
|
|
|
+ ctime = Int64((fileAttributes.contentModificationDate?.timeIntervalSince1970 ?? 0.0) * 1000)
|
|
|
|
|
+
|
|
|
|
|
+ // incremental MD5 checksum
|
|
|
|
|
+ let bufferSize = 512 * 1024
|
|
|
let file = try FileHandle(forReadingFrom: fileURL)
|
|
let file = try FileHandle(forReadingFrom: fileURL)
|
|
|
defer {
|
|
defer {
|
|
|
file.closeFile()
|
|
file.closeFile()
|
|
@@ -46,7 +98,7 @@ public struct SyncMetadata: CustomStringConvertible, Equatable {
|
|
|
return false // eof
|
|
return false // eof
|
|
|
}
|
|
}
|
|
|
}) {}
|
|
}) {}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
let computed = ctx.finalize()
|
|
let computed = ctx.finalize()
|
|
|
md5 = computed.map { String(format: "%02hhx", $0) }.joined()
|
|
md5 = computed.map { String(format: "%02hhx", $0) }.joined()
|
|
|
} catch {
|
|
} catch {
|
|
@@ -59,13 +111,13 @@ public struct SyncMetadata: CustomStringConvertible, Equatable {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
// MARK: FileSync Plugin
|
|
// MARK: FileSync Plugin
|
|
|
|
|
|
|
|
@objc(FileSync)
|
|
@objc(FileSync)
|
|
|
public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
override public func load() {
|
|
override public func load() {
|
|
|
print("debug File sync iOS plugin loaded!")
|
|
print("debug File sync iOS plugin loaded!")
|
|
|
|
|
+
|
|
|
AWSMobileClient.default().initialize { (userState, error) in
|
|
AWSMobileClient.default().initialize { (userState, error) in
|
|
|
guard error == nil else {
|
|
guard error == nil else {
|
|
|
print("error initializing AWSMobileClient. Error: \(error!.localizedDescription)")
|
|
print("error initializing AWSMobileClient. Error: \(error!.localizedDescription)")
|
|
@@ -73,35 +125,99 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// NOTE: for debug, or an activity indicator
|
|
// NOTE: for debug, or an activity indicator
|
|
|
public func debugNotification(_ message: [String: Any]) {
|
|
public func debugNotification(_ message: [String: Any]) {
|
|
|
self.notifyListeners("debug", data: message)
|
|
self.notifyListeners("debug", data: message)
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ @objc func keygen(_ call: CAPPluginCall) {
|
|
|
|
|
+ let (secretKey, publicKey) = AgeEncryption.keygen()
|
|
|
|
|
+ call.resolve(["secretKey": secretKey,
|
|
|
|
|
+ "publicKey": publicKey])
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @objc func setKey(_ call: CAPPluginCall) {
|
|
|
|
|
+ let secretKey = call.getString("secretKey")
|
|
|
|
|
+ let publicKey = call.getString("publicKey")
|
|
|
|
|
+ if secretKey == nil && publicKey == nil {
|
|
|
|
|
+ ENCRYPTION_SECRET_KEY = nil
|
|
|
|
|
+ ENCRYPTION_PUBLIC_KEY = nil
|
|
|
|
|
+ FNAME_ENCRYPTION_KEY = nil
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ guard let secretKey = secretKey, let publicKey = publicKey else {
|
|
|
|
|
+ call.reject("both secretKey and publicKey should be provided")
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ ENCRYPTION_SECRET_KEY = secretKey
|
|
|
|
|
+ ENCRYPTION_PUBLIC_KEY = publicKey
|
|
|
|
|
+ FNAME_ENCRYPTION_KEY = AgeEncryption.toRawX25519Key(secretKey)
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@objc func setEnv(_ call: CAPPluginCall) {
|
|
@objc func setEnv(_ call: CAPPluginCall) {
|
|
|
guard let env = call.getString("env") else {
|
|
guard let env = call.getString("env") else {
|
|
|
call.reject("required parameter: env")
|
|
call.reject("required parameter: env")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
+ self.setKey(call)
|
|
|
|
|
+
|
|
|
switch env {
|
|
switch env {
|
|
|
case "production", "product", "prod":
|
|
case "production", "product", "prod":
|
|
|
- URL_BASE = URL(string: "https://api-prod.logseq.com/file-sync/")!
|
|
|
|
|
|
|
+ URL_BASE = URL(string: "https://api.logseq.com/file-sync/")!
|
|
|
BUCKET = "logseq-file-sync-bucket-prod"
|
|
BUCKET = "logseq-file-sync-bucket-prod"
|
|
|
REGION = "us-east-1"
|
|
REGION = "us-east-1"
|
|
|
case "development", "develop", "dev":
|
|
case "development", "develop", "dev":
|
|
|
- URL_BASE = URL(string: "https://api.logseq.com/file-sync/")!
|
|
|
|
|
|
|
+ URL_BASE = URL(string: "https://api-dev.logseq.com/file-sync/")!
|
|
|
BUCKET = "logseq-file-sync-bucket"
|
|
BUCKET = "logseq-file-sync-bucket"
|
|
|
REGION = "us-east-2"
|
|
REGION = "us-east-2"
|
|
|
default:
|
|
default:
|
|
|
call.reject("invalid env: \(env)")
|
|
call.reject("invalid env: \(env)")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
self.debugNotification(["event": "setenv:\(env)"])
|
|
self.debugNotification(["event": "setenv:\(env)"])
|
|
|
call.resolve(["ok": true])
|
|
call.resolve(["ok": true])
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ @objc func encryptFnames(_ call: CAPPluginCall) {
|
|
|
|
|
+ guard fnameEncryptionEnabled() else {
|
|
|
|
|
+ call.reject("fname encryption key not set")
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ guard var fnames = call.getArray("filePaths") as? [String] else {
|
|
|
|
|
+ call.reject("required parameters: filePaths")
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let nFiles = fnames.count
|
|
|
|
|
+ fnames = fnames.compactMap { $0.removingPercentEncoding!.fnameEncrypt(rawKey: FNAME_ENCRYPTION_KEY!) }
|
|
|
|
|
+ if fnames.count != nFiles {
|
|
|
|
|
+ call.reject("cannot encrypt \(nFiles - fnames.count) file names")
|
|
|
|
|
+ }
|
|
|
|
|
+ call.resolve(["value": fnames])
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @objc func decryptFnames(_ call: CAPPluginCall) {
|
|
|
|
|
+ guard fnameEncryptionEnabled() else {
|
|
|
|
|
+ call.reject("fname encryption key not set")
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ guard var fnames = call.getArray("filePaths") as? [String] else {
|
|
|
|
|
+ call.reject("required parameters: filePaths")
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ let nFiles = fnames.count
|
|
|
|
|
+ fnames = fnames.compactMap { $0.fnameDecrypt(rawKey: FNAME_ENCRYPTION_KEY!)?.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) }
|
|
|
|
|
+ if fnames.count != nFiles {
|
|
|
|
|
+ call.reject("cannot decrypt \(nFiles - fnames.count) file names")
|
|
|
|
|
+ }
|
|
|
|
|
+ call.resolve(["value": fnames])
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@objc func getLocalFilesMeta(_ call: CAPPluginCall) {
|
|
@objc func getLocalFilesMeta(_ call: CAPPluginCall) {
|
|
|
|
|
+ // filePaths are url encoded
|
|
|
guard let basePath = call.getString("basePath"),
|
|
guard let basePath = call.getString("basePath"),
|
|
|
let filePaths = call.getArray("filePaths") as? [String] else {
|
|
let filePaths = call.getArray("filePaths") as? [String] else {
|
|
|
call.reject("required paremeters: basePath, filePaths")
|
|
call.reject("required paremeters: basePath, filePaths")
|
|
@@ -111,34 +227,47 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
call.reject("invalid basePath")
|
|
call.reject("invalid basePath")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
var fileMetadataDict: [String: [String: Any]] = [:]
|
|
var fileMetadataDict: [String: [String: Any]] = [:]
|
|
|
- for filePath in filePaths {
|
|
|
|
|
|
|
+ for percentFilePath in filePaths {
|
|
|
|
|
+ let filePath = percentFilePath.removingPercentEncoding!
|
|
|
let url = baseURL.appendingPathComponent(filePath)
|
|
let url = baseURL.appendingPathComponent(filePath)
|
|
|
if let meta = SyncMetadata(of: url) {
|
|
if let meta = SyncMetadata(of: url) {
|
|
|
- fileMetadataDict[filePath] = ["md5": meta.md5,
|
|
|
|
|
- "size": meta.size]
|
|
|
|
|
|
|
+ var metaObj: [String: Any] = ["md5": meta.md5,
|
|
|
|
|
+ "size": meta.size,
|
|
|
|
|
+ "ctime": meta.ctime]
|
|
|
|
|
+ if fnameEncryptionEnabled() {
|
|
|
|
|
+ metaObj["encryptedFname"] = filePath.fnameEncrypt(rawKey: FNAME_ENCRYPTION_KEY!)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fileMetadataDict[percentFilePath] = metaObj
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
call.resolve(["result": fileMetadataDict])
|
|
call.resolve(["result": fileMetadataDict])
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
@objc func getLocalAllFilesMeta(_ call: CAPPluginCall) {
|
|
@objc func getLocalAllFilesMeta(_ call: CAPPluginCall) {
|
|
|
guard let basePath = call.getString("basePath"),
|
|
guard let basePath = call.getString("basePath"),
|
|
|
let baseURL = URL(string: basePath) else {
|
|
let baseURL = URL(string: basePath) else {
|
|
|
call.reject("invalid basePath")
|
|
call.reject("invalid basePath")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
var fileMetadataDict: [String: [String: Any]] = [:]
|
|
var fileMetadataDict: [String: [String: Any]] = [:]
|
|
|
if let enumerator = FileManager.default.enumerator(at: baseURL, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsPackageDescendants, .skipsHiddenFiles]) {
|
|
if let enumerator = FileManager.default.enumerator(at: baseURL, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsPackageDescendants, .skipsHiddenFiles]) {
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
for case let fileURL as URL in enumerator {
|
|
for case let fileURL as URL in enumerator {
|
|
|
if !fileURL.isSkipped() {
|
|
if !fileURL.isSkipped() {
|
|
|
if let meta = SyncMetadata(of: fileURL) {
|
|
if let meta = SyncMetadata(of: fileURL) {
|
|
|
- fileMetadataDict[fileURL.relativePath(from: baseURL)!] = ["md5": meta.md5,
|
|
|
|
|
- "size": meta.size]
|
|
|
|
|
|
|
+ let filePath = fileURL.relativePath(from: baseURL)!
|
|
|
|
|
+ var metaObj: [String: Any] = ["md5": meta.md5,
|
|
|
|
|
+ "size": meta.size,
|
|
|
|
|
+ "ctime": meta.ctime]
|
|
|
|
|
+ if fnameEncryptionEnabled() {
|
|
|
|
|
+ metaObj["encryptedFname"] = filePath.fnameEncrypt(rawKey: FNAME_ENCRYPTION_KEY!)
|
|
|
|
|
+ }
|
|
|
|
|
+ fileMetadataDict[filePath.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!] = metaObj
|
|
|
}
|
|
}
|
|
|
} else if fileURL.isICloudPlaceholder() {
|
|
} else if fileURL.isICloudPlaceholder() {
|
|
|
try? FileManager.default.startDownloadingUbiquitousItem(at: fileURL)
|
|
try? FileManager.default.startDownloadingUbiquitousItem(at: fileURL)
|
|
@@ -147,8 +276,8 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
}
|
|
}
|
|
|
call.resolve(["result": fileMetadataDict])
|
|
call.resolve(["result": fileMetadataDict])
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
@objc func renameLocalFile(_ call: CAPPluginCall) {
|
|
@objc func renameLocalFile(_ call: CAPPluginCall) {
|
|
|
guard let basePath = call.getString("basePath"),
|
|
guard let basePath = call.getString("basePath"),
|
|
|
let baseURL = URL(string: basePath) else {
|
|
let baseURL = URL(string: basePath) else {
|
|
@@ -163,10 +292,10 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
call.reject("invalid to file")
|
|
call.reject("invalid to file")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- let fromUrl = baseURL.appendingPathComponent(from)
|
|
|
|
|
- let toUrl = baseURL.appendingPathComponent(to)
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ let fromUrl = baseURL.appendingPathComponent(from.removingPercentEncoding!)
|
|
|
|
|
+ let toUrl = baseURL.appendingPathComponent(to.removingPercentEncoding!)
|
|
|
|
|
+
|
|
|
do {
|
|
do {
|
|
|
try FileManager.default.moveItem(at: fromUrl, to: toUrl)
|
|
try FileManager.default.moveItem(at: fromUrl, to: toUrl)
|
|
|
} catch {
|
|
} catch {
|
|
@@ -174,23 +303,23 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
call.resolve(["ok": true])
|
|
call.resolve(["ok": true])
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
@objc func deleteLocalFiles(_ call: CAPPluginCall) {
|
|
@objc func deleteLocalFiles(_ call: CAPPluginCall) {
|
|
|
guard let baseURL = call.getString("basePath").flatMap({path in URL(string: path)}),
|
|
guard let baseURL = call.getString("basePath").flatMap({path in URL(string: path)}),
|
|
|
let filePaths = call.getArray("filePaths") as? [String] else {
|
|
let filePaths = call.getArray("filePaths") as? [String] else {
|
|
|
call.reject("required paremeters: basePath, filePaths")
|
|
call.reject("required paremeters: basePath, filePaths")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
for filePath in filePaths {
|
|
for filePath in filePaths {
|
|
|
- let fileUrl = baseURL.appendingPathComponent(filePath)
|
|
|
|
|
|
|
+ let fileUrl = baseURL.appendingPathComponent(filePath.removingPercentEncoding!)
|
|
|
try? FileManager.default.removeItem(at: fileUrl) // ignore any delete errors
|
|
try? FileManager.default.removeItem(at: fileUrl) // ignore any delete errors
|
|
|
}
|
|
}
|
|
|
call.resolve(["ok": true])
|
|
call.resolve(["ok": true])
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/// remote -> local
|
|
/// remote -> local
|
|
|
@objc func updateLocalFiles(_ call: CAPPluginCall) {
|
|
@objc func updateLocalFiles(_ call: CAPPluginCall) {
|
|
|
guard let baseURL = call.getString("basePath").flatMap({path in URL(string: path)}),
|
|
guard let baseURL = call.getString("basePath").flatMap({path in URL(string: path)}),
|
|
@@ -200,11 +329,27 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
call.reject("required paremeters: basePath, filePaths, graphUUID, token")
|
|
call.reject("required paremeters: basePath, filePaths, graphUUID, token")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // [encrypted-fname: original-fname]
|
|
|
|
|
+ var encryptedFilePathDict: [String: String] = [:]
|
|
|
|
|
+ if fnameEncryptionEnabled() {
|
|
|
|
|
+ for filePath in filePaths {
|
|
|
|
|
+ if let encryptedPath = filePath.removingPercentEncoding!.fnameEncrypt(rawKey: FNAME_ENCRYPTION_KEY!) {
|
|
|
|
|
+ encryptedFilePathDict[encryptedPath] = filePath
|
|
|
|
|
+ } else {
|
|
|
|
|
+ call.reject("cannot decrypt all file names")
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ encryptedFilePathDict = Dictionary(uniqueKeysWithValues: filePaths.map { ($0, $0) })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let encryptedFilePaths = Array(encryptedFilePathDict.keys)
|
|
|
|
|
+
|
|
|
let client = SyncClient(token: token, graphUUID: graphUUID)
|
|
let client = SyncClient(token: token, graphUUID: graphUUID)
|
|
|
client.delegate = self // receives notification
|
|
client.delegate = self // receives notification
|
|
|
-
|
|
|
|
|
- client.getFiles(at: filePaths) { (fileURLs, error) in
|
|
|
|
|
|
|
+
|
|
|
|
|
+ client.getFiles(at: encryptedFilePaths) { (fileURLs, error) in
|
|
|
if let error = error {
|
|
if let error = error {
|
|
|
print("debug getFiles error \(error)")
|
|
print("debug getFiles error \(error)")
|
|
|
self.debugNotification(["event": "download:error", "data": ["message": "error while getting files \(filePaths)"]])
|
|
self.debugNotification(["event": "download:error", "data": ["message": "error while getting files \(filePaths)"]])
|
|
@@ -212,14 +357,15 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
} else {
|
|
} else {
|
|
|
// handle multiple completionHandlers
|
|
// handle multiple completionHandlers
|
|
|
let group = DispatchGroup()
|
|
let group = DispatchGroup()
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
var downloaded: [String] = []
|
|
var downloaded: [String] = []
|
|
|
-
|
|
|
|
|
- for (filePath, remoteFileURL) in fileURLs {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ for (encryptedFilePath, remoteFileURL) in fileURLs {
|
|
|
group.enter()
|
|
group.enter()
|
|
|
|
|
|
|
|
|
|
+ let filePath = encryptedFilePathDict[encryptedFilePath]!
|
|
|
// NOTE: fileURLs from getFiles API is percent-encoded
|
|
// NOTE: fileURLs from getFiles API is percent-encoded
|
|
|
- let localFileURL = baseURL.appendingPathComponent(filePath.decodeFromFname())
|
|
|
|
|
|
|
+ let localFileURL = baseURL.appendingPathComponent(filePath.removingPercentEncoding!)
|
|
|
remoteFileURL.download(toFile: localFileURL) {error in
|
|
remoteFileURL.download(toFile: localFileURL) {error in
|
|
|
if let error = error {
|
|
if let error = error {
|
|
|
self.debugNotification(["event": "download:error", "data": ["message": "error while downloading \(filePath): \(error)"]])
|
|
self.debugNotification(["event": "download:error", "data": ["message": "error while downloading \(filePath): \(error)"]])
|
|
@@ -235,13 +381,61 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
self.debugNotification(["event": "download:done"])
|
|
self.debugNotification(["event": "download:done"])
|
|
|
call.resolve(["ok": true, "data": downloaded])
|
|
call.resolve(["ok": true, "data": downloaded])
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @objc func updateLocalVersionFiles(_ call: CAPPluginCall) {
|
|
|
|
|
+ guard let baseURL = call.getString("basePath").flatMap({path in URL(string: path)}),
|
|
|
|
|
+ let filePaths = call.getArray("filePaths") as? [String],
|
|
|
|
|
+ let graphUUID = call.getString("graphUUID") ,
|
|
|
|
|
+ let token = call.getString("token") else {
|
|
|
|
|
+ call.reject("required paremeters: basePath, filePaths, graphUUID, token")
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ let client = SyncClient(token: token, graphUUID: graphUUID)
|
|
|
|
|
+ client.delegate = self // receives notification
|
|
|
|
|
+
|
|
|
|
|
+ client.getVersionFiles(at: filePaths) { (fileURLDict, error) in
|
|
|
|
|
+ if let error = error {
|
|
|
|
|
+ print("debug getVersionFiles error \(error)")
|
|
|
|
|
+ self.debugNotification(["event": "version-download:error", "data": ["message": "error while getting version files \(filePaths)"]])
|
|
|
|
|
+ call.reject(error.localizedDescription)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // handle multiple completionHandlers
|
|
|
|
|
+ let group = DispatchGroup()
|
|
|
|
|
+
|
|
|
|
|
+ var downloaded: [String] = []
|
|
|
|
|
+
|
|
|
|
|
+ for (filePath, remoteFileURL) in fileURLDict {
|
|
|
|
|
+ group.enter()
|
|
|
|
|
+
|
|
|
|
|
+ // NOTE: fileURLs from getFiles API is percent-encoded
|
|
|
|
|
+ let localFileURL = baseURL.appendingPathComponent("logseq/version-files/").appendingPathComponent(filePath.removingPercentEncoding!)
|
|
|
|
|
+ remoteFileURL.download(toFile: localFileURL) {error in
|
|
|
|
|
+ if let error = error {
|
|
|
|
|
+ self.debugNotification(["event": "version-download:error", "data": ["message": "error while downloading \(filePath): \(error)"]])
|
|
|
|
|
+ print("debug download \(error) in \(filePath)")
|
|
|
|
|
+ } else {
|
|
|
|
|
+ self.debugNotification(["event": "version-download:file", "data": ["file": filePath]])
|
|
|
|
|
+ downloaded.append(filePath)
|
|
|
|
|
+ }
|
|
|
|
|
+ group.leave()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ group.notify(queue: .main) {
|
|
|
|
|
+ self.debugNotification(["event": "version-download:done"])
|
|
|
|
|
+ call.resolve(["ok": true, "data": downloaded])
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // filePaths: Encrypted file paths
|
|
|
@objc func deleteRemoteFiles(_ call: CAPPluginCall) {
|
|
@objc func deleteRemoteFiles(_ call: CAPPluginCall) {
|
|
|
- guard let filePaths = call.getArray("filePaths") as? [String],
|
|
|
|
|
|
|
+ guard var filePaths = call.getArray("filePaths") as? [String],
|
|
|
let graphUUID = call.getString("graphUUID"),
|
|
let graphUUID = call.getString("graphUUID"),
|
|
|
let token = call.getString("token"),
|
|
let token = call.getString("token"),
|
|
|
let txid = call.getInt("txid") else {
|
|
let txid = call.getInt("txid") else {
|
|
@@ -252,7 +446,15 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
call.reject("empty filePaths")
|
|
call.reject("empty filePaths")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ let nFiles = filePaths.count
|
|
|
|
|
+ if fnameEncryptionEnabled() {
|
|
|
|
|
+ filePaths = filePaths.compactMap { $0.removingPercentEncoding!.fnameEncrypt(rawKey: FNAME_ENCRYPTION_KEY!) }
|
|
|
|
|
+ }
|
|
|
|
|
+ if filePaths.count != nFiles {
|
|
|
|
|
+ call.reject("cannot encrypt all file names")
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
let client = SyncClient(token: token, graphUUID: graphUUID, txid: txid)
|
|
let client = SyncClient(token: token, graphUUID: graphUUID, txid: txid)
|
|
|
client.deleteFiles(filePaths) { txid, error in
|
|
client.deleteFiles(filePaths) { txid, error in
|
|
|
guard error == nil else {
|
|
guard error == nil else {
|
|
@@ -266,7 +468,7 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
call.resolve(["ok": true, "txid": txid])
|
|
call.resolve(["ok": true, "txid": txid])
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/// local -> remote
|
|
/// local -> remote
|
|
|
@objc func updateRemoteFiles(_ call: CAPPluginCall) {
|
|
@objc func updateRemoteFiles(_ call: CAPPluginCall) {
|
|
|
guard let baseURL = call.getString("basePath").flatMap({path in URL(string: path)}),
|
|
guard let baseURL = call.getString("basePath").flatMap({path in URL(string: path)}),
|
|
@@ -277,15 +479,15 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
call.reject("required paremeters: basePath, filePaths, graphUUID, token, txid")
|
|
call.reject("required paremeters: basePath, filePaths, graphUUID, token, txid")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
+ let fnameEncryption = call.getBool("fnameEncryption") ?? false // default to false
|
|
|
|
|
+
|
|
|
guard !filePaths.isEmpty else {
|
|
guard !filePaths.isEmpty else {
|
|
|
return call.reject("empty filePaths")
|
|
return call.reject("empty filePaths")
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- print("debug begin updateRemoteFiles \(filePaths)")
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
let client = SyncClient(token: token, graphUUID: graphUUID, txid: txid)
|
|
let client = SyncClient(token: token, graphUUID: graphUUID, txid: txid)
|
|
|
client.delegate = self
|
|
client.delegate = self
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 1. refresh_temp_credential
|
|
// 1. refresh_temp_credential
|
|
|
client.getTempCredential() { (credentials, error) in
|
|
client.getTempCredential() { (credentials, error) in
|
|
|
guard error == nil else {
|
|
guard error == nil else {
|
|
@@ -293,16 +495,16 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
call.reject("error(getTempCredential): \(error!)")
|
|
call.reject("error(getTempCredential): \(error!)")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
var files: [String: URL] = [:]
|
|
var files: [String: URL] = [:]
|
|
|
for filePath in filePaths {
|
|
for filePath in filePaths {
|
|
|
// NOTE: filePath from js may contain spaces
|
|
// NOTE: filePath from js may contain spaces
|
|
|
- let fileURL = baseURL.appendingPathComponent(filePath)
|
|
|
|
|
- files[filePath.encodeAsFname()] = fileURL
|
|
|
|
|
|
|
+ let fileURL = baseURL.appendingPathComponent(filePath.removingPercentEncoding!)
|
|
|
|
|
+ files[filePath] = fileURL
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 2. upload_temp_file
|
|
// 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 {
|
|
guard error == nil else {
|
|
|
self.debugNotification(["event": "upload:error", "data": ["message": "error while uploading temp files: \(error!)"]])
|
|
self.debugNotification(["event": "upload:error", "data": ["message": "error while uploading temp files: \(error!)"]])
|
|
|
call.reject("error(uploadTempFiles): \(error!)")
|
|
call.reject("error(uploadTempFiles): \(error!)")
|
|
@@ -314,7 +516,24 @@ public class FileSync: CAPPlugin, SyncDebugDelegate {
|
|
|
call.reject("no file to update")
|
|
call.reject("no file to update")
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- client.updateFiles(uploadedFileKeyDict) { (txid, error) in
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // encrypted-file-name: (file-key, md5)
|
|
|
|
|
+ var uploadedFileKeyMd5Dict: [String: [String]] = [:]
|
|
|
|
|
+
|
|
|
|
|
+ if fnameEncryptionEnabled() && fnameEncryption {
|
|
|
|
|
+ for (filePath, fileKey) in uploadedFileKeyDict {
|
|
|
|
|
+ guard let encryptedFilePath = filePath.removingPercentEncoding!.fnameEncrypt(rawKey: FNAME_ENCRYPTION_KEY!) else {
|
|
|
|
|
+ call.reject("cannot encrypt file name")
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ uploadedFileKeyMd5Dict[encryptedFilePath] = [fileKey, fileMd5Dict[filePath]!]
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ for (filePath, fileKey) in uploadedFileKeyDict {
|
|
|
|
|
+ uploadedFileKeyMd5Dict[filePath] = [fileKey, fileMd5Dict[filePath]!]
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ client.updateFiles(uploadedFileKeyMd5Dict) { (txid, error) in
|
|
|
guard error == nil else {
|
|
guard error == nil else {
|
|
|
self.debugNotification(["event": "upload:error", "data": ["message": "error while updating files: \(error!)"]])
|
|
self.debugNotification(["event": "upload:error", "data": ["message": "error while updating files: \(error!)"]])
|
|
|
call.reject("error updateFiles: \(error!)")
|
|
call.reject("error updateFiles: \(error!)")
|