nc-photos/np_geocoder/lib/src/web/db_util.dart

30 lines
1.2 KiB
Dart
Raw Normal View History

2023-08-28 00:54:18 +08:00
import 'package:flutter/services.dart' show rootBundle;
import 'package:http/http.dart' as http;
import 'package:sqlite3/wasm.dart';
2024-05-20 22:31:40 +08:00
// Web is no longer supported. The code here has been updated to make it build
// with the latest sqlite3 package, but they are untested
2023-08-28 00:54:18 +08:00
Future<CommonDatabase> openRawSqliteDbFromAsset(
String assetRelativePath,
String outputFilename, {
bool isReadOnly = false,
}) async {
final response = await http.get(Uri.parse("sqlite3.wasm"));
2024-05-20 22:31:40 +08:00
final sqlite3 = await WasmSqlite3.load(response.bodyBytes);
2023-08-28 00:54:18 +08:00
final fs = await IndexedDbFileSystem.open(dbName: "nc-photos");
2024-05-20 22:31:40 +08:00
sqlite3.registerVirtualFileSystem(fs, makeDefault: true);
2023-08-28 00:54:18 +08:00
2024-05-20 22:31:40 +08:00
if (fs.xAccess("/app-file/$outputFilename", SqlFlag.SQLITE_OPEN_READONLY) ==
0) {
2023-08-28 00:54:18 +08:00
// copy file from assets
final blob = await rootBundle.load("assets/$assetRelativePath");
final buffer = blob.buffer;
2024-05-20 22:31:40 +08:00
final f = fs.xOpen(Sqlite3Filename("/app-file/$outputFilename"),
SqlFlag.SQLITE_OPEN_CREATE | SqlFlag.SQLITE_OPEN_READWRITE);
f.file
.xWrite(buffer.asUint8List(blob.offsetInBytes, blob.lengthInBytes), 0);
2023-08-28 00:54:18 +08:00
await fs.flush();
}
return sqlite3.open("/app-file/$outputFilename");
}