fluffychat/lib/utils/matrix_sdk_extensions.dart/flutter_hive_collections_database.dart

147 lines
4.5 KiB
Dart
Raw Normal View History

2021-06-11 11:40:38 +02:00
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
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';
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
static const String _cipherStorageKey = 'database_encryption_key';
2021-06-11 11:40:38 +02:00
static Future<FlutterHiveCollectionsDatabase> databaseBuilder(
2021-06-11 11:40:38 +02:00
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
if (kIsWeb) 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);
2021-10-25 10:46:58 +02:00
await db.clear();
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
if (kDebugMode) {
directory = Directory(directory.uri.resolve('debug').toFilePath());
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 getApplicationSupportDirectory()).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;
}
}