fix(config): Prevent database from being unloaded from memory on clear

This commit is contained in:
Jagandeep Brar
2022-01-22 02:39:10 -05:00
parent a4c26f8038
commit cff45fcf46
3 changed files with 16 additions and 12 deletions

View File

@@ -27,7 +27,7 @@ class Database {
void bootstrap() {
clearAll();
for (_BoxInstance instance in _BoxInstance.values) instance.setDefaults();
for (_BoxInstance instance in _BoxInstance.values) instance.bootstrap();
}
void clearAll() {
@@ -96,7 +96,7 @@ extension _BoxInstanceExtension on _BoxInstance {
}
}
void setDefaults() {
void bootstrap() {
switch (this) {
case _BoxInstance.ALERTS:
break;
@@ -122,6 +122,6 @@ class _BoxManager<E> {
_BoxManager(this._instance);
Box<E> get box => Hive.box<E>(_instance.key);
Future<int> clear() async => box.clear();
Future<void> clear() async => box.keys.forEach((k) => box.delete(k));
Future<int> size() async => box.length;
}

View File

@@ -3,10 +3,10 @@ import 'package:lunasea/modules/settings.dart';
class LunaProfile {
/// Returns a list of the profiles, sorted by their lowercase key/display name.
List<String?> profilesList() => Database.profiles.box.keys
.map<String?>((profile) => profile as String?)
List<String> profilesList() => Database.profiles.box.keys
.map<String>((profile) => profile as String)
.toList()
..sort((a, b) => a!.toLowerCase().compareTo(b!.toLowerCase()));
..sort((a, b) => a.toLowerCase().compareTo(b.toLowerCase()));
/// Safely change profiles.
///

View File

@@ -22,9 +22,10 @@ class LunaEncryption {
return _encrypted;
} catch (error, stack) {
LunaLogger().error(
'Failed to decrypted "$data" using the key "$encryptionKey"',
error,
stack);
'Failed to decrypted "$data" using the key "$encryptionKey"',
error,
stack,
);
}
return ENCRYPTION_FAILURE;
}
@@ -41,9 +42,12 @@ class LunaEncryption {
IV iv = IV.fromLength(_IV_LENGTH);
final _encrypter = Encrypter(AES(key));
return _encrypter.decrypt64(data, iv: iv);
} catch (error) {
LunaLogger().warning('LunaEncryption', 'decrypt',
'Failed to decrypted "$data" using the key "$decryptionKey"');
} catch (error, stack) {
LunaLogger().error(
'Failed to decrypted "$data" using the key "$decryptionKey"',
error,
stack,
);
}
return ENCRYPTION_FAILURE;
}