selfprivacy.org.app/lib/logic/cubit/backups/backups_cubit.dart

219 lines
7.4 KiB
Dart
Raw Normal View History

2021-12-06 18:31:19 +00:00
import 'dart:async';
import 'package:easy_localization/easy_localization.dart';
2021-12-06 18:31:19 +00:00
import 'package:selfprivacy/config/get_it_config.dart';
2022-07-12 12:54:16 +00:00
import 'package:selfprivacy/logic/api_maps/rest_maps/backblaze.dart';
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server_api.dart';
2021-12-06 18:31:19 +00:00
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
import 'package:selfprivacy/logic/models/hive/backblaze_bucket.dart';
2023-06-29 09:52:09 +00:00
import 'package:selfprivacy/logic/models/backup.dart';
import 'package:selfprivacy/logic/models/hive/backups_credential.dart';
import 'package:selfprivacy/logic/models/initialize_repository_input.dart';
import 'package:selfprivacy/logic/models/service.dart';
2021-12-06 18:31:19 +00:00
part 'backups_state.dart';
class BackupsCubit extends ServerInstallationDependendCubit<BackupsState> {
2022-06-05 19:36:32 +00:00
BackupsCubit(final ServerInstallationCubit serverInstallationCubit)
2022-05-24 18:55:39 +00:00
: super(
serverInstallationCubit,
const BackupsState(preventActions: true),
);
2021-12-06 18:31:19 +00:00
2022-06-05 19:36:32 +00:00
final ServerApi api = ServerApi();
final BackblazeApi backblaze = BackblazeApi();
2021-12-06 18:31:19 +00:00
2022-05-24 18:55:39 +00:00
@override
2021-12-06 18:31:19 +00:00
Future<void> load() async {
if (serverInstallationCubit.state is ServerInstallationFinished) {
2022-06-05 19:36:32 +00:00
final BackblazeBucket? bucket = getIt<ApiConfigModel>().backblazeBucket;
2023-06-29 09:52:09 +00:00
final BackupConfiguration? backupConfig =
await api.getBackupsConfiguration();
final List<Backup> backups = await api.getBackups();
backups.sort((final a, final b) => b.time.compareTo(a.time));
2023-06-29 09:52:09 +00:00
emit(
state.copyWith(
backblazeBucket: bucket,
isInitialized: backupConfig?.isInitialized,
autobackupPeriod: backupConfig?.autobackupPeriod ?? Duration.zero,
2023-06-29 09:52:09 +00:00
backups: backups,
preventActions: false,
refreshing: false,
),
);
print(state);
2021-12-06 18:31:19 +00:00
}
}
2023-06-29 09:52:09 +00:00
Future<void> initializeBackups() async {
2021-12-06 18:31:19 +00:00
emit(state.copyWith(preventActions: true));
2023-06-29 09:52:09 +00:00
final String? encryptionKey =
(await api.getBackupsConfiguration())?.encryptionKey;
if (encryptionKey == null) {
getIt<NavigationService>()
.showSnackBar("Couldn't get encryption key from your server.");
emit(state.copyWith(preventActions: false));
return;
2021-12-06 18:31:19 +00:00
}
2023-06-29 09:52:09 +00:00
final BackblazeBucket bucket;
if (state.backblazeBucket == null) {
final String domain = serverInstallationCubit
.state.serverDomain!.domainName
.replaceAll(RegExp(r'[^a-zA-Z0-9]'), '-');
final int serverId = serverInstallationCubit.state.serverDetails!.id;
String bucketName = 'selfprivacy-$domain-$serverId';
// If bucket name is too long, shorten it
if (bucketName.length > 49) {
bucketName = bucketName.substring(0, 49);
}
final String bucketId = await backblaze.createBucket(bucketName);
final BackblazeApplicationKey key = await backblaze.createKey(bucketId);
bucket = BackblazeBucket(
bucketId: bucketId,
bucketName: bucketName,
applicationKey: key.applicationKey,
applicationKeyId: key.applicationKeyId,
encryptionKey: encryptionKey,
);
await getIt<ApiConfigModel>().storeBackblazeBucket(bucket);
emit(state.copyWith(backblazeBucket: bucket));
} else {
bucket = state.backblazeBucket!;
}
final GenericResult result = await api.initializeRepository(
InitializeRepositoryInput(
provider: BackupsProviderType.backblaze,
locationId: bucket.bucketId,
locationName: bucket.bucketName,
login: bucket.applicationKeyId,
password: bucket.applicationKey,
),
);
if (result.success == false) {
getIt<NavigationService>()
.showSnackBar(result.message ?? 'Unknown error');
emit(state.copyWith(preventActions: false));
return;
}
2021-12-06 18:31:19 +00:00
await updateBackups();
2023-06-29 09:52:09 +00:00
getIt<NavigationService>().showSnackBar(
'Backups repository is now initializing. It may take a while.',
);
2021-12-06 18:31:19 +00:00
2023-06-29 09:52:09 +00:00
emit(state.copyWith(preventActions: false));
2021-12-06 18:31:19 +00:00
}
Future<void> reuploadKey() async {
emit(state.copyWith(preventActions: true));
2022-06-05 19:36:32 +00:00
final BackblazeBucket? bucket = getIt<ApiConfigModel>().backblazeBucket;
2021-12-06 18:31:19 +00:00
if (bucket == null) {
emit(state.copyWith(isInitialized: false));
2023-06-29 09:52:09 +00:00
print('bucket is null');
2021-12-06 18:31:19 +00:00
} else {
2023-06-29 09:52:09 +00:00
print('bucket is not null');
final GenericResult result = await api.initializeRepository(
InitializeRepositoryInput(
provider: BackupsProviderType.backblaze,
locationId: bucket.bucketId,
locationName: bucket.bucketName,
login: bucket.applicationKeyId,
password: bucket.applicationKey,
),
);
print('result is $result');
if (result.success == false) {
getIt<NavigationService>()
.showSnackBar(result.message ?? 'Unknown error');
emit(state.copyWith(preventActions: false));
return;
} else {
emit(state.copyWith(preventActions: false));
getIt<NavigationService>().showSnackBar('backup.reuploaded_key');
await updateBackups();
}
2021-12-06 18:31:19 +00:00
}
}
2023-06-29 09:52:09 +00:00
@Deprecated("we don't have states")
Duration refreshTimeFromState() => const Duration(seconds: 60);
2021-12-06 18:31:19 +00:00
2022-06-05 19:36:32 +00:00
Future<void> updateBackups({final bool useTimer = false}) async {
2021-12-06 18:31:19 +00:00
emit(state.copyWith(refreshing: true));
2023-06-29 09:52:09 +00:00
final backups = await api.getBackups();
backups.sort((final a, final b) => b.time.compareTo(a.time));
2023-06-29 09:52:09 +00:00
final backupConfig = await api.getBackupsConfiguration();
2023-06-22 14:14:00 +00:00
emit(
state.copyWith(
backups: backups,
2023-06-29 09:52:09 +00:00
refreshTimer: refreshTimeFromState(),
refreshing: false,
2023-06-29 09:52:09 +00:00
isInitialized: backupConfig?.isInitialized ?? false,
autobackupPeriod: backupConfig?.autobackupPeriod,
),
);
2022-05-24 18:55:39 +00:00
if (useTimer) {
2021-12-06 18:31:19 +00:00
Timer(state.refreshTimer, () => updateBackups(useTimer: true));
2022-05-24 18:55:39 +00:00
}
2021-12-06 18:31:19 +00:00
}
Future<void> forceUpdateBackups() async {
emit(state.copyWith(preventActions: true));
getIt<NavigationService>().showSnackBar('backup.refetching_list'.tr());
await api.forceBackupListReload();
emit(state.copyWith(preventActions: false));
}
2023-06-29 09:52:09 +00:00
Future<void> createMultipleBackups(final List<Service> services) async {
emit(state.copyWith(preventActions: true));
for (final service in services) {
await api.startBackup(service.id);
}
await updateBackups();
emit(state.copyWith(preventActions: false));
}
Future<void> createBackup(final String serviceId) async {
2021-12-06 18:31:19 +00:00
emit(state.copyWith(preventActions: true));
2023-06-29 09:52:09 +00:00
await api.startBackup(serviceId);
2021-12-06 18:31:19 +00:00
await updateBackups();
emit(state.copyWith(preventActions: false));
}
2022-06-05 19:36:32 +00:00
Future<void> restoreBackup(final String backupId) async {
2021-12-06 18:31:19 +00:00
emit(state.copyWith(preventActions: true));
await api.restoreBackup(backupId);
2021-12-06 18:31:19 +00:00
emit(state.copyWith(preventActions: false));
}
Future<void> setAutobackupPeriod(final Duration? period) async {
emit(state.copyWith(preventActions: true));
final result = await api.setAutobackupPeriod(period: period?.inMinutes);
if (result.success == false) {
getIt<NavigationService>()
.showSnackBar(result.message ?? 'Unknown error');
emit(state.copyWith(preventActions: false));
} else {
getIt<NavigationService>()
.showSnackBar('backup.autobackup_period_set'.tr());
emit(
state.copyWith(
preventActions: false,
autobackupPeriod: period ?? Duration.zero,
),
);
}
await updateBackups();
}
2021-12-06 18:31:19 +00:00
@override
void clear() async {
2022-05-24 18:55:39 +00:00
emit(const BackupsState());
2021-12-06 18:31:19 +00:00
}
}