fluffychat/lib/utils/matrix_sdk_extensions/flutter_hive_collections_database.dart

153 lines
4.7 KiB
Dart
Raw Normal View History

2021-06-11 11:40:38 +02:00
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart' hide Key;
2021-06-11 11:40:38 +02:00
import 'package:flutter/services.dart';
2021-10-26 18:50:34 +02:00
2021-06-11 11:40:38 +02:00
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hive/hive.dart';
2021-10-26 18:50:34 +02:00
import 'package:matrix/matrix.dart';
2021-06-11 11:40:38 +02:00
import 'package:path_provider/path_provider.dart';
2023-05-23 15:21:38 +02:00
import 'package:universal_html/html.dart' as html;
2021-06-11 11:40:38 +02:00
class FlutterHiveCollectionsDatabase extends HiveCollectionsDatabase {
FlutterHiveCollectionsDatabase(
String name,
String path, {
HiveCipher? key,
}) : super(
2021-06-11 11:40:38 +02:00
name,
path,
key: key,
2021-06-14 08:18:55 +02:00
);
2021-06-11 11:40:38 +02:00
2022-08-28 10:15:56 +02:00
static const String _cipherStorageKey = 'hive_encryption_key';
2021-06-11 11:40:38 +02:00
static Future<FlutterHiveCollectionsDatabase> databaseBuilder(
Client client,
) async {
Logs().d('Open Hive...');
HiveAesCipher? hiverCipher;
2021-06-11 11:40:38 +02:00
try {
2021-06-12 13:41:22 +02:00
// Workaround for secure storage is calling Platform.operatingSystem on web
2023-05-23 15:21:38 +02:00
if (kIsWeb) {
// ignore: unawaited_futures
html.window.navigator.storage?.persist();
throw MissingPluginException();
}
2021-06-12 13:41:22 +02:00
2021-10-14 18:09:30 +02:00
const secureStorage = FlutterSecureStorage();
2021-06-11 11:40:38 +02:00
final containsEncryptionKey =
await secureStorage.read(key: _cipherStorageKey) != null;
2021-06-11 11:40:38 +02:00
if (!containsEncryptionKey) {
// do not try to create a buggy secure storage for new Linux users
if (Platform.isLinux) throw MissingPluginException();
2021-06-11 11:40:38 +02:00
final key = Hive.generateSecureKey();
await secureStorage.write(
key: _cipherStorageKey,
2021-06-11 11:40:38 +02:00
value: base64UrlEncode(key),
);
}
// workaround for if we just wrote to the key and it still doesn't exist
final rawEncryptionKey = await secureStorage.read(key: _cipherStorageKey);
if (rawEncryptionKey == null) throw MissingPluginException();
hiverCipher = HiveAesCipher(base64Url.decode(rawEncryptionKey));
2021-06-11 11:40:38 +02:00
} on MissingPluginException catch (_) {
const FlutterSecureStorage()
.delete(key: _cipherStorageKey)
.catchError((_) {});
2021-06-14 08:18:55 +02:00
Logs().i('Hive encryption is not supported on this platform');
} catch (e, s) {
const FlutterSecureStorage()
.delete(key: _cipherStorageKey)
.catchError((_) {});
Logs().w('Unable to init Hive encryption', e, s);
2021-06-11 11:40:38 +02:00
}
final db = FlutterHiveCollectionsDatabase(
2022-08-28 08:03:53 +02:00
'hive_collections_${client.clientName.replaceAll(' ', '_').toLowerCase()}',
await _findDatabasePath(client),
key: hiverCipher,
2021-06-11 11:40:38 +02:00
);
try {
await db.open();
2022-08-28 07:04:11 +02:00
} catch (e, s) {
Logs().w('Unable to open Hive. Delete database and storage key...', e, s);
const FlutterSecureStorage().delete(key: _cipherStorageKey);
2022-08-28 10:15:56 +02:00
await db.clear().catchError((_) {});
await Hive.deleteFromDisk();
rethrow;
}
Logs().d('Hive is ready');
2021-06-11 11:40:38 +02:00
return db;
}
static Future<String> _findDatabasePath(Client client) async {
String path = client.clientName;
if (!kIsWeb) {
Directory directory;
try {
if (Platform.isLinux) {
directory = await getApplicationSupportDirectory();
} else {
directory = await getApplicationDocumentsDirectory();
}
} catch (_) {
try {
directory = await getLibraryDirectory();
} catch (_) {
directory = Directory.current;
}
}
// do not destroy your stable FluffyChat in debug mode
directory = Directory(
directory.uri.resolve(kDebugMode ? 'hive_debug' : 'hive').toFilePath(),
);
2022-08-28 10:15:56 +02:00
directory.create(recursive: true);
path = directory.path;
}
return path;
}
2021-06-11 11:40:38 +02:00
@override
2021-06-14 08:18:55 +02:00
int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0;
2021-06-11 11:40:38 +02:00
@override
bool get supportsFileStoring => !kIsWeb;
2021-06-11 11:40:38 +02:00
2021-06-14 08:18:55 +02:00
Future<String> _getFileStoreDirectory() async {
try {
try {
return (await getTemporaryDirectory()).path;
} catch (_) {
return (await getApplicationDocumentsDirectory()).path;
}
} catch (_) {
2022-01-29 12:35:03 +01:00
return (await getDownloadsDirectory())!.path;
2021-06-14 08:18:55 +02:00
}
2021-06-11 11:40:38 +02:00
}
@override
2022-01-29 12:35:03 +01:00
Future<Uint8List?> getFile(Uri mxcUri) async {
2021-06-14 08:18:55 +02:00
if (!supportsFileStoring) return null;
final tempDirectory = await _getFileStoreDirectory();
2021-08-26 19:03:08 +02:00
final file =
File('$tempDirectory/${Uri.encodeComponent(mxcUri.toString())}');
2021-06-11 11:40:38 +02:00
if (await file.exists() == false) return null;
final bytes = await file.readAsBytes();
2021-06-14 08:18:55 +02:00
return bytes;
2021-06-11 11:40:38 +02:00
}
@override
2021-08-26 19:03:08 +02:00
Future storeFile(Uri mxcUri, Uint8List bytes, int time) async {
2021-06-14 08:18:55 +02:00
if (!supportsFileStoring) return null;
final tempDirectory = await _getFileStoreDirectory();
2021-08-26 19:03:08 +02:00
final file =
File('$tempDirectory/${Uri.encodeComponent(mxcUri.toString())}');
2021-06-11 11:40:38 +02:00
if (await file.exists()) return;
2021-06-14 08:18:55 +02:00
await file.writeAsBytes(bytes);
2021-06-11 11:40:38 +02:00
return;
}
}