2022-08-18 16:33:33 +02:00
|
|
|
import 'dart:io';
|
2022-09-11 03:01:06 +02:00
|
|
|
import 'package:collection/collection.dart';
|
|
|
|
import 'package:dio/dio.dart';
|
2023-03-05 10:12:46 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-09-12 02:41:53 +02:00
|
|
|
import 'package:injectable/injectable.dart';
|
2023-08-11 03:11:19 +02:00
|
|
|
import 'package:revanced_manager/app/app.locator.dart';
|
2023-10-15 11:51:31 +02:00
|
|
|
import 'package:revanced_manager/services/download_manager.dart';
|
2023-07-01 01:41:03 +02:00
|
|
|
import 'package:revanced_manager/services/manager_api.dart';
|
2024-05-18 19:52:13 +02:00
|
|
|
import 'package:synchronized/synchronized.dart';
|
2023-07-01 01:41:03 +02:00
|
|
|
|
2022-09-12 02:41:53 +02:00
|
|
|
@lazySingleton
|
2022-07-31 21:46:27 +02:00
|
|
|
class GithubAPI {
|
2023-10-15 11:51:31 +02:00
|
|
|
late final Dio _dio;
|
2023-08-11 03:11:19 +02:00
|
|
|
late final ManagerAPI _managerAPI = locator<ManagerAPI>();
|
2023-10-15 11:51:31 +02:00
|
|
|
late final DownloadManager _downloadManager = locator<DownloadManager>();
|
2024-05-18 19:52:13 +02:00
|
|
|
final Map<String, Lock> _lockMap = {};
|
2023-04-18 09:57:26 +02:00
|
|
|
|
2023-01-30 13:35:06 +01:00
|
|
|
Future<void> initialize(String repoUrl) async {
|
2023-10-15 11:51:31 +02:00
|
|
|
_dio = _downloadManager.initDio(repoUrl);
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> clearAllCache() async {
|
2023-10-15 11:51:31 +02:00
|
|
|
await _downloadManager.clearAllCache();
|
2022-08-18 18:32:58 +02:00
|
|
|
}
|
|
|
|
|
2024-05-18 19:52:13 +02:00
|
|
|
Future<Response> _dioGetSynchronously(String path) async {
|
|
|
|
// Create a new Lock for each path
|
|
|
|
if (!_lockMap.containsKey(path)) {
|
|
|
|
_lockMap[path] = Lock();
|
|
|
|
}
|
|
|
|
return _lockMap[path]!.synchronized(() async {
|
|
|
|
return await _dio.get(path);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-18 16:15:29 +02:00
|
|
|
Future<Map<String, dynamic>?> getLatestRelease(
|
|
|
|
String repoName,
|
2023-08-03 22:23:56 +02:00
|
|
|
) async {
|
|
|
|
try {
|
2024-05-18 19:52:13 +02:00
|
|
|
final response = await _dioGetSynchronously(
|
2023-08-03 22:23:56 +02:00
|
|
|
'/repos/$repoName/releases/latest',
|
|
|
|
);
|
|
|
|
return response.data;
|
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-19 09:44:09 +02:00
|
|
|
Future<String?> getManagerChangelogs() async {
|
2022-08-09 02:16:33 +02:00
|
|
|
try {
|
2024-05-18 19:52:13 +02:00
|
|
|
final response = await _dioGetSynchronously(
|
2024-06-19 09:44:09 +02:00
|
|
|
'/repos/${_managerAPI.defaultManagerRepo}/releases?per_page=50',
|
2022-08-09 02:16:33 +02:00
|
|
|
);
|
2024-06-19 09:44:09 +02:00
|
|
|
final buffer = StringBuffer();
|
2023-07-10 14:36:50 +02:00
|
|
|
final String currentVersion =
|
2023-10-15 11:51:31 +02:00
|
|
|
await _managerAPI.getCurrentManagerVersion();
|
2024-06-19 09:44:09 +02:00
|
|
|
for (final release in response.data) {
|
|
|
|
if (release['tag_name'] == currentVersion) {
|
|
|
|
if (buffer.isEmpty) {
|
|
|
|
buffer.writeln(release['body']);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (release['prerelease']) {
|
2024-03-17 18:14:22 +01:00
|
|
|
continue;
|
|
|
|
}
|
2024-06-19 09:44:09 +02:00
|
|
|
buffer.writeln(release['body']);
|
2023-07-01 01:41:03 +02:00
|
|
|
}
|
2024-06-19 09:44:09 +02:00
|
|
|
return buffer.toString();
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-12 10:18:03 +02:00
|
|
|
return null;
|
2022-08-09 02:16:33 +02:00
|
|
|
}
|
2022-07-31 21:46:27 +02:00
|
|
|
}
|
2022-08-01 20:06:27 +02:00
|
|
|
|
2024-04-28 23:40:28 +02:00
|
|
|
Future<File?> getReleaseFile(
|
2023-08-03 22:23:56 +02:00
|
|
|
String extension,
|
|
|
|
String repoName,
|
|
|
|
String version,
|
2023-08-11 03:11:19 +02:00
|
|
|
String url,
|
2023-08-03 22:23:56 +02:00
|
|
|
) async {
|
2022-09-11 03:01:06 +02:00
|
|
|
try {
|
2023-08-11 03:11:19 +02:00
|
|
|
if (url.isNotEmpty) {
|
2023-10-15 11:51:31 +02:00
|
|
|
return await _downloadManager.getSingleFile(
|
2023-08-11 03:11:19 +02:00
|
|
|
url,
|
|
|
|
);
|
|
|
|
}
|
2024-05-18 19:52:13 +02:00
|
|
|
final response = await _dioGetSynchronously(
|
2024-04-28 23:40:28 +02:00
|
|
|
'/repos/$repoName/releases/tags/$version',
|
|
|
|
);
|
|
|
|
final Map<String, dynamic>? release = response.data;
|
2023-08-03 22:23:56 +02:00
|
|
|
if (release != null) {
|
|
|
|
final Map<String, dynamic>? asset =
|
|
|
|
(release['assets'] as List<dynamic>).firstWhereOrNull(
|
|
|
|
(asset) => (asset['name'] as String).endsWith(extension),
|
|
|
|
);
|
|
|
|
if (asset != null) {
|
2023-08-11 03:11:19 +02:00
|
|
|
final String downloadUrl = asset['browser_download_url'];
|
|
|
|
if (extension == '.apk') {
|
|
|
|
_managerAPI.setIntegrationsDownloadURL(downloadUrl);
|
|
|
|
} else {
|
2023-09-21 00:35:32 +02:00
|
|
|
_managerAPI.setPatchesDownloadURL(downloadUrl);
|
2023-08-11 03:11:19 +02:00
|
|
|
}
|
2023-10-15 11:51:31 +02:00
|
|
|
return await _downloadManager.getSingleFile(
|
2023-08-11 03:11:19 +02:00
|
|
|
downloadUrl,
|
2023-08-03 22:23:56 +02:00
|
|
|
);
|
|
|
|
}
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
2023-08-03 22:23:56 +02:00
|
|
|
return null;
|
2022-08-12 20:07:16 +02:00
|
|
|
}
|
2022-07-31 21:46:27 +02:00
|
|
|
}
|