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

178 lines
5.8 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';
import 'package:selfprivacy/logic/api_maps/backblaze.dart';
import 'package:selfprivacy/logic/api_maps/server.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/backblaze_bucket.dart';
import 'package:selfprivacy/logic/models/backup.dart';
part 'backups_state.dart';
class BackupsCubit extends AppConfigDependendCubit<BackupsState> {
BackupsCubit(AppConfigCubit appConfigCubit)
: super(appConfigCubit, BackupsState(preventActions: true));
final api = ServerApi();
final backblaze = BackblazeApi();
Future<void> load() async {
if (appConfigCubit.state is AppConfigFinished) {
final bucket = getIt<ApiConfigModel>().backblazeBucket;
if (bucket == null) {
emit(BackupsState(
isInitialized: false, preventActions: false, refreshing: false));
} else {
final status = await api.getBackupStatus();
switch (status.status) {
case BackupStatusEnum.noKey:
case BackupStatusEnum.notInitialized:
emit(BackupsState(
backups: [],
isInitialized: true,
preventActions: false,
progress: 0,
status: status.status,
refreshing: false,
));
break;
case BackupStatusEnum.initializing:
emit(BackupsState(
backups: [],
isInitialized: true,
preventActions: false,
progress: 0,
status: status.status,
refreshTimer: Duration(seconds: 10),
refreshing: false,
));
break;
case BackupStatusEnum.initialized:
case BackupStatusEnum.error:
final backups = await api.getBackups();
emit(BackupsState(
backups: backups,
isInitialized: true,
preventActions: false,
progress: status.progress,
status: status.status,
2021-12-09 03:22:33 +00:00
error: status.errorMessage ?? '',
2021-12-06 18:31:19 +00:00
refreshing: false,
));
break;
case BackupStatusEnum.backingUp:
case BackupStatusEnum.restoring:
final backups = await api.getBackups();
emit(BackupsState(
backups: backups,
isInitialized: true,
preventActions: true,
progress: status.progress,
status: status.status,
2021-12-09 03:22:33 +00:00
error: status.errorMessage ?? '',
2021-12-06 18:31:19 +00:00
refreshTimer: Duration(seconds: 5),
refreshing: false,
));
break;
default:
emit(BackupsState());
}
Timer(state.refreshTimer, () => updateBackups(useTimer: true));
}
}
}
Future<void> createBucket() async {
emit(state.copyWith(preventActions: true));
2022-02-01 01:56:05 +00:00
final domain = appConfigCubit.state.cloudFlareDomain!.domainName
.replaceAll(RegExp(r'[^a-zA-Z0-9]'), '-');
2021-12-06 18:31:19 +00:00
final serverId = appConfigCubit.state.hetznerServer!.id;
var bucketName = 'selfprivacy-$domain-$serverId';
// If bucket name is too long, shorten it
if (bucketName.length > 49) {
bucketName = bucketName.substring(0, 49);
}
final bucketId = await backblaze.createBucket(bucketName);
final key = await backblaze.createKey(bucketId);
final bucket = BackblazeBucket(
bucketId: bucketId,
bucketName: bucketName,
applicationKey: key.applicationKey,
applicationKeyId: key.applicationKeyId);
await getIt<ApiConfigModel>().storeBackblazeBucket(bucket);
await api.uploadBackblazeConfig(bucket);
await updateBackups();
emit(state.copyWith(isInitialized: true, preventActions: false));
}
Future<void> reuploadKey() async {
emit(state.copyWith(preventActions: true));
final bucket = getIt<ApiConfigModel>().backblazeBucket;
if (bucket == null) {
emit(state.copyWith(isInitialized: false));
} else {
await api.uploadBackblazeConfig(bucket);
emit(state.copyWith(isInitialized: true, preventActions: false));
getIt<NavigationService>().showSnackBar('providers.backup.reuploadedKey');
}
}
Duration refreshTimeFromState(BackupStatusEnum status) {
switch (status) {
case BackupStatusEnum.backingUp:
case BackupStatusEnum.restoring:
return Duration(seconds: 5);
case BackupStatusEnum.initializing:
return Duration(seconds: 10);
default:
return Duration(seconds: 60);
}
}
Future<void> updateBackups({bool useTimer = false}) async {
emit(state.copyWith(refreshing: true));
final backups = await api.getBackups();
final status = await api.getBackupStatus();
emit(state.copyWith(
backups: backups,
progress: status.progress,
status: status.status,
error: status.errorMessage,
refreshTimer: refreshTimeFromState(status.status),
refreshing: false,
));
if (useTimer)
Timer(state.refreshTimer, () => updateBackups(useTimer: true));
}
Future<void> forceUpdateBackups() async {
emit(state.copyWith(preventActions: true));
await api.forceBackupListReload();
2021-12-23 13:52:12 +00:00
getIt<NavigationService>()
.showSnackBar('providers.backup.refetchingList'.tr());
emit(state.copyWith(preventActions: false));
}
2021-12-06 18:31:19 +00:00
Future<void> createBackup() async {
emit(state.copyWith(preventActions: true));
await api.startBackup();
await updateBackups();
emit(state.copyWith(preventActions: false));
}
Future<void> restoreBackup(String backupId) async {
emit(state.copyWith(preventActions: true));
await api.restoreBackup(backupId);
emit(state.copyWith(preventActions: false));
}
@override
void clear() async {
emit(BackupsState());
}
}