Skip to content

Commit 1b7cdbd

Browse files
committed
Support checking app asset hash
1 parent c74cf39 commit 1b7cdbd

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
123

src/serious_python/example/flet_example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Future prepareApp() async {
135135
await setupDesktop();
136136

137137
// extract app from asset
138-
appDir = await extractAssetZip(assetPath);
138+
appDir = await extractAssetZip(assetPath, checkHash: true);
139139

140140
// set current directory to app path
141141
Directory.current = appDir;

src/serious_python/example/flet_example/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,5 @@ flutter:
6666
uses-material-design: true
6767

6868
assets:
69-
- app/app.zip
69+
- app/app.zip
70+
- app/app.zip.hash

src/serious_python_platform_interface/lib/src/utils.dart

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import 'package:path/path.dart' as p;
1010
import 'package:path_provider/path_provider.dart';
1111

1212
Future<String> extractAssetOrFile(String path,
13-
{bool isAsset = true, String? targetPath}) async {
13+
{bool isAsset = true, String? targetPath, bool checkHash = false}) async {
1414
WidgetsFlutterBinding.ensureInitialized();
1515
PackageInfo packageInfo = await PackageInfo.fromPlatform();
1616
final documentsOrTempDir = (defaultTargetPlatform == TargetPlatform.iOS ||
@@ -22,14 +22,33 @@ Future<String> extractAssetOrFile(String path,
2222
"${packageInfo.appName}-${packageInfo.version}-${packageInfo.buildNumber}",
2323
targetPath ?? p.dirname(path)));
2424

25+
String assetHash = "";
26+
String destHash = "";
27+
var hashFile = File(p.join(destDir.path, ".hash"));
28+
2529
// re-create dir
2630
if (await destDir.exists()) {
2731
if (kDebugMode) {
2832
// always re-create in debug mode
2933
await destDir.delete(recursive: true);
3034
} else {
31-
debugPrint("Application archive already unpacked.");
32-
return destDir.path;
35+
if (checkHash) {
36+
// read asset hash from asset
37+
try {
38+
assetHash = (await rootBundle.loadString("$path.hash")).trim();
39+
// ignore: empty_catches
40+
} catch (e) {}
41+
if (await hashFile.exists()) {
42+
destHash = (await hashFile.readAsString()).trim();
43+
}
44+
}
45+
46+
if (assetHash != destHash) {
47+
await destDir.delete(recursive: true);
48+
} else {
49+
debugPrint("Application archive already unpacked.");
50+
return destDir.path;
51+
}
3352
}
3453
}
3554
await destDir.create(recursive: true);
@@ -61,16 +80,24 @@ Future<String> extractAssetOrFile(String path,
6180
}
6281
}
6382

83+
if (checkHash) {
84+
await hashFile.writeAsString(assetHash);
85+
}
86+
6487
debugPrint("Finished unpacking application archive.");
6588
return destDir.path;
6689
}
6790

68-
Future<String> extractAssetZip(String assetPath, {String? targetPath}) async {
69-
return extractAssetOrFile(assetPath, targetPath: targetPath);
91+
Future<String> extractAssetZip(String assetPath,
92+
{String? targetPath, bool checkHash = false}) async {
93+
return extractAssetOrFile(assetPath,
94+
targetPath: targetPath, checkHash: checkHash);
7095
}
7196

72-
Future<String> extractFileZip(String filePath, {String? targetPath}) async {
73-
return extractAssetOrFile(filePath, isAsset: false, targetPath: targetPath);
97+
Future<String> extractFileZip(String filePath,
98+
{String? targetPath, bool checkHash = false}) async {
99+
return extractAssetOrFile(filePath,
100+
isAsset: false, targetPath: targetPath, checkHash: checkHash);
74101
}
75102

76103
Future<String> extractAsset(String assetPath) async {

0 commit comments

Comments
 (0)