Skip to content

Commit fa2ab61

Browse files
committed
Adding web utils to serious_python_platform_interface
1 parent 6986cf6 commit fa2ab61

File tree

4 files changed

+190
-110
lines changed

4 files changed

+190
-110
lines changed
Lines changed: 14 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,27 @@
1-
import 'dart:io';
2-
3-
import 'package:archive/archive_io.dart';
4-
import 'package:flutter/foundation.dart';
5-
import 'package:flutter/services.dart';
6-
import 'package:flutter/widgets.dart';
7-
import 'package:path/path.dart' as p;
8-
import 'package:path_provider/path_provider.dart';
1+
import 'utils_web.dart' if (dart.library.io) 'utils_io.dart';
92

103
Future<String> extractAssetOrFile(String path,
11-
{bool isAsset = true, String? targetPath, bool checkHash = false}) async {
12-
WidgetsFlutterBinding.ensureInitialized();
13-
final supportDir = await getApplicationSupportDirectory();
14-
final destDir =
15-
Directory(p.join(supportDir.path, "flet", targetPath ?? p.dirname(path)));
16-
17-
String assetHash = "";
18-
String destHash = "";
19-
var hashFile = File(p.join(destDir.path, ".hash"));
20-
21-
// re-create dir
22-
if (await destDir.exists()) {
23-
if (kDebugMode) {
24-
// always re-create in debug mode
25-
await destDir.delete(recursive: true);
26-
} else {
27-
if (checkHash) {
28-
// read asset hash from asset
29-
try {
30-
assetHash = (await rootBundle.loadString("$path.hash")).trim();
31-
// ignore: empty_catches
32-
} catch (e) {}
33-
if (await hashFile.exists()) {
34-
destHash = (await hashFile.readAsString()).trim();
35-
}
36-
}
37-
38-
if (assetHash != destHash ||
39-
(checkHash && assetHash == "" && destHash == "")) {
40-
await destDir.delete(recursive: true);
41-
} else {
42-
debugPrint("Application archive already unpacked to ${destDir.path}");
43-
return destDir.path;
44-
}
45-
}
46-
}
47-
48-
debugPrint("extractAssetOrFile directory: ${destDir.path}");
49-
await destDir.create(recursive: true);
50-
51-
// unpack from asset or file
52-
debugPrint("Start unpacking archive: $path");
53-
Stopwatch stopwatch = Stopwatch()..start();
54-
55-
try {
56-
Archive archive;
57-
if (isAsset) {
58-
final bytes = await rootBundle.load(path);
59-
var data = bytes.buffer.asUint8List();
60-
archive = ZipDecoder().decodeBytes(data);
61-
} else {
62-
final inputStream = InputFileStream(path);
63-
archive = ZipDecoder().decodeBuffer(inputStream);
64-
}
65-
await extractArchiveToDiskAsync(archive, destDir.path, asyncWrite: true);
66-
} catch (e) {
67-
debugPrint("Error unpacking archive: $e");
68-
await destDir.delete(recursive: true);
69-
rethrow;
70-
}
71-
72-
debugPrint("Finished unpacking application archive in ${stopwatch.elapsed}");
73-
74-
if (checkHash) {
75-
await hashFile.writeAsString(assetHash);
76-
}
77-
78-
return destDir.path;
4+
{bool isAsset = true, String? targetPath, bool checkHash = false}) {
5+
return getPlatformUtils().extractAssetOrFile(path,
6+
isAsset: isAsset, targetPath: targetPath, checkHash: checkHash);
797
}
808

819
Future<String> extractAssetZip(String assetPath,
82-
{String? targetPath, bool checkHash = false}) async {
83-
return extractAssetOrFile(assetPath,
10+
{String? targetPath, bool checkHash = false}) {
11+
return getPlatformUtils().extractAssetZip(assetPath,
8412
targetPath: targetPath, checkHash: checkHash);
8513
}
8614

8715
Future<String> extractFileZip(String filePath,
88-
{String? targetPath, bool checkHash = false}) async {
89-
return extractAssetOrFile(filePath,
90-
isAsset: false, targetPath: targetPath, checkHash: checkHash);
16+
{String? targetPath, bool checkHash = false}) {
17+
return getPlatformUtils().extractFileZip(filePath,
18+
targetPath: targetPath, checkHash: checkHash);
9119
}
9220

93-
Future<String> extractAsset(String assetPath) async {
94-
WidgetsFlutterBinding.ensureInitialized();
95-
96-
// (re-)create destination directory
97-
final supportDir = await getApplicationSupportDirectory();
98-
final destDir =
99-
Directory(p.join(supportDir.path, "flet", p.dirname(assetPath)));
100-
101-
await destDir.create(recursive: true);
102-
103-
// extract file from assets
104-
var destPath = p.join(destDir.path, p.basename(assetPath));
105-
if (kDebugMode && await File(destPath).exists()) {
106-
await File(destPath).delete();
107-
}
108-
ByteData data = await rootBundle.load(assetPath);
109-
List<int> bytes =
110-
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
111-
await File(destPath).writeAsBytes(bytes);
112-
return destPath;
21+
Future<String> extractAsset(String assetPath) {
22+
return getPlatformUtils().extractAsset(assetPath);
11323
}
11424

115-
Future<String> getDirFiles(String path, {bool recursive = false}) async {
116-
final dir = Directory(path);
117-
if (!await dir.exists()) {
118-
return "<not found>";
119-
}
120-
return (await dir.list(recursive: recursive).toList())
121-
.map((file) => file.path)
122-
.join('\n');
123-
}
25+
Future<String> getDirFiles(String path, {bool recursive = false}) {
26+
return getPlatformUtils().getDirFiles(path, recursive: recursive);
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
abstract class PlatformUtils {
2+
Future<String> extractAssetOrFile(String path,
3+
{bool isAsset = true, String? targetPath, bool checkHash = false});
4+
5+
Future<String> extractAssetZip(String assetPath,
6+
{String? targetPath, bool checkHash = false});
7+
8+
Future<String> extractFileZip(String filePath,
9+
{String? targetPath, bool checkHash = false});
10+
11+
Future<String> extractAsset(String assetPath);
12+
13+
Future<String> getDirFiles(String path, {bool recursive = false});
14+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import 'dart:io';
2+
3+
import 'package:archive/archive_io.dart';
4+
import 'package:flutter/foundation.dart';
5+
import 'package:flutter/services.dart';
6+
import 'package:flutter/widgets.dart';
7+
import 'package:path/path.dart' as p;
8+
import 'package:path_provider/path_provider.dart';
9+
10+
import 'utils_interface.dart';
11+
12+
class IOUtils implements PlatformUtils {
13+
@override
14+
Future<String> extractAssetOrFile(String path,
15+
{bool isAsset = true, String? targetPath, bool checkHash = false}) async {
16+
WidgetsFlutterBinding.ensureInitialized();
17+
final supportDir = await getApplicationSupportDirectory();
18+
final destDir = Directory(p.join(supportDir.path, "flet", targetPath ?? p.dirname(path)));
19+
20+
String assetHash = "";
21+
String destHash = "";
22+
var hashFile = File(p.join(destDir.path, ".hash"));
23+
24+
if (await destDir.exists()) {
25+
if (kDebugMode) {
26+
await destDir.delete(recursive: true);
27+
} else if (checkHash) {
28+
try {
29+
assetHash = (await rootBundle.loadString("$path.hash")).trim();
30+
} catch (e) {
31+
assetHash = "";
32+
}
33+
34+
if (await hashFile.exists()) {
35+
destHash = (await hashFile.readAsString()).trim();
36+
}
37+
38+
if (assetHash != destHash || (checkHash && assetHash.isEmpty && destHash.isEmpty)) {
39+
await destDir.delete(recursive: true);
40+
} else {
41+
return destDir.path;
42+
}
43+
}
44+
}
45+
46+
await _extractArchive(path, destDir, isAsset);
47+
48+
if (checkHash) {
49+
await hashFile.writeAsString(assetHash);
50+
}
51+
52+
return destDir.path;
53+
}
54+
55+
Future<void> _extractArchive(String path, Directory destDir, bool isAsset) async {
56+
await destDir.create(recursive: true);
57+
58+
try {
59+
Archive archive;
60+
if (isAsset) {
61+
final bytes = await rootBundle.load(path);
62+
var data = bytes.buffer.asUint8List();
63+
archive = ZipDecoder().decodeBytes(data);
64+
} else {
65+
final inputStream = InputFileStream(path);
66+
archive = ZipDecoder().decodeBuffer(inputStream);
67+
}
68+
await extractArchiveToDiskAsync(archive, destDir.path, asyncWrite: true);
69+
} catch (e) {
70+
debugPrint("Error unpacking archive: $e");
71+
await destDir.delete(recursive: true);
72+
rethrow;
73+
}
74+
}
75+
76+
@override
77+
Future<String> extractAssetZip(String assetPath, {String? targetPath, bool checkHash = false}) {
78+
return extractAssetOrFile(assetPath, targetPath: targetPath, checkHash: checkHash);
79+
}
80+
81+
@override
82+
Future<String> extractFileZip(String filePath, {String? targetPath, bool checkHash = false}) {
83+
return extractAssetOrFile(filePath, isAsset: false, targetPath: targetPath, checkHash: checkHash);
84+
}
85+
86+
@override
87+
Future<String> extractAsset(String assetPath) async {
88+
WidgetsFlutterBinding.ensureInitialized();
89+
final supportDir = await getApplicationSupportDirectory();
90+
final destDir = Directory(p.join(supportDir.path, "flet", p.dirname(assetPath)));
91+
await destDir.create(recursive: true);
92+
93+
var destPath = p.join(destDir.path, p.basename(assetPath));
94+
if (kDebugMode && await File(destPath).exists()) {
95+
await File(destPath).delete();
96+
}
97+
98+
ByteData data = await rootBundle.load(assetPath);
99+
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
100+
await File(destPath).writeAsBytes(bytes);
101+
return destPath;
102+
}
103+
104+
@override
105+
Future<String> getDirFiles(String path, {bool recursive = false}) async {
106+
final dir = Directory(path);
107+
if (!await dir.exists()) {
108+
return "<not found>";
109+
}
110+
return (await dir.list(recursive: recursive).toList()).map((file) => file.path).join('\n');
111+
}
112+
}
113+
114+
PlatformUtils getPlatformUtils() => IOUtils();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import 'package:flutter/services.dart';
2+
import 'package:flutter/widgets.dart';
3+
import 'utils_interface.dart';
4+
5+
class WebUtils implements PlatformUtils {
6+
@override
7+
Future<String> extractAssetOrFile(String path,
8+
{bool isAsset = true, String? targetPath, bool checkHash = false}) async {
9+
WidgetsFlutterBinding.ensureInitialized();
10+
try {
11+
if (isAsset) {
12+
await rootBundle.load(path);
13+
}
14+
return path;
15+
} catch (e) {
16+
debugPrint('Error handling web asset: $e');
17+
rethrow;
18+
}
19+
}
20+
21+
@override
22+
Future<String> extractAssetZip(String assetPath,
23+
{String? targetPath, bool checkHash = false}) {
24+
return extractAssetOrFile(assetPath,
25+
targetPath: targetPath, checkHash: checkHash);
26+
}
27+
28+
@override
29+
Future<String> extractFileZip(String filePath,
30+
{String? targetPath, bool checkHash = false}) {
31+
return extractAssetOrFile(filePath,
32+
isAsset: false, targetPath: targetPath, checkHash: checkHash);
33+
}
34+
35+
@override
36+
Future<String> extractAsset(String assetPath) async {
37+
WidgetsFlutterBinding.ensureInitialized();
38+
await rootBundle.load(assetPath);
39+
return assetPath;
40+
}
41+
42+
@override
43+
Future<String> getDirFiles(String path, {bool recursive = false}) async {
44+
return path;
45+
}
46+
}
47+
48+
PlatformUtils getPlatformUtils() => WebUtils();

0 commit comments

Comments
 (0)