2022-08-01 21:05:11 +02:00
|
|
|
import 'dart:io';
|
2022-08-02 09:55:01 +02:00
|
|
|
import 'package:dio/dio.dart';
|
2022-08-06 14:13:28 +02:00
|
|
|
import 'package:injectable/injectable.dart';
|
2022-08-01 21:05:11 +02:00
|
|
|
import 'package:path_provider/path_provider.dart' as p;
|
2022-08-06 23:35:35 +02:00
|
|
|
import 'package:revanced_manager/constants.dart';
|
2022-08-02 09:55:01 +02:00
|
|
|
import 'github_api.dart';
|
2022-07-31 21:46:27 +02:00
|
|
|
|
2022-08-01 21:05:11 +02:00
|
|
|
// use path_provider to get the path of the storage directory
|
2022-08-06 14:13:28 +02:00
|
|
|
@lazySingleton
|
2022-08-02 10:06:35 +02:00
|
|
|
class ManagerAPI {
|
|
|
|
Dio dio = Dio();
|
|
|
|
GithubAPI githubAPI = GithubAPI();
|
|
|
|
|
|
|
|
Future<String?> getPath() async {
|
|
|
|
final path = await p.getApplicationSupportDirectory();
|
|
|
|
final workDir = Directory('${path.path}/revanced').createSync();
|
|
|
|
final workDirPath = "${path.path}/revanced";
|
|
|
|
return workDirPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<File?> downloadAssets(String repo) async {
|
|
|
|
try {
|
|
|
|
final workDir = await getPath();
|
|
|
|
final dlUrl = await githubAPI.latestRelease(ghOrg, repo);
|
|
|
|
final name = dlUrl
|
|
|
|
?.split('/')
|
|
|
|
.lastWhere((element) => element.contains('revanced'));
|
|
|
|
print(name);
|
|
|
|
final assetFile = File('$workDir/$name');
|
|
|
|
final response = await dio.get(
|
|
|
|
dlUrl!,
|
|
|
|
options: Options(
|
|
|
|
responseType: ResponseType.bytes,
|
|
|
|
followRedirects: true,
|
|
|
|
receiveTimeout: 0,
|
|
|
|
),
|
|
|
|
onReceiveProgress: (count, total) {
|
|
|
|
print('$count/$total');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
final raf = assetFile.openSync(mode: FileMode.write);
|
|
|
|
raf.writeFromSync(response.data);
|
|
|
|
raf.closeSync();
|
|
|
|
return assetFile;
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> downloadPatches() async {
|
|
|
|
await downloadAssets(patchesRepo);
|
|
|
|
}
|
2022-08-02 09:55:01 +02:00
|
|
|
|
2022-08-02 10:06:35 +02:00
|
|
|
Future<void> downloadIntegrations() async {
|
|
|
|
await downloadAssets(integrationsRepo);
|
2022-08-02 09:55:01 +02:00
|
|
|
}
|
2022-08-01 21:05:11 +02:00
|
|
|
}
|