feat: full local assets / album sync

This commit is contained in:
shenlong-tanwen
2024-10-17 23:33:00 +05:30
parent a09710ec7b
commit c91a2878dc
87 changed files with 2417 additions and 366 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ import Flutter
// Register piegon handler
let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
ImmichHostServiceSetup.setUp(binaryMessenger: controller.binaryMessenger, api: ImmichHostServiceImpl())
ImHostServiceSetup.setUp(binaryMessenger: controller.binaryMessenger, api: ImHostServiceImpl())
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
@@ -1,38 +1,37 @@
import CryptoKit
class ImmichHostServiceImpl: ImmichHostService {
func digestFiles(paths: [String], completion: @escaping (Result<[FlutterStandardTypedData?]?, Error>) -> Void) {
class ImHostServiceImpl: ImHostService {
func digestFiles(paths: [String], completion: @escaping (Result<[FlutterStandardTypedData?], Error>) -> Void) {
let bufsize = 2 * 1024 * 1024
// Private error to throw if file cannot be read
enum DigestError: String, LocalizedError {
case NoFileHandle = "Cannot Open File Handle"
public var errorDescription: String? { self.rawValue }
}
// Compute hash in background thread
DispatchQueue.global(qos: .background).async {
var hashes: [FlutterStandardTypedData?] = Array(repeating: nil, count: paths.count)
for i in (0 ..< paths.count) {
let hashes = paths.map { path -> FlutterStandardTypedData? in
do {
guard let file = FileHandle(forReadingAtPath: paths[i]) else { throw DigestError.NoFileHandle }
var hasher = Insecure.SHA1.init();
guard let file = FileHandle(forReadingAtPath: path) else {
throw NSError(domain: "ImHostServiceImpl", code: 1, userInfo: [NSLocalizedDescriptionKey: "Cannot Open File Handle"])
}
defer { file.closeFile() }
var hasher = Insecure.SHA1()
while autoreleasepool(invoking: {
let chunk = file.readData(ofLength: bufsize)
guard !chunk.isEmpty else { return false } // EOF
hasher.update(data: chunk)
return true // continue
}) { }
let digest = hasher.finalize()
hashes[i] = FlutterStandardTypedData(bytes: Data(Array(digest.makeIterator())))
return FlutterStandardTypedData(bytes: Data(digest))
} catch {
print("Cannot calculate the digest of the file \(paths[i]) due to \(error.localizedDescription)")
print("Cannot calculate the digest of the file \(path) due to \(error.localizedDescription)")
return nil
}
}
// Return result in main thread
DispatchQueue.main.async {
completion(.success(Array(hashes)))
completion(.success(hashes))
}
}
}