fix: Use sqlcipher for FluffyBox

This commit is contained in:
Krille Fear 2021-11-17 19:29:17 +01:00
parent db013043a7
commit c4fa46c9f3
3 changed files with 16 additions and 23 deletions

View File

@ -1,3 +1,5 @@
//@dart=2.12
import 'dart:io'; import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
@ -8,15 +10,12 @@ import 'package:encrypt/encrypt.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart' as sqflite; import 'package:sqflite_sqlcipher/sqflite.dart' as sqflite;
import '../platform_infos.dart';
class FlutterFluffyBoxDatabase extends FluffyBoxDatabase { class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
FlutterFluffyBoxDatabase( FlutterFluffyBoxDatabase(
String name, { String name, {
String path, Future<sqflite.Database> Function()? openSqlDatabase,
Future<sqflite.Database> Function() openSqlDatabase,
}) : super( }) : super(
name, name,
openSqlDatabase: openSqlDatabase, openSqlDatabase: openSqlDatabase,
@ -27,6 +26,7 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
static Future<FluffyBoxDatabase> databaseBuilder(Client client) async { static Future<FluffyBoxDatabase> databaseBuilder(Client client) async {
Logs().d('Open FluffyBox...'); Logs().d('Open FluffyBox...');
String? password;
try { try {
// Workaround for secure storage is calling Platform.operatingSystem on web // Workaround for secure storage is calling Platform.operatingSystem on web
if (kIsWeb) throw MissingPluginException(); if (kIsWeb) throw MissingPluginException();
@ -43,24 +43,27 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
} }
// workaround for if we just wrote to the key and it still doesn't exist // workaround for if we just wrote to the key and it still doesn't exist
final rawEncryptionKey = await secureStorage.read(key: _cipherStorageKey); password = await secureStorage.read(key: _cipherStorageKey);
if (rawEncryptionKey == null) throw MissingPluginException(); if (password == null) throw MissingPluginException();
} on MissingPluginException catch (_) { } on MissingPluginException catch (_) {
Logs().i('FluffyBox encryption is not supported on this platform'); Logs().i('FluffyBox encryption is not supported on this platform');
} }
final db = FluffyBoxDatabase( final db = FluffyBoxDatabase(
'fluffybox_${client.clientName.replaceAll(' ', '_').toLowerCase()}', 'fluffybox_${client.clientName.replaceAll(' ', '_').toLowerCase()}',
openSqlDatabase: () => _openSqlDatabase(client), openSqlDatabase: () => _openSqlDatabase(client, password),
); );
await db.open(); await db.open();
Logs().d('FluffyBox is ready'); Logs().d('FluffyBox is ready');
return db; return db;
} }
static Future<sqflite.Database> _openSqlDatabase(Client client) async { static Future<sqflite.Database> _openSqlDatabase(
Client client,
String? password,
) async {
final path = await _findDatabasePath(client); final path = await _findDatabasePath(client);
return await sqflite.openDatabase(path); return await sqflite.openDatabase(path, password: password);
} }
static Future<String> _findDatabasePath(Client client) async { static Future<String> _findDatabasePath(Client client) async {
@ -85,9 +88,7 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
@override @override
int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0; int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0;
@override @override
bool get supportsFileStoring => (PlatformInfos.isIOS || bool get supportsFileStoring => !kIsWeb;
PlatformInfos.isAndroid ||
PlatformInfos.isDesktop);
Future<String> _getFileStoreDirectory() async { Future<String> _getFileStoreDirectory() async {
try { try {
@ -97,12 +98,12 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
return (await getApplicationDocumentsDirectory()).path; return (await getApplicationDocumentsDirectory()).path;
} }
} catch (_) { } catch (_) {
return (await getDownloadsDirectory()).path; return (await getDownloadsDirectory())!.path;
} }
} }
@override @override
Future<Uint8List> getFile(Uri mxcUri) async { Future<Uint8List?> getFile(Uri mxcUri) async {
if (!supportsFileStoring) return null; if (!supportsFileStoring) return null;
final tempDirectory = await _getFileStoreDirectory(); final tempDirectory = await _getFileStoreDirectory();
final file = final file =

View File

@ -658,13 +658,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.1" version: "2.0.1"
idb_sqflite:
dependency: "direct main"
description:
name: idb_sqflite
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
image: image:
dependency: transitive dependency: transitive
description: description:

View File

@ -42,7 +42,6 @@ dependencies:
future_loading_dialog: ^0.2.1 future_loading_dialog: ^0.2.1
geolocator: ^7.6.2 geolocator: ^7.6.2
hive_flutter: ^1.1.0 hive_flutter: ^1.1.0
idb_sqflite: ^1.0.1
image_picker: ^0.8.4+2 image_picker: ^0.8.4+2
intl: any intl: any
localstorage: ^4.0.0+1 localstorage: ^4.0.0+1