fix: Use new FluffyBox 0.2.0

This commit is contained in:
Krille Fear 2021-11-18 15:26:53 +01:00
parent fc84245555
commit 5fde793b29
3 changed files with 22 additions and 86 deletions

View File

@ -1,99 +1,67 @@
//@dart=2.12
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/foundation.dart' hide Key;
import 'package:flutter/services.dart';
import 'package:encrypt/encrypt.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart' as ffi;
import 'package:sqflite_sqlcipher/sqflite.dart' as sqflite;
class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
FlutterFluffyBoxDatabase(
String name, {
Future<sqflite.Database> Function()? openSqlDatabase,
String name,
String path, {
List<int>? key,
}) : super(
name,
openSqlDatabase: openSqlDatabase,
path,
key: key,
);
static const String _cipherStorageKey = 'database_encryption_key';
static const int _cipherStorageKeyLength = 512;
static Future<FluffyBoxDatabase> databaseBuilder(Client client) async {
Logs().d('Open FluffyBox...');
String? password;
List<int>? hiverCipher;
try {
// Workaround for secure storage is calling Platform.operatingSystem on web
if (kIsWeb) throw MissingPluginException();
const secureStorage = FlutterSecureStorage();
final containsEncryptionKey =
await secureStorage.containsKey(key: _cipherStorageKey);
if (!containsEncryptionKey) {
final key = SecureRandom(_cipherStorageKeyLength).base64;
final key = Hive.generateSecureKey();
await secureStorage.write(
key: _cipherStorageKey,
value: key,
value: base64UrlEncode(key),
);
}
// workaround for if we just wrote to the key and it still doesn't exist
password = await secureStorage.read(key: _cipherStorageKey);
if (password == null) throw MissingPluginException();
final rawEncryptionKey = await secureStorage.read(key: _cipherStorageKey);
if (rawEncryptionKey == null) throw MissingPluginException();
hiverCipher = base64Url.decode(rawEncryptionKey);
} on MissingPluginException catch (_) {
Logs().i('FluffyBox encryption is not supported on this platform');
}
final db = FluffyBoxDatabase(
'fluffybox_${client.clientName.replaceAll(' ', '_').toLowerCase()}',
openSqlDatabase: kIsWeb ? null : () => _openSqlDatabase(client, password),
await _findDatabasePath(client),
key: hiverCipher,
);
await db.open();
Logs().d('FluffyBox is ready');
return db;
}
static Future<void> _onConfigure(sqflite.Database db) async {
await db.execute('PRAGMA page_size = 8192');
await db.execute('PRAGMA cache_size = 16384');
await db.execute('PRAGMA temp_store = MEMORY');
}
static Future<sqflite.Database> _openSqlDatabase(
Client client,
String? password,
) async {
final path = await _findDatabasePath(client);
try {
late final sqflite.Database db;
if (Platform.isAndroid || Platform.isIOS) {
db = await sqflite.openDatabase(
path,
password: password,
onConfigure: _onConfigure,
);
return db;
} else {
db = await ffi.databaseFactoryFfi.openDatabase(
path,
options: sqflite.SqlCipherOpenDatabaseOptions(
password: password,
onConfigure: _onConfigure,
),
);
}
await db.execute('PRAGMA journal_mode = WAL');
return db;
} catch (_) {
File(path).delete();
Logs().w('Failed to open database. Delete file now...');
rethrow;
}
}
static Future<String> _findDatabasePath(Client client) async {
String path = client.clientName;
if (!kIsWeb) {
@ -107,8 +75,7 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
directory = Directory.current;
}
}
path =
'${directory.path}${client.clientName.replaceAll(' ', '-')}.sqflite';
path = directory.path;
}
return path;
}

View File

@ -357,7 +357,7 @@ packages:
name: fluffybox
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.7"
version: "0.2.0"
flutter:
dependency: "direct main"
description: flutter
@ -782,7 +782,7 @@ packages:
description:
path: "."
ref: "krille/idb"
resolved-ref: d59c93f61329d3ddac6a36f682d3bf41ef3ab410
resolved-ref: d13cf8c099bb5c10c0bcb1a7ef64b0c6bd5cd240
url: "https://gitlab.com/famedly/company/frontend/famedlysdk.git"
source: git
version: "0.7.0-nullsafety.6"
@ -1318,34 +1318,6 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1+1"
sqflite_common_ffi:
dependency: "direct main"
description:
name: sqflite_common_ffi
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
sqflite_sqlcipher:
dependency: "direct main"
description:
name: sqflite_sqlcipher
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
sqlcipher_flutter_libs:
dependency: "direct main"
description:
name: sqlcipher_flutter_libs
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.1"
sqlite3:
dependency: transitive
description:
name: sqlite3
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
stack_trace:
dependency: transitive
description:

View File

@ -67,9 +67,6 @@ dependencies:
sentry: ^6.0.1
share: ^2.0.4
slugify: ^2.0.0
sqflite_common_ffi: ^2.1.0
sqflite_sqlcipher: ^2.1.0
sqlcipher_flutter_libs: ^0.5.1
swipe_to_action: ^0.2.0
uni_links: ^0.5.1
unifiedpush: ^1.0.6