Fix non-APK GitHub release asset downloads (#1581)

This commit is contained in:
Imran Remtulla
2024-05-05 21:35:16 -04:00
parent c0a2e372e5
commit a3fddc5400
2 changed files with 21 additions and 22 deletions

View File

@@ -273,10 +273,11 @@ class GitHub extends AppSource {
List<MapEntry<String, String>> getReleaseAssetUrls(dynamic release) => List<MapEntry<String, String>> getReleaseAssetUrls(dynamic release) =>
(release['assets'] as List<dynamic>?)?.map((e) { (release['assets'] as List<dynamic>?)?.map((e) {
return (e['name'] != null) && var url = !e['name'].toString().toLowerCase().endsWith('.apk')
((e['url'] ?? e['browser_download_url']) != null) ? (e['browser_download_url'] ?? e['url'])
? MapEntry(e['name'] as String, : (e['url'] ?? e['browser_download_url']);
(e['url'] ?? e['browser_download_url']) as String) return (e['name'] != null) && (url != null)
? MapEntry(e['name'] as String, url as String)
: const MapEntry('', ''); : const MapEntry('', '');
}).toList() ?? }).toList() ??
[]; [];

View File

@@ -142,19 +142,20 @@ List<MapEntry<String, int>> moveStrToEndMapEntryWithCount(
return arr; return arr;
} }
Future<File> downloadFileWithRetry( Future<File> downloadFileWithRetry(String url, String fileName,
String url, String fileNameNoExt, Function? onProgress, String destDir, bool fileNameHasExt, Function? onProgress, String destDir,
{bool useExisting = true, {bool useExisting = true,
Map<String, String>? headers, Map<String, String>? headers,
int retries = 3}) async { int retries = 3}) async {
try { try {
return await downloadFile(url, fileNameNoExt, onProgress, destDir, return await downloadFile(
url, fileName, fileNameHasExt, onProgress, destDir,
useExisting: useExisting, headers: headers); useExisting: useExisting, headers: headers);
} catch (e) { } catch (e) {
if (retries > 0 && e is ClientException) { if (retries > 0 && e is ClientException) {
await Future.delayed(const Duration(seconds: 5)); await Future.delayed(const Duration(seconds: 5));
return await downloadFileWithRetry( return await downloadFileWithRetry(
url, fileNameNoExt, onProgress, destDir, url, fileName, fileNameHasExt, onProgress, destDir,
useExisting: useExisting, headers: headers, retries: (retries - 1)); useExisting: useExisting, headers: headers, retries: (retries - 1));
} else { } else {
rethrow; rethrow;
@@ -201,8 +202,8 @@ Future<String> checkPartialDownloadHash(String url, int bytesToGrab,
return hashListOfLists(bytes); return hashListOfLists(bytes);
} }
Future<File> downloadFile( Future<File> downloadFile(String url, String fileName, bool fileNameHasExt,
String url, String fileNameNoExt, Function? onProgress, String destDir, Function? onProgress, String destDir,
{bool useExisting = true, Map<String, String>? headers}) async { {bool useExisting = true, Map<String, String>? headers}) async {
// Send the initial request but cancel it as soon as you have the headers // Send the initial request but cancel it as soon as you have the headers
var reqHeaders = headers ?? {}; var reqHeaders = headers ?? {};
@@ -222,7 +223,11 @@ Future<File> downloadFile(
if (url.toLowerCase().endsWith('.apk') && ext != 'apk') { if (url.toLowerCase().endsWith('.apk') && ext != 'apk') {
ext = 'apk'; ext = 'apk';
} }
File downloadedFile = File('$destDir/$fileNameNoExt.$ext'); File downloadedFile = File('$destDir/$fileName.$ext');
if (fileNameHasExt) {
// If the user says the filename already has an ext, ignore whatever you inferred from above
downloadedFile = File('$destDir/$fileName');
}
bool rangeFeatureEnabled = false; bool rangeFeatureEnabled = false;
if (resHeaders['accept-ranges']?.isNotEmpty == true) { if (resHeaders['accept-ranges']?.isNotEmpty == true) {
@@ -435,8 +440,8 @@ class AppsProvider with ChangeNotifier {
var headers = await source.getRequestHeaders(app.additionalSettings, var headers = await source.getRequestHeaders(app.additionalSettings,
forAPKDownload: true); forAPKDownload: true);
var downloadedFile = await downloadFileWithRetry( var downloadedFile = await downloadFileWithRetry(
downloadUrl, fileNameNoExt, downloadUrl, fileNameNoExt, false, headers: headers,
headers: headers, (double? progress) { (double? progress) {
int? prog = progress?.ceil(); int? prog = progress?.ceil();
if (apps[app.id] != null) { if (apps[app.id] != null) {
apps[app.id]!.downloadProgress = progress; apps[app.id]!.downloadProgress = progress;
@@ -969,15 +974,8 @@ class AppsProvider with ChangeNotifier {
if (!downloadsAccessible && exportDir != null) { if (!downloadsAccessible && exportDir != null) {
downloadPath = exportDir.path; downloadPath = exportDir.path;
} }
await downloadFile( await downloadFile(fileUrl.value, fileUrl.key, true,
fileUrl.value, (double? progress) {
fileUrl.key
.split('.')
.reversed
.toList()
.sublist(1)
.reversed
.join('.'), (double? progress) {
notificationsProvider notificationsProvider
.notify(DownloadNotification(fileUrl.key, progress?.ceil() ?? 0)); .notify(DownloadNotification(fileUrl.key, progress?.ceil() ?? 0));
}, downloadPath, }, downloadPath,