让用户选择更新源,并同步在线更新历史
设置里可以在 YanJiu Git 和 GitHub 之间选择,默认是 YanJiu Git。选中的源连不上时改用另一个,结果会写明实际用的是哪一个。更新历史从所选源读取已发布版本并在本机缓存。表单里被省略号截断的提示可以点开。 Co-authored-by: yanjiu0721 <yanjiu0721@users.noreply.github.com>
This commit is contained in:
1 parent
b8cc446454
commit
2e77fcc376
14 files changed
+1771
-68
No files matched your search
@@ -1,13 +1,16 @@
|
||||
// Copyright (C) 2026 言灸
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:ffi' show Abi;
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../version.dart';
|
||||
import 'installer.dart';
|
||||
import 'release_feed.dart';
|
||||
import 'update_check.dart';
|
||||
|
||||
class UpdateResult {
|
||||
@@ -19,31 +22,63 @@ class UpdateResult {
|
||||
|
||||
const platformChannel = MethodChannel('dev.netquality/platform');
|
||||
|
||||
Future<UpdateResult> checkForUpdate() async {
|
||||
UpdateEndpoints endpointsFromEnvironment() {
|
||||
return updateEndpoints(
|
||||
githubApi: Platform.environment['NQ_UPDATE_GITHUB_API'],
|
||||
giteaApi: Platform.environment['NQ_UPDATE_GITEA_API'],
|
||||
);
|
||||
}
|
||||
|
||||
Future<ReleaseCache?> readStoredReleaseHistory() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return decodeReleaseCache(prefs.getString(releaseHistoryCacheKey));
|
||||
}
|
||||
|
||||
Future<ReleaseSync> loadReleaseHistory(
|
||||
UpdateSource preferred, {
|
||||
bool? allowNetwork,
|
||||
}) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return syncAndStore(
|
||||
preferred: preferred,
|
||||
endpoints: endpointsFromEnvironment(),
|
||||
fetch: fetchReleaseText,
|
||||
allowNetwork:
|
||||
allowNetwork ?? Platform.environment['FLUTTER_TEST'] != 'true',
|
||||
readCache: () async => prefs.getString(releaseHistoryCacheKey),
|
||||
writeCache: (json) async {
|
||||
await prefs.setString(releaseHistoryCacheKey, json);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<UpdateResult> checkForUpdate({
|
||||
UpdateSource preferred = UpdateSource.gitea,
|
||||
}) async {
|
||||
final history = loadReleaseHistory(preferred);
|
||||
final arch = await _arch();
|
||||
if (arch.isEmpty) {
|
||||
await _finishHistory(history);
|
||||
return const UpdateResult(message: '认不出这台设备的系统或 CPU,没有下载安装包');
|
||||
}
|
||||
final endpoints = UpdateEndpoints(
|
||||
github: Platform.environment['NQ_UPDATE_GITHUB_API'] ??
|
||||
UpdateEndpoints.githubDefault,
|
||||
gitea: Platform.environment['NQ_UPDATE_GITEA_API'] ??
|
||||
UpdateEndpoints.giteaDefault,
|
||||
);
|
||||
final check = await findUpdate(
|
||||
current: appVersion,
|
||||
endpoints: endpoints,
|
||||
endpoints: endpointsFromEnvironment(),
|
||||
preferred: preferred,
|
||||
os: _os(),
|
||||
arch: arch,
|
||||
fetch: (url) => _getText(url),
|
||||
fetch: fetchReleaseText,
|
||||
);
|
||||
if (check.offer == null) {
|
||||
return UpdateResult(message: check.message);
|
||||
await _finishHistory(history);
|
||||
return UpdateResult(message: check.message, offer: check.offer);
|
||||
}
|
||||
|
||||
Future<void> _finishHistory(Future<ReleaseSync> history) async {
|
||||
try {
|
||||
await history;
|
||||
} on Object {
|
||||
// A failed history sync still leaves the update check usable.
|
||||
}
|
||||
return UpdateResult(
|
||||
message: '发现 ${check.offer!.version}。可以下载并覆盖安装。',
|
||||
offer: check.offer,
|
||||
);
|
||||
}
|
||||
|
||||
Future<String> installOffer(UpdateOffer offer) async {
|
||||
@@ -155,7 +190,45 @@ Future<String?> _getText(Uri url) async {
|
||||
if (bytes == null) {
|
||||
return null;
|
||||
}
|
||||
return String.fromCharCodes(bytes);
|
||||
return utf8.decode(bytes, allowMalformed: true);
|
||||
}
|
||||
|
||||
/// Release JSON, not an installer. A 429, or a 403 that says the quota is
|
||||
/// gone, is reported as a rate limit so the caller can say so in Chinese.
|
||||
Future<String?> fetchReleaseText(Uri url) async {
|
||||
final client = HttpClient();
|
||||
client.connectionTimeout = const Duration(seconds: 15);
|
||||
try {
|
||||
final request = await client.getUrl(url);
|
||||
request.headers.set(HttpHeaders.userAgentHeader, 'netquality/$appVersion');
|
||||
request.headers.set(HttpHeaders.acceptHeader, 'application/json');
|
||||
final response = await request.close().timeout(const Duration(seconds: 20));
|
||||
final bytes = <int>[];
|
||||
await for (final chunk in response.timeout(const Duration(seconds: 20))) {
|
||||
bytes.addAll(chunk);
|
||||
}
|
||||
final text = utf8.decode(bytes, allowMalformed: true);
|
||||
if (isReleaseRateLimited(
|
||||
status: response.statusCode,
|
||||
rateLimitRemaining: response.headers.value('x-ratelimit-remaining'),
|
||||
body: text,
|
||||
)) {
|
||||
throw UpdateFetchException(
|
||||
rateLimited: true,
|
||||
status: response.statusCode,
|
||||
);
|
||||
}
|
||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||
return null;
|
||||
}
|
||||
return text;
|
||||
} on UpdateFetchException {
|
||||
rethrow;
|
||||
} on Object {
|
||||
return null;
|
||||
} finally {
|
||||
client.close(force: true);
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<int>?> _getBytes(Uri url) async {
|
||||
|
||||
@@ -0,0 +1,558 @@
|
||||
// Copyright (C) 2026 言灸
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import '../content/release_history.dart';
|
||||
import 'update_check.dart';
|
||||
|
||||
/// SharedPreferences key for the last release list that a source actually returned.
|
||||
const releaseHistoryCacheKey = 'netquality.release-history.v1';
|
||||
|
||||
enum HistoryOrigin { remote, cache, bundled }
|
||||
|
||||
class ReleaseCache {
|
||||
const ReleaseCache({required this.source, required this.entries});
|
||||
|
||||
final String source;
|
||||
final List<ReleaseEntry> entries;
|
||||
}
|
||||
|
||||
class ReleaseSync {
|
||||
const ReleaseSync({
|
||||
required this.entries,
|
||||
required this.note,
|
||||
required this.origin,
|
||||
this.cacheJson,
|
||||
this.used,
|
||||
});
|
||||
|
||||
final List<ReleaseEntry> entries;
|
||||
final String note;
|
||||
final HistoryOrigin origin;
|
||||
|
||||
/// Non-null when [origin] is [HistoryOrigin.remote] and the list should be saved.
|
||||
final String? cacheJson;
|
||||
final UpdateSource? used;
|
||||
}
|
||||
|
||||
class ReleasePage {
|
||||
const ReleasePage({required this.entries, required this.complete});
|
||||
|
||||
final List<ReleaseEntry> entries;
|
||||
|
||||
/// False when this page was full and another page may exist.
|
||||
final bool complete;
|
||||
}
|
||||
|
||||
/// Turns a `/releases/latest` URL into one page of the release list.
|
||||
/// GitHub pages with `per_page`; Gitea pages with `limit`. A testing override
|
||||
/// of the latest URL is reused, so the list follows `NQ_UPDATE_*_API`.
|
||||
Uri releaseListUri(
|
||||
String latestUrl, {
|
||||
required UpdateSource source,
|
||||
required int page,
|
||||
required int pageSize,
|
||||
}) {
|
||||
final parsed = Uri.parse(latestUrl.trim());
|
||||
var path = parsed.path;
|
||||
if (path.endsWith('/latest')) {
|
||||
path = path.substring(0, path.length - '/latest'.length);
|
||||
}
|
||||
final params = Map<String, String>.from(parsed.queryParameters);
|
||||
if (source == UpdateSource.github) {
|
||||
params['per_page'] = '$pageSize';
|
||||
params.remove('limit');
|
||||
} else {
|
||||
params['limit'] = '$pageSize';
|
||||
params.remove('per_page');
|
||||
}
|
||||
params['page'] = '$page';
|
||||
return parsed.replace(path: path, queryParameters: params);
|
||||
}
|
||||
|
||||
/// GitHub and Gitea both return `tag_name`, `name`, `body`, `published_at`,
|
||||
/// `draft`, `prerelease`, and `assets[].browser_download_url`. A list page is
|
||||
/// a JSON array. A single release object is accepted so a `/latest` body can
|
||||
/// still be shown. An error object is not a page.
|
||||
ReleasePage? parseReleasePage(
|
||||
String body, {
|
||||
required String sourceLabel,
|
||||
required int pageSize,
|
||||
}) {
|
||||
Object? decoded;
|
||||
try {
|
||||
decoded = jsonDecode(body);
|
||||
} on FormatException {
|
||||
return null;
|
||||
}
|
||||
if (decoded is Map) {
|
||||
if (decoded['tag_name'] is! String) {
|
||||
return null;
|
||||
}
|
||||
final entry = _entryFromRelease(
|
||||
Map<String, dynamic>.from(decoded),
|
||||
sourceLabel: sourceLabel,
|
||||
);
|
||||
return ReleasePage(entries: [?entry], complete: true);
|
||||
}
|
||||
if (decoded is! List) {
|
||||
return null;
|
||||
}
|
||||
final entries = <ReleaseEntry>[];
|
||||
for (final item in decoded) {
|
||||
if (item is! Map) {
|
||||
continue;
|
||||
}
|
||||
final entry = _entryFromRelease(
|
||||
Map<String, dynamic>.from(item),
|
||||
sourceLabel: sourceLabel,
|
||||
);
|
||||
if (entry != null) {
|
||||
entries.add(entry);
|
||||
}
|
||||
}
|
||||
return ReleasePage(entries: entries, complete: decoded.length < pageSize);
|
||||
}
|
||||
|
||||
ReleaseEntry? _entryFromRelease(
|
||||
Map<String, dynamic> json, {
|
||||
required String sourceLabel,
|
||||
}) {
|
||||
if (json['draft'] == true) {
|
||||
return null;
|
||||
}
|
||||
final tag = json['tag_name'];
|
||||
if (tag is! String) {
|
||||
return null;
|
||||
}
|
||||
final version = SemVer.parse(tag);
|
||||
if (version == null) {
|
||||
return null;
|
||||
}
|
||||
final versionText = '${version.major}.${version.minor}.${version.patch}';
|
||||
final name = json['name'];
|
||||
final title = name is String && name.trim().isNotEmpty
|
||||
? name.trim()
|
||||
: '网络质量探测 $versionText';
|
||||
final body = json['body'] is String ? json['body'] as String : '';
|
||||
final points = notesToPoints(body, title: title);
|
||||
if (points.isEmpty) {
|
||||
points.add('这次发布没有正文。');
|
||||
}
|
||||
final pre = json['prerelease'] == true;
|
||||
final published = _publishedDay(json['published_at']);
|
||||
final tagText = tag.trim();
|
||||
return ReleaseEntry(
|
||||
version: versionText,
|
||||
tag: tagText,
|
||||
title: title,
|
||||
published: published,
|
||||
source: '来自 $sourceLabel · 标签 $tagText${pre ? ' · 预发布' : ''}',
|
||||
points: points,
|
||||
);
|
||||
}
|
||||
|
||||
String? _publishedDay(Object? raw) {
|
||||
if (raw is! String || raw.length < 10) {
|
||||
return null;
|
||||
}
|
||||
final day = raw.substring(0, 10);
|
||||
if (!RegExp(r'^\d{4}-\d{2}-\d{2}$').hasMatch(day)) {
|
||||
return null;
|
||||
}
|
||||
return day;
|
||||
}
|
||||
|
||||
/// Splits a release body into the short points the history page already shows.
|
||||
/// Headings and bullet items each become one point. A title repeated as the
|
||||
/// first heading is dropped. Versions that are not in the body are not added.
|
||||
List<String> notesToPoints(String body, {String title = ''}) {
|
||||
final points = <String>[];
|
||||
final buffer = StringBuffer();
|
||||
void flush() {
|
||||
final text = _plain(buffer.toString());
|
||||
buffer.clear();
|
||||
if (text.isNotEmpty) {
|
||||
points.add(text);
|
||||
}
|
||||
}
|
||||
|
||||
for (final raw in body.replaceAll('\r\n', '\n').split('\n')) {
|
||||
final trimmed = raw.trim();
|
||||
if (trimmed.isEmpty) {
|
||||
flush();
|
||||
continue;
|
||||
}
|
||||
if (trimmed.startsWith('```')) {
|
||||
flush();
|
||||
continue;
|
||||
}
|
||||
final heading = RegExp(r'^#{1,6}\s+').firstMatch(trimmed);
|
||||
if (heading != null) {
|
||||
flush();
|
||||
final text = _plain(trimmed.substring(heading.end));
|
||||
if (text.isNotEmpty) {
|
||||
points.add(text);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
final bullet = RegExp(r'^[-*+]\s+').firstMatch(trimmed);
|
||||
if (bullet != null) {
|
||||
flush();
|
||||
final text = _plain(trimmed.substring(bullet.end));
|
||||
if (text.isNotEmpty) {
|
||||
points.add(text);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (buffer.isNotEmpty) {
|
||||
buffer.write(' ');
|
||||
}
|
||||
buffer.write(trimmed);
|
||||
}
|
||||
flush();
|
||||
if (points.isNotEmpty && title.isNotEmpty && points.first == title) {
|
||||
points.removeAt(0);
|
||||
}
|
||||
return points;
|
||||
}
|
||||
|
||||
String _plain(String input) {
|
||||
var text = input.trim();
|
||||
text = text.replaceAllMapped(RegExp(r'\[([^\]]+)\]\(([^)]+)\)'), (match) {
|
||||
final label = match.group(1)!.trim();
|
||||
final url = match.group(2)!.trim();
|
||||
if (label.isEmpty) {
|
||||
return url;
|
||||
}
|
||||
if (url.isEmpty || url == label) {
|
||||
return label;
|
||||
}
|
||||
return '$label($url)';
|
||||
});
|
||||
text = text.replaceAll('`', '');
|
||||
text = text.replaceAll(RegExp(r'\s+'), ' ');
|
||||
return text.trim();
|
||||
}
|
||||
|
||||
String encodeReleaseCache({
|
||||
required String source,
|
||||
required List<ReleaseEntry> entries,
|
||||
}) {
|
||||
return jsonEncode({
|
||||
'source': source,
|
||||
'entries': [for (final entry in entries) entry.toJson()],
|
||||
});
|
||||
}
|
||||
|
||||
ReleaseCache? decodeReleaseCache(String? raw) {
|
||||
if (raw == null || raw.trim().isEmpty) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
final decoded = jsonDecode(raw);
|
||||
if (decoded is! Map) {
|
||||
return null;
|
||||
}
|
||||
final source = decoded['source'];
|
||||
final rawEntries = decoded['entries'];
|
||||
if (source is! String || rawEntries is! List) {
|
||||
return null;
|
||||
}
|
||||
final entries = <ReleaseEntry>[];
|
||||
for (final item in rawEntries) {
|
||||
if (item is! Map) {
|
||||
continue;
|
||||
}
|
||||
final entry = ReleaseEntry.fromJson(Map<String, dynamic>.from(item));
|
||||
if (entry.version.isEmpty && entry.title.isEmpty) {
|
||||
continue;
|
||||
}
|
||||
entries.add(entry);
|
||||
}
|
||||
return ReleaseCache(source: source, entries: entries);
|
||||
} on FormatException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
String freshHistoryNote({
|
||||
required String sourceLabel,
|
||||
required bool fellBack,
|
||||
required bool preferredRateLimited,
|
||||
required bool incomplete,
|
||||
required bool empty,
|
||||
}) {
|
||||
final head = !fellBack
|
||||
? '已从 $sourceLabel 同步。'
|
||||
: preferredRateLimited
|
||||
? '所选更新源暂时限制了请求次数,这次改用了 $sourceLabel。'
|
||||
: '所选更新源连不上,这次改用了 $sourceLabel。';
|
||||
final tail = empty
|
||||
? '这个更新源目前没有已发布的版本。'
|
||||
: incomplete
|
||||
? '后面的页没有取完,可能不是全部。'
|
||||
: '比当前安装更新的发布也会列在这里。';
|
||||
return '$head$tail';
|
||||
}
|
||||
|
||||
String staleHistoryNote({
|
||||
required bool rateLimited,
|
||||
required bool hasCache,
|
||||
String? sourceLabel,
|
||||
}) {
|
||||
final reason = rateLimited ? '更新源暂时限制了请求次数' : '网络暂时连不上';
|
||||
if (!hasCache) {
|
||||
return '$reason,也没有保存过在线列表。下面是安装包里的说明,可能不是最新。';
|
||||
}
|
||||
final from = sourceLabel == null || sourceLabel.isEmpty
|
||||
? ''
|
||||
: '从 $sourceLabel ';
|
||||
return '$reason。下面是上次$from保存的列表,可能不是最新。';
|
||||
}
|
||||
|
||||
String? cacheSourceLabel(String source) {
|
||||
if (source == updateSourceGithub) {
|
||||
return updateSourceLabel(UpdateSource.github);
|
||||
}
|
||||
if (source == updateSourceGitea) {
|
||||
return updateSourceLabel(UpdateSource.gitea);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Reads the chosen source's release list, then the other source if that
|
||||
/// fails. A network failure keeps [cachedJson], or [bundled] when nothing
|
||||
/// has been saved. Published releases newer than the installed app stay in
|
||||
/// the list; versions that the payload does not contain are not added.
|
||||
Future<ReleaseSync> syncReleaseHistory({
|
||||
required UpdateSource preferred,
|
||||
required UpdateEndpoints endpoints,
|
||||
required Future<String?> Function(Uri url) fetch,
|
||||
String? cachedJson,
|
||||
List<ReleaseEntry> bundled = releaseHistory,
|
||||
int pageLimit = 5,
|
||||
int githubPageSize = 100,
|
||||
int giteaPageSize = 50,
|
||||
}) async {
|
||||
final order = preferred == UpdateSource.github
|
||||
? const [UpdateSource.github, UpdateSource.gitea]
|
||||
: const [UpdateSource.gitea, UpdateSource.github];
|
||||
var sawRate = false;
|
||||
var preferredRate = false;
|
||||
for (var i = 0; i < order.length; i++) {
|
||||
final source = order[i];
|
||||
final pageSize = source == UpdateSource.github
|
||||
? githubPageSize
|
||||
: giteaPageSize;
|
||||
try {
|
||||
final read = await _readSource(
|
||||
latestUrl: endpoints.latestFor(source),
|
||||
source: source,
|
||||
fetch: fetch,
|
||||
pageLimit: pageLimit,
|
||||
pageSize: pageSize,
|
||||
);
|
||||
if (read == null) {
|
||||
continue;
|
||||
}
|
||||
final entries = _sortedUnique(read.entries);
|
||||
return ReleaseSync(
|
||||
entries: entries,
|
||||
origin: HistoryOrigin.remote,
|
||||
used: source,
|
||||
cacheJson: encodeReleaseCache(
|
||||
source: updateSourceId(source),
|
||||
entries: entries,
|
||||
),
|
||||
note: freshHistoryNote(
|
||||
sourceLabel: updateSourceLabel(source),
|
||||
fellBack: i > 0,
|
||||
preferredRateLimited: i > 0 && preferredRate,
|
||||
incomplete: read.incomplete,
|
||||
empty: entries.isEmpty,
|
||||
),
|
||||
);
|
||||
} on UpdateFetchException catch (error) {
|
||||
if (error.rateLimited) {
|
||||
sawRate = true;
|
||||
if (i == 0) {
|
||||
preferredRate = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return _offline(
|
||||
cachedJson: cachedJson,
|
||||
bundled: bundled,
|
||||
rateLimited: sawRate,
|
||||
);
|
||||
}
|
||||
|
||||
class _SourceRead {
|
||||
const _SourceRead({required this.entries, required this.incomplete});
|
||||
|
||||
final List<ReleaseEntry> entries;
|
||||
final bool incomplete;
|
||||
}
|
||||
|
||||
Future<_SourceRead?> _readSource({
|
||||
required String latestUrl,
|
||||
required UpdateSource source,
|
||||
required Future<String?> Function(Uri url) fetch,
|
||||
required int pageLimit,
|
||||
required int pageSize,
|
||||
}) async {
|
||||
final entries = <ReleaseEntry>[];
|
||||
final label = updateSourceLabel(source);
|
||||
for (var page = 1; page <= pageLimit; page++) {
|
||||
final uri = releaseListUri(
|
||||
latestUrl,
|
||||
source: source,
|
||||
page: page,
|
||||
pageSize: pageSize,
|
||||
);
|
||||
String? body;
|
||||
try {
|
||||
body = await fetch(uri);
|
||||
} on UpdateFetchException {
|
||||
if (page == 1) {
|
||||
rethrow;
|
||||
}
|
||||
return _SourceRead(entries: entries, incomplete: true);
|
||||
} on Object {
|
||||
if (page == 1) {
|
||||
return null;
|
||||
}
|
||||
return _SourceRead(entries: entries, incomplete: true);
|
||||
}
|
||||
if (body == null) {
|
||||
if (page == 1) {
|
||||
return null;
|
||||
}
|
||||
return _SourceRead(entries: entries, incomplete: true);
|
||||
}
|
||||
final parsed = parseReleasePage(
|
||||
body,
|
||||
sourceLabel: label,
|
||||
pageSize: pageSize,
|
||||
);
|
||||
if (parsed == null) {
|
||||
if (page == 1) {
|
||||
return null;
|
||||
}
|
||||
return _SourceRead(entries: entries, incomplete: true);
|
||||
}
|
||||
entries.addAll(parsed.entries);
|
||||
if (parsed.complete) {
|
||||
return _SourceRead(entries: entries, incomplete: false);
|
||||
}
|
||||
if (page == pageLimit) {
|
||||
return _SourceRead(entries: entries, incomplete: true);
|
||||
}
|
||||
}
|
||||
return _SourceRead(entries: entries, incomplete: true);
|
||||
}
|
||||
|
||||
List<ReleaseEntry> _sortedUnique(List<ReleaseEntry> entries) {
|
||||
final sorted = [...entries];
|
||||
sorted.sort((a, b) {
|
||||
final av = SemVer.parse(a.version);
|
||||
final bv = SemVer.parse(b.version);
|
||||
if (av != null && bv != null) {
|
||||
final compared = bv.compareTo(av);
|
||||
if (compared != 0) {
|
||||
return compared;
|
||||
}
|
||||
}
|
||||
final published = (b.published ?? '').compareTo(a.published ?? '');
|
||||
if (published != 0) {
|
||||
return published;
|
||||
}
|
||||
return (a.tag ?? '').compareTo(b.tag ?? '');
|
||||
});
|
||||
final seen = <String>{};
|
||||
final unique = <ReleaseEntry>[];
|
||||
for (final entry in sorted) {
|
||||
final key = (entry.tag == null || entry.tag!.isEmpty)
|
||||
? entry.version
|
||||
: entry.tag!;
|
||||
if (key.isEmpty || !seen.add(key)) {
|
||||
continue;
|
||||
}
|
||||
unique.add(entry);
|
||||
}
|
||||
return unique;
|
||||
}
|
||||
|
||||
ReleaseSync _offline({
|
||||
required String? cachedJson,
|
||||
required List<ReleaseEntry> bundled,
|
||||
required bool rateLimited,
|
||||
}) {
|
||||
final cache = decodeReleaseCache(cachedJson);
|
||||
if (cache != null) {
|
||||
return ReleaseSync(
|
||||
entries: cache.entries,
|
||||
origin: HistoryOrigin.cache,
|
||||
note: staleHistoryNote(
|
||||
rateLimited: rateLimited,
|
||||
hasCache: true,
|
||||
sourceLabel: cacheSourceLabel(cache.source),
|
||||
),
|
||||
);
|
||||
}
|
||||
return ReleaseSync(
|
||||
entries: bundled,
|
||||
origin: HistoryOrigin.bundled,
|
||||
note: staleHistoryNote(rateLimited: rateLimited, hasCache: false),
|
||||
);
|
||||
}
|
||||
|
||||
/// Same sync as [syncReleaseHistory], plus reading and writing the local cache.
|
||||
/// [allowNetwork] is false in widget tests so opening history does not call out.
|
||||
Future<ReleaseSync> syncAndStore({
|
||||
required UpdateSource preferred,
|
||||
required UpdateEndpoints endpoints,
|
||||
required Future<String?> Function(Uri url) fetch,
|
||||
required Future<String?> Function() readCache,
|
||||
required Future<void> Function(String json) writeCache,
|
||||
bool allowNetwork = true,
|
||||
List<ReleaseEntry> bundled = releaseHistory,
|
||||
int pageLimit = 5,
|
||||
int githubPageSize = 100,
|
||||
int giteaPageSize = 50,
|
||||
}) async {
|
||||
final cached = await readCache();
|
||||
if (!allowNetwork) {
|
||||
final stored = decodeReleaseCache(cached);
|
||||
if (stored != null) {
|
||||
return ReleaseSync(
|
||||
entries: stored.entries,
|
||||
origin: HistoryOrigin.cache,
|
||||
note: '下面是上次保存的列表。',
|
||||
);
|
||||
}
|
||||
return ReleaseSync(
|
||||
entries: bundled,
|
||||
origin: HistoryOrigin.bundled,
|
||||
note: '',
|
||||
);
|
||||
}
|
||||
final result = await syncReleaseHistory(
|
||||
preferred: preferred,
|
||||
endpoints: endpoints,
|
||||
fetch: fetch,
|
||||
cachedJson: cached,
|
||||
bundled: bundled,
|
||||
pageLimit: pageLimit,
|
||||
githubPageSize: githubPageSize,
|
||||
giteaPageSize: giteaPageSize,
|
||||
);
|
||||
final json = result.cacheJson;
|
||||
if (json != null) {
|
||||
await writeCache(json);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -3,6 +3,65 @@
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
/// Where release metadata is downloaded from. YanJiu Git is the default
|
||||
/// because github.com is often blocked by DNS pollution on the user's network.
|
||||
enum UpdateSource { gitea, github }
|
||||
|
||||
const updateSourceGitea = 'gitea';
|
||||
const updateSourceGithub = 'github';
|
||||
|
||||
UpdateSource parseUpdateSource(String? raw) =>
|
||||
raw == updateSourceGithub ? UpdateSource.github : UpdateSource.gitea;
|
||||
|
||||
String updateSourceId(UpdateSource source) =>
|
||||
source == UpdateSource.github ? updateSourceGithub : updateSourceGitea;
|
||||
|
||||
String updateSourceLabel(UpdateSource source) =>
|
||||
source == UpdateSource.github ? 'GitHub' : 'YanJiu Git';
|
||||
|
||||
/// Environment variables override the live release URLs. Empty values keep
|
||||
/// the defaults, so a blank test variable does not point the client nowhere.
|
||||
UpdateEndpoints updateEndpoints({String? githubApi, String? giteaApi}) {
|
||||
return UpdateEndpoints(
|
||||
github: githubApi == null || githubApi.trim().isEmpty
|
||||
? UpdateEndpoints.githubDefault
|
||||
: githubApi.trim(),
|
||||
gitea: giteaApi == null || giteaApi.trim().isEmpty
|
||||
? UpdateEndpoints.giteaDefault
|
||||
: giteaApi.trim(),
|
||||
);
|
||||
}
|
||||
|
||||
/// 429, a spent quota header, or a 403 body that says the limit was hit.
|
||||
bool isReleaseRateLimited({
|
||||
required int status,
|
||||
String? rateLimitRemaining,
|
||||
required String body,
|
||||
}) {
|
||||
if (status == 429) {
|
||||
return true;
|
||||
}
|
||||
if (status != 403) {
|
||||
return false;
|
||||
}
|
||||
if (rateLimitRemaining == '0') {
|
||||
return true;
|
||||
}
|
||||
return body.toLowerCase().contains('rate limit');
|
||||
}
|
||||
|
||||
/// The release API refused the request because the caller asked too often.
|
||||
class UpdateFetchException implements Exception {
|
||||
const UpdateFetchException({required this.rateLimited, this.status});
|
||||
|
||||
final bool rateLimited;
|
||||
final int? status;
|
||||
|
||||
@override
|
||||
String toString() =>
|
||||
'UpdateFetchException(rateLimited: $rateLimited, status: $status)';
|
||||
}
|
||||
|
||||
/// Numeric version. A leading v and a +build suffix are ignored.
|
||||
class SemVer implements Comparable<SemVer> {
|
||||
const SemVer(this.major, this.minor, this.patch);
|
||||
@@ -68,6 +127,9 @@ class UpdateEndpoints {
|
||||
|
||||
final String github;
|
||||
final String gitea;
|
||||
|
||||
String latestFor(UpdateSource source) =>
|
||||
source == UpdateSource.github ? github : gitea;
|
||||
}
|
||||
|
||||
class UpdateOffer {
|
||||
@@ -87,11 +149,23 @@ class UpdateOffer {
|
||||
}
|
||||
|
||||
class UpdateCheck {
|
||||
const UpdateCheck({this.offer, required this.reachable, this.message = ''});
|
||||
const UpdateCheck({
|
||||
this.offer,
|
||||
required this.reachable,
|
||||
this.message = '',
|
||||
this.used,
|
||||
this.fellBack = false,
|
||||
});
|
||||
|
||||
final UpdateOffer? offer;
|
||||
final bool reachable;
|
||||
final String message;
|
||||
|
||||
/// The source that actually answered. Null when neither source could be used.
|
||||
final UpdateSource? used;
|
||||
|
||||
/// True when [used] is the spare source because the chosen one failed.
|
||||
final bool fellBack;
|
||||
}
|
||||
|
||||
/// Picks the client archive for this OS and CPU. Server packages are ignored.
|
||||
@@ -120,7 +194,8 @@ String? pickClientAsset(
|
||||
if (!archOk || !name.contains(wantOs)) {
|
||||
continue;
|
||||
}
|
||||
if (wantOs == 'windows' && (name.endsWith('.zip') || name.endsWith('.exe'))) {
|
||||
if (wantOs == 'windows' &&
|
||||
(name.endsWith('.zip') || name.endsWith('.exe'))) {
|
||||
return asset.name;
|
||||
}
|
||||
if (wantOs == 'linux' &&
|
||||
@@ -182,7 +257,8 @@ UpdateCheck interpretRelease({
|
||||
if (pickedName == null) {
|
||||
return UpdateCheck(
|
||||
reachable: true,
|
||||
message: '发现 ${remote.major}.${remote.minor}.${remote.patch},但没有适合这台设备的安装包',
|
||||
message:
|
||||
'发现 ${remote.major}.${remote.minor}.${remote.patch},但没有适合这台设备的安装包',
|
||||
);
|
||||
}
|
||||
final picked = assets.firstWhere((asset) => asset.name == pickedName);
|
||||
@@ -205,33 +281,140 @@ UpdateCheck interpretRelease({
|
||||
);
|
||||
}
|
||||
|
||||
enum _SourceFailure { down, rate }
|
||||
|
||||
/// Asks [preferred] first. A source that answers, even with "already current",
|
||||
/// is kept. The other source is used only when the chosen one cannot be read,
|
||||
/// and the message names the source that actually answered.
|
||||
Future<UpdateCheck> findUpdate({
|
||||
required String current,
|
||||
required UpdateEndpoints endpoints,
|
||||
required UpdateSource preferred,
|
||||
required Future<String?> Function(Uri url) fetch,
|
||||
required String os,
|
||||
required String arch,
|
||||
}) async {
|
||||
final order = preferred == UpdateSource.github
|
||||
? const [UpdateSource.github, UpdateSource.gitea]
|
||||
: const [UpdateSource.gitea, UpdateSource.github];
|
||||
var sawRate = false;
|
||||
var sawDown = false;
|
||||
_SourceFailure? preferredFailure;
|
||||
for (var i = 0; i < order.length; i++) {
|
||||
final source = order[i];
|
||||
final outcome = await _readLatest(
|
||||
url: endpoints.latestFor(source),
|
||||
fetch: fetch,
|
||||
current: current,
|
||||
os: os,
|
||||
arch: arch,
|
||||
);
|
||||
final failure = outcome.failure;
|
||||
if (failure == null) {
|
||||
final check = outcome.check!;
|
||||
final fellBack = i > 0;
|
||||
return UpdateCheck(
|
||||
reachable: true,
|
||||
offer: check.offer,
|
||||
used: source,
|
||||
fellBack: fellBack,
|
||||
message: _withSource(
|
||||
check,
|
||||
_sourceClause(
|
||||
used: source,
|
||||
fellBack: fellBack,
|
||||
preferredFailure: preferredFailure,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (i == 0) {
|
||||
preferredFailure = failure;
|
||||
}
|
||||
if (failure == _SourceFailure.rate) {
|
||||
sawRate = true;
|
||||
} else {
|
||||
sawDown = true;
|
||||
}
|
||||
}
|
||||
return UpdateCheck(reachable: false, message: _bothFailed(sawRate, sawDown));
|
||||
}
|
||||
|
||||
class _LatestOutcome {
|
||||
const _LatestOutcome.ok(this.check) : failure = null;
|
||||
const _LatestOutcome.fail(this.failure) : check = null;
|
||||
|
||||
final UpdateCheck? check;
|
||||
final _SourceFailure? failure;
|
||||
}
|
||||
|
||||
Future<_LatestOutcome> _readLatest({
|
||||
required String url,
|
||||
required Future<String?> Function(Uri url) fetch,
|
||||
required String current,
|
||||
required String os,
|
||||
required String arch,
|
||||
}) async {
|
||||
String? body;
|
||||
try {
|
||||
body = await fetch(Uri.parse(endpoints.github));
|
||||
body = await fetch(Uri.parse(url));
|
||||
} on UpdateFetchException catch (error) {
|
||||
if (error.rateLimited) {
|
||||
return const _LatestOutcome.fail(_SourceFailure.rate);
|
||||
}
|
||||
return const _LatestOutcome.fail(_SourceFailure.down);
|
||||
} on Object {
|
||||
body = null;
|
||||
return const _LatestOutcome.fail(_SourceFailure.down);
|
||||
}
|
||||
var check = interpretRelease(current: current, body: body, os: os, arch: arch);
|
||||
if (body == null) {
|
||||
return const _LatestOutcome.fail(_SourceFailure.down);
|
||||
}
|
||||
final check = interpretRelease(
|
||||
current: current,
|
||||
body: body,
|
||||
os: os,
|
||||
arch: arch,
|
||||
);
|
||||
if (!check.reachable) {
|
||||
try {
|
||||
body = await fetch(Uri.parse(endpoints.gitea));
|
||||
} on Object {
|
||||
body = null;
|
||||
}
|
||||
check = interpretRelease(current: current, body: body, os: os, arch: arch);
|
||||
if (!check.reachable) {
|
||||
return const UpdateCheck(
|
||||
reachable: false,
|
||||
message: 'GitHub 和备用源都连不上,没有检查到更新',
|
||||
);
|
||||
}
|
||||
return const _LatestOutcome.fail(_SourceFailure.down);
|
||||
}
|
||||
return check;
|
||||
return _LatestOutcome.ok(check);
|
||||
}
|
||||
|
||||
String _sourceClause({
|
||||
required UpdateSource used,
|
||||
required bool fellBack,
|
||||
required _SourceFailure? preferredFailure,
|
||||
}) {
|
||||
final usedLabel = updateSourceLabel(used);
|
||||
if (!fellBack) {
|
||||
return '这次用的是 $usedLabel';
|
||||
}
|
||||
final preferredLabel = updateSourceLabel(
|
||||
used == UpdateSource.github ? UpdateSource.gitea : UpdateSource.github,
|
||||
);
|
||||
if (preferredFailure == _SourceFailure.rate) {
|
||||
return '$preferredLabel 暂时限制了请求次数,这次用的是 $usedLabel';
|
||||
}
|
||||
return '$preferredLabel 连不上,这次用的是 $usedLabel';
|
||||
}
|
||||
|
||||
String _withSource(UpdateCheck check, String clause) {
|
||||
if (check.offer != null) {
|
||||
return '发现 ${check.offer!.version}。可以下载并覆盖安装。($clause)';
|
||||
}
|
||||
if (check.message.isEmpty) {
|
||||
return clause;
|
||||
}
|
||||
return '${check.message}($clause)';
|
||||
}
|
||||
|
||||
String _bothFailed(bool sawRate, bool sawDown) {
|
||||
if (sawRate && sawDown) {
|
||||
return '所选更新源暂时限制了请求次数,另一个也连不上,没有检查到更新';
|
||||
}
|
||||
if (sawRate) {
|
||||
return 'YanJiu Git 和 GitHub 都暂时限制了请求次数,请稍后再试';
|
||||
}
|
||||
return 'YanJiu Git 和 GitHub 都连不上,没有检查到更新';
|
||||
}
|
||||
Reference in new issue
Block a user