Add file hash comparison to optimize asset copying in aniyomiBridge
This commit is contained in:
@@ -10,6 +10,7 @@ import 'package:k3vinb5_aniyomi_bridge/models/extension.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:crypto/crypto.dart';
|
||||
import 'package:k3vinb5_aniyomi_bridge/jmodels/jvideo.dart';
|
||||
import 'package:k3vinb5_aniyomi_bridge/jmodels/jsanime.dart';
|
||||
import 'package:k3vinb5_aniyomi_bridge/jmodels/jsepisode.dart';
|
||||
@@ -369,7 +370,19 @@ class AniyomiBridge {
|
||||
_aniyomiBridgeJarName,
|
||||
);
|
||||
File aniyomiBridgeCore = File(aniyomiBridgeCorePath);
|
||||
bool needsCopy = false;
|
||||
if (!(await aniyomiBridgeCore.exists())) {
|
||||
needsCopy = true;
|
||||
} else {
|
||||
String assetPath = "$_packageAssetsDir/$_aniyomiBridgeJarName";
|
||||
String existingFileHash = await _computeFileHash(aniyomiBridgeCore);
|
||||
String assetHash = await _computeAssetHash(assetPath);
|
||||
if (existingFileHash != assetHash) {
|
||||
needsCopy = true;
|
||||
}
|
||||
}
|
||||
if (needsCopy) {
|
||||
_logger.i("Copying new aniyomiBridgeCore binary to $aniyomiBridgeCorePath");
|
||||
await _copyAssetToFile(
|
||||
"$_packageAssetsDir/$_aniyomiBridgeJarName",
|
||||
aniyomiBridgeCorePath,
|
||||
@@ -377,6 +390,19 @@ class AniyomiBridge {
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _computeFileHash(File file) async {
|
||||
final bytes = await file.readAsBytes();
|
||||
final digest = sha256.convert(bytes);
|
||||
return digest.toString();
|
||||
}
|
||||
|
||||
Future<String> _computeAssetHash(String assetPath) async {
|
||||
final byteData = await rootBundle.load(assetPath);
|
||||
final bytes = byteData.buffer.asUint8List();
|
||||
final digest = sha256.convert(bytes);
|
||||
return digest.toString();
|
||||
}
|
||||
|
||||
Future<File> _copyAssetToFile(String assetPath, String outPath) async {
|
||||
final byteData = await rootBundle.load(assetPath);
|
||||
final buffer = byteData.buffer.asUint8List();
|
||||
|
||||
Reference in New Issue
Block a user