Fix profile update bug

This commit is contained in:
problematicconsumer
2023-09-22 23:52:20 +03:30
parent 9cd4fe48bd
commit 92538d137b
11 changed files with 73 additions and 32 deletions

View File

@@ -24,10 +24,12 @@ AppDatabase appDatabase(AppDatabaseRef ref) => AppDatabase.connect();
SharedPreferences sharedPreferences(SharedPreferencesRef ref) =>
throw UnimplementedError('sharedPreferences must be overridden');
// TODO: set options for dio
@Riverpod(keepAlive: true)
Dio dio(DioRef ref) => Dio(
BaseOptions(
connectTimeout: const Duration(seconds: 15),
sendTimeout: const Duration(seconds: 15),
receiveTimeout: const Duration(seconds: 15),
headers: {
"User-Agent": ref.watch(appInfoProvider).userAgent,
},

View File

@@ -57,11 +57,15 @@ class CoreFacadeImpl with ExceptionHandler, InfraLogger implements CoreFacade {
}
@override
TaskEither<CoreServiceFailure, Unit> parseConfig(String path) {
TaskEither<CoreServiceFailure, Unit> parseConfig(
String path,
String tempPath,
bool debug,
) {
return exceptionHandler(
() {
return singbox
.parseConfig(path)
.parseConfig(path, tempPath, debug)
.mapLeft(CoreServiceFailure.invalidConfig)
.run();
},

View File

@@ -198,21 +198,27 @@ class ProfilesRepositoryImpl
) {
return TaskEither(
() async {
final tempPath = filesEditor.configPath("temp_$fileName");
final path = filesEditor.configPath(fileName);
final response = await dio.download(url.trim(), path);
final headers = await _populateHeaders(response.headers.map, path);
final parseResult = await singbox.parseConfig(path).run();
return parseResult.fold(
(l) async {
await File(path).delete();
loggy.warning("error parsing config: $l");
return left(ProfileFailure.invalidConfig(l.msg));
},
(_) async {
final profile = Profile.fromResponse(url, headers);
return right(profile);
},
);
try {
final response = await dio.download(url.trim(), tempPath);
final headers =
await _populateHeaders(response.headers.map, tempPath);
final parseResult =
await singbox.parseConfig(path, tempPath, false).run();
return parseResult.fold(
(l) async {
loggy.warning("error parsing config: $l");
return left(ProfileFailure.invalidConfig(l.msg));
},
(_) async {
final profile = Profile.fromResponse(url, headers);
return right(profile);
},
);
} finally {
if (await File(tempPath).exists()) await File(tempPath).delete();
}
},
);
}