mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-02-02 14:16:58 +00:00
feat: Implement distinction for connection errors on storage page
Now user gets notified when connection error occurs
This commit is contained in:
parent
6b5a4f7875
commit
1dfd2180d2
|
@ -2,9 +2,12 @@ import 'dart:io';
|
|||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:selfprivacy/config/get_it_config.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/api_generic_result.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/api_map.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
||||
|
||||
export 'package:selfprivacy/logic/api_maps/api_generic_result.dart';
|
||||
|
||||
class BackblazeApiAuth {
|
||||
BackblazeApiAuth({required this.authorizationToken, required this.apiUrl});
|
||||
|
||||
|
@ -71,28 +74,46 @@ class BackblazeApi extends ApiMap {
|
|||
);
|
||||
}
|
||||
|
||||
Future<bool> isValid(final String encodedApiKey) async {
|
||||
Future<APIGenericResult<bool>> isApiTokenValid(
|
||||
final String encodedApiKey,
|
||||
) async {
|
||||
final Dio client = await getClient();
|
||||
bool isTokenValid = false;
|
||||
try {
|
||||
final Response response = await client.get(
|
||||
'b2_authorize_account',
|
||||
options: Options(headers: {'Authorization': 'Basic $encodedApiKey'}),
|
||||
options: Options(
|
||||
followRedirects: false,
|
||||
validateStatus: (final status) =>
|
||||
status != null && (status >= 200 || status == 401),
|
||||
headers: {'Authorization': 'Basic $encodedApiKey'},
|
||||
),
|
||||
);
|
||||
if (response.statusCode == HttpStatus.ok) {
|
||||
if (response.data['allowed']['capabilities'].contains('listBuckets')) {
|
||||
return true;
|
||||
isTokenValid = true;
|
||||
}
|
||||
return false;
|
||||
isTokenValid = false;
|
||||
} else if (response.statusCode == HttpStatus.unauthorized) {
|
||||
return false;
|
||||
isTokenValid = false;
|
||||
} else {
|
||||
throw Exception('code: ${response.statusCode}');
|
||||
}
|
||||
} on DioError {
|
||||
return false;
|
||||
} on DioError catch (e) {
|
||||
print(e);
|
||||
return APIGenericResult(
|
||||
data: false,
|
||||
success: false,
|
||||
message: e.toString(),
|
||||
);
|
||||
} finally {
|
||||
close(client);
|
||||
}
|
||||
|
||||
return APIGenericResult(
|
||||
data: isTokenValid,
|
||||
success: true,
|
||||
);
|
||||
}
|
||||
|
||||
// Create bucket
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
import 'dart:async';
|
||||
import 'package:cubit_form/cubit_form.dart';
|
||||
import 'package:selfprivacy/config/get_it_config.dart';
|
||||
import 'package:selfprivacy/logic/api_maps/rest_maps/backblaze.dart';
|
||||
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||
import 'package:selfprivacy/logic/get_it/navigation.dart';
|
||||
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
|
||||
class BackblazeFormCubit extends FormCubit {
|
||||
BackblazeFormCubit(this.serverInstallationCubit) {
|
||||
//var regExp = RegExp(r"\s+|[-!$%^&*()@+|~=`{}\[\]:<>?,.\/]");
|
||||
keyId = FieldCubit(
|
||||
initalValue: '',
|
||||
validations: [
|
||||
|
@ -40,7 +41,7 @@ class BackblazeFormCubit extends FormCubit {
|
|||
|
||||
@override
|
||||
FutureOr<bool> asyncValidation() async {
|
||||
late bool isKeyValid;
|
||||
late APIGenericResult<bool> backblazeResponse;
|
||||
final BackblazeApi apiClient = BackblazeApi(isWithToken: false);
|
||||
|
||||
try {
|
||||
|
@ -48,18 +49,30 @@ class BackblazeFormCubit extends FormCubit {
|
|||
keyId.state.value,
|
||||
applicationKey.state.value,
|
||||
);
|
||||
isKeyValid = await apiClient.isValid(encodedApiKey);
|
||||
backblazeResponse = await apiClient.isApiTokenValid(encodedApiKey);
|
||||
} catch (e) {
|
||||
addError(e);
|
||||
isKeyValid = false;
|
||||
backblazeResponse = APIGenericResult(
|
||||
success: false,
|
||||
data: false,
|
||||
message: e.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
if (!isKeyValid) {
|
||||
keyId.setError('initializing.backblaze_bad_key_error'.tr());
|
||||
applicationKey.setError('initializing.backblaze_bad_key_error'.tr());
|
||||
if (!backblazeResponse.success) {
|
||||
getIt<NavigationService>().showSnackBar(
|
||||
'initializing.could_not_connect'.tr(),
|
||||
);
|
||||
keyId.setError('');
|
||||
applicationKey.setError('');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
if (!backblazeResponse.data) {
|
||||
keyId.setError('initializing.backblaze_bad_key_error'.tr());
|
||||
applicationKey.setError('initializing.backblaze_bad_key_error'.tr());
|
||||
}
|
||||
|
||||
return backblazeResponse.data;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue