feat: full local assets / album sync
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user