fluffychat/lib/utils/matrix_sdk_extensions.dart/fluffybox_database.dart

138 lines
4.2 KiB
Dart
Raw Normal View History

2021-11-17 19:29:17 +01:00
//@dart=2.12
2021-11-17 12:20:15 +01:00
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:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart';
2021-11-18 07:59:35 +01:00
import 'package:sqflite_common_ffi/sqflite_ffi.dart' as ffi;
2021-11-17 19:29:17 +01:00
import 'package:sqflite_sqlcipher/sqflite.dart' as sqflite;
2021-11-17 12:20:15 +01:00
2021-11-17 19:17:40 +01:00
class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
FlutterFluffyBoxDatabase(
2021-11-17 12:20:15 +01:00
String name, {
2021-11-17 19:29:17 +01:00
Future<sqflite.Database> Function()? openSqlDatabase,
2021-11-17 12:20:15 +01:00
}) : super(
name,
2021-11-17 19:17:40 +01:00
openSqlDatabase: openSqlDatabase,
2021-11-17 12:20:15 +01:00
);
static const String _cipherStorageKey = 'database_encryption_key';
static const int _cipherStorageKeyLength = 512;
static Future<FluffyBoxDatabase> databaseBuilder(Client client) async {
2021-11-17 12:48:45 +01:00
Logs().d('Open FluffyBox...');
2021-11-17 19:29:17 +01:00
String? password;
2021-11-18 07:59:35 +01:00
if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
try {
const secureStorage = FlutterSecureStorage();
final containsEncryptionKey =
await secureStorage.containsKey(key: _cipherStorageKey);
if (!containsEncryptionKey) {
final key = SecureRandom(_cipherStorageKeyLength).base64;
await secureStorage.write(
key: _cipherStorageKey,
value: key,
);
}
2021-11-17 12:20:15 +01:00
2021-11-18 07:59:35 +01:00
// 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();
} on MissingPluginException catch (_) {
Logs().i('FluffyBox encryption is not supported on this platform');
2021-11-17 12:20:15 +01:00
}
}
final db = FluffyBoxDatabase(
2021-11-17 19:17:40 +01:00
'fluffybox_${client.clientName.replaceAll(' ', '_').toLowerCase()}',
2021-11-17 19:29:17 +01:00
openSqlDatabase: () => _openSqlDatabase(client, password),
2021-11-17 12:20:15 +01:00
);
await db.open();
2021-11-17 12:48:45 +01:00
Logs().d('FluffyBox is ready');
2021-11-17 12:20:15 +01:00
return db;
}
2021-11-17 19:29:17 +01:00
static Future<sqflite.Database> _openSqlDatabase(
Client client,
String? password,
) async {
2021-11-17 19:17:40 +01:00
final path = await _findDatabasePath(client);
2021-11-18 07:59:35 +01:00
try {
if (Platform.isAndroid || Platform.isIOS) {
//opensqflite.open.overrideFor(sqlite3.OperatingSystem.android, openCipherOnAndroid);
final db = await sqflite.openDatabase(path, password: password);
return db;
}
final db = await ffi.databaseFactoryFfi.openDatabase(path);
return db;
} catch (_) {
File(path).delete();
rethrow;
}
2021-11-17 12:20:15 +01:00
}
static Future<String> _findDatabasePath(Client client) async {
String path = client.clientName;
if (!kIsWeb) {
Directory directory;
try {
directory = await getApplicationSupportDirectory();
} catch (_) {
try {
directory = await getLibraryDirectory();
} catch (_) {
directory = Directory.current;
}
}
2021-11-17 19:17:40 +01:00
path =
'${directory.path}${client.clientName.replaceAll(' ', '-')}.sqflite';
2021-11-17 12:20:15 +01:00
}
return path;
}
@override
int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0;
@override
2021-11-17 19:29:17 +01:00
bool get supportsFileStoring => !kIsWeb;
2021-11-17 12:20:15 +01:00
Future<String> _getFileStoreDirectory() async {
try {
try {
return (await getApplicationSupportDirectory()).path;
} catch (_) {
return (await getApplicationDocumentsDirectory()).path;
}
} catch (_) {
2021-11-17 19:29:17 +01:00
return (await getDownloadsDirectory())!.path;
2021-11-17 12:20:15 +01:00
}
}
@override
2021-11-17 19:29:17 +01:00
Future<Uint8List?> getFile(Uri mxcUri) async {
2021-11-17 12:20:15 +01:00
if (!supportsFileStoring) return null;
final tempDirectory = await _getFileStoreDirectory();
final file =
File('$tempDirectory/${Uri.encodeComponent(mxcUri.toString())}');
if (await file.exists() == false) return null;
final bytes = await file.readAsBytes();
return bytes;
}
@override
Future storeFile(Uri mxcUri, Uint8List bytes, int time) async {
if (!supportsFileStoring) return null;
final tempDirectory = await _getFileStoreDirectory();
final file =
File('$tempDirectory/${Uri.encodeComponent(mxcUri.toString())}');
if (await file.exists()) return;
await file.writeAsBytes(bytes);
return;
}
}