mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-16 21:33:41 +00:00
refactor: remove proxy API (#1596)
This commit is contained in:
98 files changed
+5958
-1225
No files matched your search
@@ -1,50 +0,0 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../api/model/downloader_config.dart';
|
||||
|
||||
enum MirrorType { githubSource, githubRelease }
|
||||
|
||||
Future<String> githubAutoMirror(String rawUrl, MirrorType type, {required ExtraConfigGithubMirror config}) async {
|
||||
final mirrorUrls = githubMirrorUrls(rawUrl, type, config: config);
|
||||
if (mirrorUrls.isEmpty) return rawUrl;
|
||||
|
||||
final results = await Future.wait(
|
||||
mirrorUrls.map((url) async {
|
||||
final client = Dio(
|
||||
BaseOptions(connectTimeout: const Duration(seconds: 3), sendTimeout: const Duration(seconds: 3)),
|
||||
);
|
||||
final startedAt = DateTime.now();
|
||||
try {
|
||||
final response = await client.head<void>(url);
|
||||
if (response.statusCode == 200) return (url, DateTime.now().difference(startedAt));
|
||||
} catch (_) {
|
||||
// A failed mirror probe must never prevent the original download.
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
return (url, null);
|
||||
}),
|
||||
);
|
||||
|
||||
final available = results.where((result) => result.$2 != null).toList()
|
||||
..sort((left, right) => left.$2!.compareTo(right.$2!));
|
||||
return available.isEmpty ? rawUrl : available.first.$1;
|
||||
}
|
||||
|
||||
List<String> githubMirrorUrls(String rawUrl, MirrorType type, {required ExtraConfigGithubMirror config}) {
|
||||
if (!config.enabled) return const [];
|
||||
final urls = <String>[];
|
||||
for (final mirror in config.mirrors.where((item) => !item.isDeleted)) {
|
||||
switch (mirror.type) {
|
||||
case GithubMirrorType.jsdelivr:
|
||||
if (type != MirrorType.githubSource) continue;
|
||||
final match = RegExp(r'.*raw\.githubusercontent\.com(/[^/]+)(/[^/]+)/(?:master|main)(/.*)').firstMatch(rawUrl);
|
||||
if (match != null) {
|
||||
urls.add('${mirror.url}${match.group(1)}${match.group(2)}${match.group(3)}');
|
||||
}
|
||||
case GithubMirrorType.ghProxy:
|
||||
urls.add('${mirror.url}/$rawUrl');
|
||||
}
|
||||
}
|
||||
return urls;
|
||||
}
|
||||
@@ -1,22 +1,20 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:install_plugin/install_plugin.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:pub_semver/pub_semver.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import '../api/api.dart' as api;
|
||||
import '../api/gopeed_site_api.dart';
|
||||
import '../api/model/downloader_config.dart';
|
||||
import '../core/capabilities/gopeed_capability.dart';
|
||||
import '../core/network/candidate_download.dart';
|
||||
import '../core/network/app_update_client.dart';
|
||||
import 'arch/arch.dart';
|
||||
import 'github_mirror.dart';
|
||||
import 'log_util.dart';
|
||||
import 'package_info.dart';
|
||||
import 'util.dart';
|
||||
|
||||
export '../core/network/app_update_client.dart' show VersionInfo;
|
||||
|
||||
enum UpdateChannel {
|
||||
windowsInstaller,
|
||||
windowsPortable,
|
||||
@@ -46,17 +44,6 @@ UpdateChannel? get updateChannel {
|
||||
|
||||
String get _updaterBinaryName => 'updater${Util.isWindows() ? '.exe' : ''}';
|
||||
|
||||
const _releasePageSize = 10;
|
||||
const _githubReleasesUrl = 'https://api.github.com/repos/GopeedLab/gopeed/releases?per_page=$_releasePageSize';
|
||||
|
||||
class VersionInfo {
|
||||
const VersionInfo({required this.version, required this.changeLog, required this.releaseUrl});
|
||||
|
||||
final String version;
|
||||
final String changeLog;
|
||||
final String releaseUrl;
|
||||
}
|
||||
|
||||
/// Installs the small native updater bundled with desktop releases.
|
||||
///
|
||||
/// Development builds do not necessarily contain this asset. Callers should
|
||||
@@ -71,91 +58,27 @@ Future<void> installUpdater() async {
|
||||
);
|
||||
}
|
||||
|
||||
Future<VersionInfo?> checkUpdate() async {
|
||||
List<dynamic> releases;
|
||||
try {
|
||||
final releaseDataStr = (await api.proxyRequest(_githubReleasesUrl)).data;
|
||||
if (releaseDataStr == null || releaseDataStr.isEmpty) {
|
||||
throw const FormatException('Empty GitHub releases response');
|
||||
}
|
||||
final releaseData = jsonDecode(releaseDataStr);
|
||||
if (releaseData is! List<dynamic>) {
|
||||
throw const FormatException('Invalid GitHub releases response');
|
||||
}
|
||||
releases = releaseData;
|
||||
} catch (_) {
|
||||
releases = await GopeedSiteApi.instance.getReleases(perPage: _releasePageSize);
|
||||
}
|
||||
return selectUpdateRelease(releases, appVersion);
|
||||
Future<VersionInfo?> checkUpdate() => AppUpdateClient().check(appVersion);
|
||||
|
||||
/// Package selection belongs to the client, including when the Downloader is remote.
|
||||
String appUpdateAssetName(String version, UpdateChannel? channel, String architecture) {
|
||||
final arch = {'ia32': '386', 'x64': 'amd64'}[architecture] ?? architecture;
|
||||
final prefix = 'Gopeed-v$version';
|
||||
return switch (channel) {
|
||||
UpdateChannel.windowsInstaller => '$prefix-windows-$arch.zip',
|
||||
UpdateChannel.windowsPortable => '$prefix-windows-$arch-portable.zip',
|
||||
UpdateChannel.macosDmg => '$prefix-macos-$arch.dmg',
|
||||
UpdateChannel.linuxDeb => '$prefix-linux-$arch.deb',
|
||||
UpdateChannel.androidApk => switch (architecture) {
|
||||
'arm' => '$prefix-android-armeabi-v7a.apk',
|
||||
'arm64' => '$prefix-android-arm64-v8a.apk',
|
||||
'x64' => '$prefix-android-x86_64.apk',
|
||||
_ => '$prefix-android.apk',
|
||||
},
|
||||
_ => '',
|
||||
};
|
||||
}
|
||||
|
||||
bool isNewerVersion(String latest, String current) {
|
||||
try {
|
||||
return _parseVersion(latest).compareTo(_parseVersion(current)) > 0;
|
||||
} on FormatException {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
VersionInfo? selectUpdateRelease(List<dynamic> releases, String currentVersionText) {
|
||||
late final Version currentVersion;
|
||||
try {
|
||||
currentVersion = _parseVersion(currentVersionText);
|
||||
} on FormatException {
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, dynamic>? selectedRelease;
|
||||
Version? selectedVersion;
|
||||
|
||||
for (final item in releases) {
|
||||
if (item is! Map) continue;
|
||||
final release = Map<String, dynamic>.from(item);
|
||||
if (release['draft'] == true) continue;
|
||||
|
||||
final tagName = release['tag_name'];
|
||||
if (tagName is! String || tagName.isEmpty) continue;
|
||||
|
||||
late final Version candidateVersion;
|
||||
try {
|
||||
candidateVersion = _parseVersion(tagName);
|
||||
} on FormatException {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (candidateVersion.compareTo(currentVersion) <= 0) continue;
|
||||
|
||||
final candidateIsPrerelease = release['prerelease'] == true || candidateVersion.isPreRelease;
|
||||
if (!currentVersion.isPreRelease) {
|
||||
if (candidateIsPrerelease) continue;
|
||||
} else if (candidateIsPrerelease && !_hasSameReleaseCore(candidateVersion, currentVersion)) {
|
||||
// Preview builds graduate through their own beta/rc line before returning
|
||||
// to stable; they do not jump into the next version's preview line.
|
||||
continue;
|
||||
}
|
||||
|
||||
if (selectedVersion == null || candidateVersion.compareTo(selectedVersion) > 0) {
|
||||
selectedRelease = release;
|
||||
selectedVersion = candidateVersion;
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedRelease == null || selectedVersion == null) return null;
|
||||
final tagName = selectedRelease['tag_name'] as String;
|
||||
return VersionInfo(
|
||||
version: _versionText(tagName),
|
||||
changeLog: (selectedRelease['body'] ?? '').toString(),
|
||||
releaseUrl: (selectedRelease['html_url'] ?? 'https://github.com/GopeedLab/gopeed/releases/tag/$tagName').toString(),
|
||||
);
|
||||
}
|
||||
|
||||
Version _parseVersion(String version) => Version.parse(_versionText(version));
|
||||
|
||||
String _versionText(String version) => version.startsWith('v') ? version.substring(1) : version;
|
||||
|
||||
bool _hasSameReleaseCore(Version first, Version second) =>
|
||||
first.major == second.major && first.minor == second.minor && first.patch == second.patch;
|
||||
|
||||
/// Extracts the matching section from Gopeed's bilingual GitHub release notes.
|
||||
String localizedReleaseNotes(String fullChangeLog, String languageCode) {
|
||||
final isChinese = languageCode.toLowerCase().startsWith('zh');
|
||||
@@ -175,23 +98,23 @@ typedef UpdateProgressCallback = void Function(int received, int total);
|
||||
/// Store-distributed and unsupported channels safely open the release page.
|
||||
Future<void> updateApp(
|
||||
VersionInfo versionInfo, {
|
||||
required ExtraConfigGithubMirror githubMirror,
|
||||
required GopeedService service,
|
||||
required UpdateProgressCallback onProgress,
|
||||
}) async {
|
||||
final channel = updateChannel;
|
||||
final assetName = updateAssetName(versionInfo.version, channel: channel);
|
||||
final assetName = appUpdateAssetName(versionInfo.version, channel, getArch().name);
|
||||
var assetPath = '';
|
||||
|
||||
if (assetName.isNotEmpty) {
|
||||
final rawUrl = 'https://github.com/GopeedLab/gopeed/releases/download/v${versionInfo.version}/$assetName';
|
||||
final expectedSha256 = versionInfo.assetSha256[assetName];
|
||||
if (expectedSha256 == null) throw const DownloadChecksumException();
|
||||
onProgress(0, 0);
|
||||
assetPath = path.join((await getTemporaryDirectory()).path, assetName);
|
||||
final downloadUrl = await githubAutoMirror(rawUrl, MirrorType.githubRelease, config: githubMirror);
|
||||
final client = Dio();
|
||||
try {
|
||||
await client.download(downloadUrl, assetPath, onReceiveProgress: onProgress);
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
final urls = await service.getMirrorCandidates(
|
||||
'github',
|
||||
'https://github.com/GopeedLab/gopeed/releases/download/v${versionInfo.version}/$assetName',
|
||||
);
|
||||
await downloadFromCandidates(urls, assetPath, expectedSha256: expectedSha256, onReceiveProgress: onProgress);
|
||||
}
|
||||
|
||||
switch (channel) {
|
||||
@@ -233,33 +156,3 @@ Future<void> _openRelease(String releaseUrl) async {
|
||||
final opened = await launchUrl(Uri.parse(releaseUrl), mode: LaunchMode.externalApplication);
|
||||
if (!opened) throw StateError('Unable to open the release page.');
|
||||
}
|
||||
|
||||
String updateAssetName(String version, {UpdateChannel? channel, Architecture? architecture}) {
|
||||
final targetChannel = channel ?? updateChannel;
|
||||
final arch = architecture ?? getArch();
|
||||
|
||||
String commonArchName() => switch (arch) {
|
||||
Architecture.ia32 => '386',
|
||||
Architecture.x64 => 'amd64',
|
||||
_ => arch.name,
|
||||
};
|
||||
|
||||
return switch (targetChannel) {
|
||||
UpdateChannel.windowsInstaller => 'Gopeed-v$version-windows-${commonArchName()}.zip',
|
||||
UpdateChannel.windowsPortable => 'Gopeed-v$version-windows-${commonArchName()}-portable.zip',
|
||||
UpdateChannel.macosDmg => 'Gopeed-v$version-macos-${commonArchName()}.dmg',
|
||||
UpdateChannel.linuxDeb => 'Gopeed-v$version-linux-${commonArchName()}.deb',
|
||||
UpdateChannel.androidApk => _androidAssetName(version, arch),
|
||||
_ => '',
|
||||
};
|
||||
}
|
||||
|
||||
String _androidAssetName(String version, Architecture arch) {
|
||||
final archName = switch (arch) {
|
||||
Architecture.arm => 'armeabi-v7a',
|
||||
Architecture.arm64 => 'arm64-v8a',
|
||||
Architecture.x64 => 'x86_64',
|
||||
_ => null,
|
||||
};
|
||||
return 'Gopeed-v$version-android${archName == null ? '' : '-$archName'}.apk';
|
||||
}
|
||||
Reference in new issue
Block a user