From 82dfdf04f9f306f66e3bd2d52553ea862b4b4ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?NaiJi=20=E2=9C=A8?= Date: Wed, 6 Sep 2023 00:36:49 +0300 Subject: [PATCH] fix: Implement Backblaze bucket restoration on server recovery (#324) Resolves issue [320](https://git.selfprivacy.org/SelfPrivacy/selfprivacy.org.app/issues/320). Co-authored-by: NaiJi Reviewed-on: https://git.selfprivacy.org/SelfPrivacy/selfprivacy.org.app/pulls/324 Reviewed-by: Inex Code --- lib/logic/api_maps/rest_maps/backblaze.dart | 38 +++++++++++++++++++ lib/logic/cubit/backups/backups_cubit.dart | 25 ++++++++++-- .../server_installation_cubit.dart | 17 +++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/lib/logic/api_maps/rest_maps/backblaze.dart b/lib/logic/api_maps/rest_maps/backblaze.dart index b9df76c2..d908cd26 100644 --- a/lib/logic/api_maps/rest_maps/backblaze.dart +++ b/lib/logic/api_maps/rest_maps/backblaze.dart @@ -2,6 +2,8 @@ import 'dart:io'; import 'package:dio/dio.dart'; import 'package:selfprivacy/config/get_it_config.dart'; +import 'package:selfprivacy/logic/models/backup.dart'; +import 'package:selfprivacy/logic/models/hive/backblaze_bucket.dart'; import 'package:selfprivacy/logic/models/hive/backups_credential.dart'; import 'package:selfprivacy/logic/api_maps/generic_result.dart'; import 'package:selfprivacy/logic/api_maps/rest_maps/rest_api_map.dart'; @@ -179,6 +181,42 @@ class BackblazeApi extends RestApiMap { } } + Future fetchBucket( + final BackupsCredential credentials, + final BackupConfiguration configuration, + ) async { + BackblazeBucket? bucket; + final BackblazeApiAuth auth = await getAuthorizationToken(); + final Dio client = await getClient(); + client.options.baseUrl = auth.apiUrl; + final Response response = await client.get( + '$apiPrefix/b2_list_buckets', + queryParameters: { + 'accountId': getIt().backblazeCredential!.keyId, + }, + options: Options( + headers: {'Authorization': auth.authorizationToken}, + ), + ); + close(client); + if (response.statusCode == HttpStatus.ok) { + for (final rawBucket in response.data['buckets']) { + if (rawBucket['bucketId'] == configuration.locationId) { + bucket = BackblazeBucket( + bucketId: rawBucket['bucketId'], + bucketName: rawBucket['bucketName'], + encryptionKey: configuration.encryptionKey, + applicationKeyId: '', + applicationKey: '', + ); + } + } + return bucket; + } else { + throw Exception('code: ${response.statusCode}'); + } + } + @override bool hasLogger; diff --git a/lib/logic/cubit/backups/backups_cubit.dart b/lib/logic/cubit/backups/backups_cubit.dart index 938b606a..89470656 100644 --- a/lib/logic/cubit/backups/backups_cubit.dart +++ b/lib/logic/cubit/backups/backups_cubit.dart @@ -109,17 +109,34 @@ class BackupsCubit extends ServerInstallationDependendCubit { Future reuploadKey() async { emit(state.copyWith(preventActions: true)); - final BackblazeBucket? bucket = getIt().backblazeBucket; + BackblazeBucket? bucket = getIt().backblazeBucket; if (bucket == null) { emit(state.copyWith(isInitialized: false)); } else { + String login = bucket.applicationKeyId; + String password = bucket.applicationKey; + if (login.isEmpty || password.isEmpty) { + final BackblazeApplicationKey key = + await backblaze.createKey(bucket.bucketId); + login = key.applicationKeyId; + password = key.applicationKey; + bucket = BackblazeBucket( + bucketId: bucket.bucketId, + bucketName: bucket.bucketName, + encryptionKey: bucket.encryptionKey, + applicationKey: password, + applicationKeyId: login, + ); + await getIt().storeBackblazeBucket(bucket); + emit(state.copyWith(backblazeBucket: bucket)); + } final GenericResult result = await api.initializeRepository( InitializeRepositoryInput( provider: BackupsProviderType.backblaze, locationId: bucket.bucketId, locationName: bucket.bucketName, - login: bucket.applicationKeyId, - password: bucket.applicationKey, + login: login, + password: password, ), ); if (result.success == false) { @@ -129,7 +146,7 @@ class BackupsCubit extends ServerInstallationDependendCubit { return; } else { emit(state.copyWith(preventActions: false)); - getIt().showSnackBar('backup.reuploaded_key'); + getIt().showSnackBar('backup.reuploaded_key'.tr()); await updateBackups(); } } diff --git a/lib/logic/cubit/server_installation/server_installation_cubit.dart b/lib/logic/cubit/server_installation/server_installation_cubit.dart index a10532ef..195c5ded 100644 --- a/lib/logic/cubit/server_installation/server_installation_cubit.dart +++ b/lib/logic/cubit/server_installation/server_installation_cubit.dart @@ -5,7 +5,9 @@ import 'package:easy_localization/easy_localization.dart'; import 'package:equatable/equatable.dart'; import 'package:selfprivacy/config/get_it_config.dart'; import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server_api.dart'; +import 'package:selfprivacy/logic/api_maps/rest_maps/backblaze.dart'; import 'package:selfprivacy/logic/api_maps/tls_options.dart'; +import 'package:selfprivacy/logic/models/hive/backblaze_bucket.dart'; import 'package:selfprivacy/logic/models/hive/backups_credential.dart'; import 'package:selfprivacy/logic/models/callback_dialogue_branching.dart'; import 'package:selfprivacy/logic/models/hive/server_details.dart'; @@ -199,8 +201,23 @@ class ServerInstallationCubit extends Cubit { applicationKey: applicationKey, provider: BackupsProviderType.backblaze, ); + final BackblazeBucket? bucket; await repository.saveBackblazeKey(backblazeCredential); if (state is ServerInstallationRecovery) { + final configuration = await ServerApi( + customToken: + (state as ServerInstallationRecovery).serverDetails!.apiToken, + isWithToken: true, + ).getBackupsConfiguration(); + if (configuration != null) { + try { + bucket = await BackblazeApi() + .fetchBucket(backblazeCredential, configuration); + await getIt().storeBackblazeBucket(bucket!); + } catch (e) { + print(e); + } + } finishRecoveryProcess(backblazeCredential); return; }