feat: Implement distinction for connection errors on storage page
Now user gets notified when connection error occurspull/149/head
parent
6b5a4f7875
commit
1dfd2180d2
|
@ -2,9 +2,12 @@ import 'dart:io';
|
||||||
|
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:selfprivacy/config/get_it_config.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/api_maps/rest_maps/api_map.dart';
|
||||||
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
||||||
|
|
||||||
|
export 'package:selfprivacy/logic/api_maps/api_generic_result.dart';
|
||||||
|
|
||||||
class BackblazeApiAuth {
|
class BackblazeApiAuth {
|
||||||
BackblazeApiAuth({required this.authorizationToken, required this.apiUrl});
|
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();
|
final Dio client = await getClient();
|
||||||
|
bool isTokenValid = false;
|
||||||
try {
|
try {
|
||||||
final Response response = await client.get(
|
final Response response = await client.get(
|
||||||
'b2_authorize_account',
|
'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.statusCode == HttpStatus.ok) {
|
||||||
if (response.data['allowed']['capabilities'].contains('listBuckets')) {
|
if (response.data['allowed']['capabilities'].contains('listBuckets')) {
|
||||||
return true;
|
isTokenValid = true;
|
||||||
}
|
}
|
||||||
return false;
|
isTokenValid = false;
|
||||||
} else if (response.statusCode == HttpStatus.unauthorized) {
|
} else if (response.statusCode == HttpStatus.unauthorized) {
|
||||||
return false;
|
isTokenValid = false;
|
||||||
} else {
|
} else {
|
||||||
throw Exception('code: ${response.statusCode}');
|
throw Exception('code: ${response.statusCode}');
|
||||||
}
|
}
|
||||||
} on DioError {
|
} on DioError catch (e) {
|
||||||
return false;
|
print(e);
|
||||||
|
return APIGenericResult(
|
||||||
|
data: false,
|
||||||
|
success: false,
|
||||||
|
message: e.toString(),
|
||||||
|
);
|
||||||
} finally {
|
} finally {
|
||||||
close(client);
|
close(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return APIGenericResult(
|
||||||
|
data: isTokenValid,
|
||||||
|
success: true,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create bucket
|
// Create bucket
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:cubit_form/cubit_form.dart';
|
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/api_maps/rest_maps/backblaze.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.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:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
||||||
import 'package:easy_localization/easy_localization.dart';
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
|
|
||||||
class BackblazeFormCubit extends FormCubit {
|
class BackblazeFormCubit extends FormCubit {
|
||||||
BackblazeFormCubit(this.serverInstallationCubit) {
|
BackblazeFormCubit(this.serverInstallationCubit) {
|
||||||
//var regExp = RegExp(r"\s+|[-!$%^&*()@+|~=`{}\[\]:<>?,.\/]");
|
|
||||||
keyId = FieldCubit(
|
keyId = FieldCubit(
|
||||||
initalValue: '',
|
initalValue: '',
|
||||||
validations: [
|
validations: [
|
||||||
|
@ -40,7 +41,7 @@ class BackblazeFormCubit extends FormCubit {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
FutureOr<bool> asyncValidation() async {
|
FutureOr<bool> asyncValidation() async {
|
||||||
late bool isKeyValid;
|
late APIGenericResult<bool> backblazeResponse;
|
||||||
final BackblazeApi apiClient = BackblazeApi(isWithToken: false);
|
final BackblazeApi apiClient = BackblazeApi(isWithToken: false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -48,18 +49,30 @@ class BackblazeFormCubit extends FormCubit {
|
||||||
keyId.state.value,
|
keyId.state.value,
|
||||||
applicationKey.state.value,
|
applicationKey.state.value,
|
||||||
);
|
);
|
||||||
isKeyValid = await apiClient.isValid(encodedApiKey);
|
backblazeResponse = await apiClient.isApiTokenValid(encodedApiKey);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
addError(e);
|
addError(e);
|
||||||
isKeyValid = false;
|
backblazeResponse = APIGenericResult(
|
||||||
|
success: false,
|
||||||
|
data: false,
|
||||||
|
message: e.toString(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isKeyValid) {
|
if (!backblazeResponse.success) {
|
||||||
keyId.setError('initializing.backblaze_bad_key_error'.tr());
|
getIt<NavigationService>().showSnackBar(
|
||||||
applicationKey.setError('initializing.backblaze_bad_key_error'.tr());
|
'initializing.could_not_connect'.tr(),
|
||||||
|
);
|
||||||
|
keyId.setError('');
|
||||||
|
applicationKey.setError('');
|
||||||
return false;
|
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 New Issue