add assertions

This commit is contained in:
Ax333l 2024-07-13 22:57:49 +02:00
parent c0f6699c0d
commit c8975f55bf
No known key found for this signature in database
GPG Key ID: D2B4D85271127D23
3 changed files with 18 additions and 9 deletions

View File

@ -31,16 +31,25 @@ class DownloadedAppRepository(app: Application, db: AppDatabase) {
// Converted integers cannot contain / or .. unlike the package name or version, so they are safer to use here. // Converted integers cannot contain / or .. unlike the package name or version, so they are safer to use here.
val relativePath = File(generateUid().toString()) val relativePath = File(generateUid().toString())
val savePath = dir.resolve(relativePath).also { it.mkdirs() } val saveDir = dir.resolve(relativePath).also { it.mkdirs() }
val targetFile = saveDir.resolve("base.apk")
try { try {
val scope = object : DownloadScope { val scope = object : DownloadScope {
override val saveLocation = savePath.resolve("base.apk") override val targetFile = targetFile
override suspend fun reportProgress(bytesReceived: Int, bytesTotal: Int?) = onDownload(bytesReceived.megaBytes to bytesTotal?.megaBytes) override suspend fun reportProgress(bytesReceived: Int, bytesTotal: Int?) {
require(bytesReceived >= 0) { "bytesReceived must not be negative" }
require(bytesTotal == null || bytesTotal >= bytesReceived) { "bytesTotal must be greater than or equal to bytesReceived" }
require(bytesTotal != 0) { "bytesTotal must not be zero" }
onDownload(bytesReceived.megaBytes to bytesTotal?.megaBytes)
}
} }
plugin.download(scope, app) plugin.download(scope, app)
if (!targetFile.exists()) throw Exception("Downloader did not download any files")
dao.insert( dao.insert(
DownloadedApp( DownloadedApp(
packageName = app.packageName, packageName = app.packageName,
@ -49,12 +58,12 @@ class DownloadedAppRepository(app: Application, db: AppDatabase) {
) )
) )
} catch (e: Exception) { } catch (e: Exception) {
savePath.deleteRecursively() saveDir.deleteRecursively()
throw e throw e
} }
// Return the Apk file. // Return the Apk file.
return getApkFileForDir(savePath) return getApkFileForDir(saveDir)
} }
suspend fun get(packageName: String, version: String) = dao.get(packageName, version) suspend fun get(packageName: String, version: String) = dao.get(packageName, version)

View File

@ -6,7 +6,7 @@ interface DownloadScope {
/** /**
* The location where the downloaded APK should be saved. * The location where the downloaded APK should be saved.
*/ */
val saveLocation: File val targetFile: File
/** /**
* A callback for reporting download progress * A callback for reporting download progress

View File

@ -43,11 +43,11 @@ private fun installedAppDownloader(context: DownloaderContext) = downloader<Inst
} }
download { download {
Files.copy(Path(it.apkPath), saveLocation.toPath(), StandardCopyOption.REPLACE_EXISTING) Files.copy(Path(it.apkPath), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
} }
} }
private val Int.megabytes get() = times(1_000_000) private val Int.megaBytes get() = times(1_000_000)
val examplePaginatedDownloader = paginatedDownloader { val examplePaginatedDownloader = paginatedDownloader {
versionPager { packageName, versionHint -> versionPager { packageName, versionHint ->
@ -81,7 +81,7 @@ val examplePaginatedDownloader = paginatedDownloader {
download { download {
for (i in 0..5) { for (i in 0..5) {
reportProgress(i.megabytes , 5.megabytes) reportProgress(i.megaBytes , 5.megaBytes)
delay(1000L) delay(1000L)
} }