wip: add platform channels

This commit is contained in:
shenlong-tanwen
2024-09-15 03:31:01 +05:30
parent f1dcfbc594
commit 37b15869d5
9 changed files with 149 additions and 12 deletions
@@ -1,5 +1,15 @@
package com.alextran.immich
import ImmichHostService
import com.alextran.immich.platform.ImmichHostServiceImpl
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
class MainActivity: FlutterActivity()
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
// Register piegon handler
ImmichHostService.setUp(flutterEngine.dartExecutor.binaryMessenger, ImmichHostServiceImpl())
}
}
@@ -0,0 +1,47 @@
package com.alextran.immich.platform
import ImmichHostService
import android.util.Log
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.FileInputStream
import java.security.MessageDigest
class ImmichHostServiceImpl: ImmichHostService {
@OptIn(DelicateCoroutinesApi::class)
override fun digestFiles(paths: List<String>, callback: (Result<List<ByteArray?>?>) -> Unit) {
GlobalScope.launch(Dispatchers.IO) {
val buf = ByteArray(Companion.BUFFER_SIZE)
val digest: MessageDigest = MessageDigest.getInstance("SHA-1")
val hashes = arrayOfNulls<ByteArray>(paths.size)
for (i in paths.indices) {
val path = paths[i]
var len = 0
try {
val file = FileInputStream(path)
file.use { assetFile ->
while (true) {
len = assetFile.read(buf)
if (len != Companion.BUFFER_SIZE) break
digest.update(buf)
}
}
digest.update(buf, 0, len)
hashes[i] = digest.digest()
} catch (e: Exception) {
// skip this file
Log.w(TAG, "Failed to hash file ${paths[i]}: $e")
}
}
callback(Result.success(hashes.asList()))
}
}
companion object {
private const val BUFFER_SIZE = 2 * 1024 * 1024;
private const val TAG = "ImmichHostServiceImpl"
}
}