Smarter APK caching (#459)

This commit is contained in:
Imran Remtulla
2023-04-30 02:47:53 -04:00
parent 08586870fb
commit 7c592756fe

View File

@@ -113,21 +113,20 @@ class AppsProvider with ChangeNotifier {
() async { () async {
// Load Apps into memory (in background, this is done later instead of in the constructor) // Load Apps into memory (in background, this is done later instead of in the constructor)
await loadApps(); await loadApps();
// Delete existing APKs // Delete any partial APKs
(await getExternalStorageDirectory()) (await getExternalCacheDirectories())
?.listSync() ?.first
.where((element) => .listSync()
element.path.endsWith('.apk') || .where((element) => element.path.endsWith('.apk.part'))
element.path.endsWith('.apk.part')) .forEach((partialApk) {
.forEach((apk) { partialApk.delete();
apk.delete();
}); });
}(); }();
} }
downloadFile(String url, String fileName, Function? onProgress, downloadFile(String url, String fileName, Function? onProgress,
{bool useExisting = true}) async { {bool useExisting = true}) async {
var destDir = (await getExternalStorageDirectory())!.path; var destDir = (await getExternalCacheDirectories())!.first.path;
StreamedResponse response = StreamedResponse response =
await Client().send(Request('GET', Uri.parse(url))); await Client().send(Request('GET', Uri.parse(url)));
File downloadedFile = File('$destDir/$fileName'); File downloadedFile = File('$destDir/$fileName');
@@ -191,15 +190,6 @@ class AppsProvider with ChangeNotifier {
} }
prevProg = prog; prevProg = prog;
}); });
// Delete older versions of the APK if any
for (var file in downloadedFile.parent.listSync()) {
var fn = file.path.split('/').last;
if (fn.startsWith('${app.id}-') &&
fn.endsWith('.apk') &&
fn != fileName) {
file.delete();
}
}
// If the APK package ID is different from the App ID, it is either new (using a placeholder ID) or the ID has changed // If the APK package ID is different from the App ID, it is either new (using a placeholder ID) or the ID has changed
// The former case should be handled (give the App its real ID), the latter is a security issue // The former case should be handled (give the App its real ID), the latter is a security issue
var newInfo = await PackageArchiveInfo.fromPath(downloadedFile.path); var newInfo = await PackageArchiveInfo.fromPath(downloadedFile.path);
@@ -217,6 +207,15 @@ class AppsProvider with ChangeNotifier {
await saveApps([app], onlyIfExists: !isTempId); await saveApps([app], onlyIfExists: !isTempId);
} }
} }
// Delete older versions of the APK if any
for (var file in downloadedFile.parent.listSync()) {
var fn = file.path.split('/').last;
if (fn.startsWith('${app.id}-') &&
fn.endsWith('.apk') &&
fn != fileName) {
file.delete();
}
}
return DownloadedApk(app.id, downloadedFile); return DownloadedApk(app.id, downloadedFile);
} finally { } finally {
notificationsProvider?.cancel(notifId); notificationsProvider?.cancel(notifId);
@@ -289,6 +288,7 @@ class AppsProvider with ChangeNotifier {
} else if (code == 0) { } else if (code == 0) {
apps[file.appId]!.app.installedVersion = apps[file.appId]!.app.installedVersion =
apps[file.appId]!.app.latestVersion; apps[file.appId]!.app.latestVersion;
file.file.delete();
} }
await saveApps([apps[file.appId]!.app]); await saveApps([apps[file.appId]!.app]);
} }