Skip to content

Commit

Permalink
add assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Axelen123 committed Jul 13, 2024
1 parent c0f6699 commit c8975f5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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 {
val scope = object : DownloadScope {
override val saveLocation = savePath.resolve("base.apk")
override suspend fun reportProgress(bytesReceived: Int, bytesTotal: Int?) = onDownload(bytesReceived.megaBytes to bytesTotal?.megaBytes)
override val targetFile = targetFile
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)

if (!targetFile.exists()) throw Exception("Downloader did not download any files")

dao.insert(
DownloadedApp(
packageName = app.packageName,
Expand All @@ -49,12 +58,12 @@ class DownloadedAppRepository(app: Application, db: AppDatabase) {
)
)
} catch (e: Exception) {
savePath.deleteRecursively()
saveDir.deleteRecursively()
throw e
}

// Return the Apk file.
return getApkFileForDir(savePath)
return getApkFileForDir(saveDir)
}

suspend fun get(packageName: String, version: String) = dao.get(packageName, version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface DownloadScope {
/**
* The location where the downloaded APK should be saved.
*/
val saveLocation: File
val targetFile: File

/**
* A callback for reporting download progress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ private fun installedAppDownloader(context: DownloaderContext) = downloader<Inst
}

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 {
versionPager { packageName, versionHint ->
Expand Down Expand Up @@ -81,7 +81,7 @@ val examplePaginatedDownloader = paginatedDownloader {

download {
for (i in 0..5) {
reportProgress(i.megabytes , 5.megabytes)
reportProgress(i.megaBytes , 5.megaBytes)
delay(1000L)
}

Expand Down

0 comments on commit c8975f5

Please sign in to comment.