fluffychat/lib/utils/matrix_sdk_extensions.dart/flutter_famedly_sdk_hive_database.dart

113 lines
3.5 KiB
Dart
Raw Normal View History

2021-06-11 09:40:38 +00:00
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:matrix/matrix.dart';
2021-06-11 09:40:38 +00:00
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:path_provider/path_provider.dart';
import '../platform_infos.dart';
2021-06-11 09:40:38 +00:00
class FlutterFamedlySdkHiveDatabase extends FamedlySdkHiveDatabase {
FlutterFamedlySdkHiveDatabase(String name, {HiveCipher encryptionCipher})
: super(
name,
encryptionCipher: encryptionCipher,
2021-06-14 06:18:55 +00:00
);
2021-06-11 09:40:38 +00:00
static bool _hiveInitialized = false;
static const String _hiveCipherStorageKey = 'hive_encryption_key';
static Future<FamedlySdkHiveDatabase> hiveDatabaseBuilder(
Client client) async {
if (!kIsWeb && !_hiveInitialized) {
Logs().i('Init Hive database...');
if (PlatformInfos.isLinux) {
2021-06-29 06:49:47 +00:00
Hive.init((await getApplicationSupportDirectory()).path);
2021-06-29 05:59:37 +00:00
} else {
await Hive.initFlutter();
}
2021-06-11 09:40:38 +00:00
_hiveInitialized = true;
}
HiveCipher hiverCipher;
try {
2021-06-12 11:41:22 +00:00
// Workaround for secure storage is calling Platform.operatingSystem on web
if (kIsWeb) throw MissingPluginException();
2021-06-11 09:40:38 +00:00
final secureStorage = const FlutterSecureStorage();
final containsEncryptionKey =
await secureStorage.containsKey(key: _hiveCipherStorageKey);
if (!containsEncryptionKey) {
final key = Hive.generateSecureKey();
await secureStorage.write(
key: _hiveCipherStorageKey,
value: base64UrlEncode(key),
);
}
final encryptionKey = base64Url.decode(
await secureStorage.read(key: _hiveCipherStorageKey),
);
hiverCipher = HiveAesCipher(encryptionKey);
} on MissingPluginException catch (_) {
2021-06-14 06:18:55 +00:00
Logs().i('Hive encryption is not supported on this platform');
2021-06-11 09:40:38 +00:00
}
final db = FlutterFamedlySdkHiveDatabase(
2021-06-11 09:40:38 +00:00
client.clientName,
encryptionCipher: hiverCipher,
);
Logs().i('Open Hive database...');
await db.open();
Logs().i('Hive database is ready!');
return db;
}
@override
2021-06-14 06:18:55 +00:00
int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0;
2021-06-11 09:40:38 +00:00
@override
bool get supportsFileStoring => (PlatformInfos.isIOS ||
PlatformInfos.isAndroid ||
PlatformInfos.isDesktop);
2021-06-11 09:40:38 +00:00
2021-06-14 06:18:55 +00:00
Future<String> _getFileStoreDirectory() async {
try {
try {
return (await getApplicationSupportDirectory()).path;
} on MissingPlatformDirectoryException {
return (await getApplicationDocumentsDirectory()).path;
}
} on MissingPlatformDirectoryException {
2021-06-14 06:18:55 +00:00
return (await getDownloadsDirectory()).path;
}
2021-06-11 09:40:38 +00:00
}
@override
Future<Uint8List> getFile(String mxcUri) async {
2021-06-14 06:18:55 +00:00
if (!supportsFileStoring) return null;
final tempDirectory = await _getFileStoreDirectory();
final file = File('$tempDirectory/${Uri.encodeComponent(mxcUri)}');
2021-06-11 09:40:38 +00:00
if (await file.exists() == false) return null;
final bytes = await file.readAsBytes();
2021-06-14 06:18:55 +00:00
return bytes;
2021-06-11 09:40:38 +00:00
}
@override
Future storeFile(String mxcUri, Uint8List bytes, int time) async {
2021-06-14 06:18:55 +00:00
if (!supportsFileStoring) return null;
final tempDirectory = await _getFileStoreDirectory();
final file = File('$tempDirectory/${Uri.encodeComponent(mxcUri)}');
2021-06-11 09:40:38 +00:00
if (await file.exists()) return;
2021-06-14 06:18:55 +00:00
await file.writeAsBytes(bytes);
2021-06-11 09:40:38 +00:00
return;
}
@override
Future<void> clear(int clientId) async {
await super.clear(clientId);
}
}