From 8abade8881bc2b79afe7cb27277ff6ae4c6d2903 Mon Sep 17 00:00:00 2001 From: Aayush Gupta Date: Wed, 27 Mar 2024 19:52:41 +0530 Subject: [PATCH] Avoid accessing file's path when working with downloads This will help us to use document tree in future to allow users custom download directory. Signed-off-by: Aayush Gupta --- .../com/aurora/store/data/model/Request.kt | 4 ++- .../aurora/store/data/work/DownloadWorker.kt | 27 +++++++++---------- .../java/com/aurora/store/util/PathUtil.kt | 10 +++---- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/aurora/store/data/model/Request.kt b/app/src/main/java/com/aurora/store/data/model/Request.kt index e283ed9ab..92435a024 100644 --- a/app/src/main/java/com/aurora/store/data/model/Request.kt +++ b/app/src/main/java/com/aurora/store/data/model/Request.kt @@ -1,8 +1,10 @@ package com.aurora.store.data.model +import java.io.File + data class Request( val url: String, - val filePath: String, + val file: File, val size: Long, val sha1: String, val sha256: String diff --git a/app/src/main/java/com/aurora/store/data/work/DownloadWorker.kt b/app/src/main/java/com/aurora/store/data/work/DownloadWorker.kt index 2f756c186..07a9ed3b0 100644 --- a/app/src/main/java/com/aurora/store/data/work/DownloadWorker.kt +++ b/app/src/main/java/com/aurora/store/data/work/DownloadWorker.kt @@ -167,7 +167,7 @@ class DownloadWorker @AssistedInject constructor( } } - if (!requestList.all { File(it.filePath).exists() }) return Result.failure() + if (!requestList.all { it.file.exists() }) return Result.failure() // Mark download as completed onSuccess() @@ -220,7 +220,7 @@ class DownloadWorker @AssistedInject constructor( private fun getDownloadRequest(files: List, libPackageName: String?): List { val downloadList = mutableListOf() files.filter { it.url.isNotBlank() }.forEach { - val filePath = when (it.type) { + val file = when (it.type) { GPlayFile.FileType.BASE, GPlayFile.FileType.SPLIT -> { PathUtil.getApkDownloadFile( appContext, download.packageName, download.versionCode, it, libPackageName @@ -231,26 +231,25 @@ class DownloadWorker @AssistedInject constructor( PathUtil.getObbDownloadFile(download.packageName, it) } } - downloadList.add(Request(it.url, filePath, it.size, it.sha1, it.sha256)) + downloadList.add(Request(it.url, file, it.size, it.sha1, it.sha256)) } return downloadList } private suspend fun downloadFile(request: Request): Result { return withContext(Dispatchers.IO) { - val requestFile = File(request.filePath) val algorithm = if (request.sha256.isBlank()) Algorithm.SHA1 else Algorithm.SHA256 val expectedSha = if (algorithm == Algorithm.SHA1) request.sha1 else request.sha256 // If file exists and sha matches the request, no need to download again - if (requestFile.exists() && validSha(requestFile, expectedSha, algorithm)) { - Log.i(TAG, "$requestFile is already downloaded!") - downloadedBytes += requestFile.length() + if (request.file.exists() && validSha(request.file, expectedSha, algorithm)) { + Log.i(TAG, "${request.file} is already downloaded!") + downloadedBytes += request.file.length() return@withContext Result.success() } try { - val isNewFile = requestFile.createNewFile() + val isNewFile = request.file.createNewFile() val connection = if (proxy != null) { URL(request.url).openConnection(proxy) as HttpsURLConnection } else { @@ -258,22 +257,22 @@ class DownloadWorker @AssistedInject constructor( } if (!isNewFile) { - Log.i(TAG, "$requestFile has an unfinished download, resuming!") - downloadedBytes += requestFile.length() - connection.setRequestProperty("Range", "bytes=${requestFile.length()}-") + Log.i(TAG, "${request.file} has an unfinished download, resuming!") + downloadedBytes += request.file.length() + connection.setRequestProperty("Range", "bytes=${request.file.length()}-") } connection.inputStream.use { input -> - FileOutputStream(requestFile, !isNewFile).use { + FileOutputStream(request.file, !isNewFile).use { input.copyTo(it, request.size).collectLatest { p -> onProgress(p) } } } // Ensure downloaded file matches expected sha - assert(validSha(requestFile, expectedSha, algorithm)) + assert(validSha(request.file, expectedSha, algorithm)) return@withContext Result.success() } catch (exception: Exception) { - Log.e(TAG, "Failed to download ${request.filePath}!", exception) + Log.e(TAG, "Failed to download ${request.file}!", exception) notifyStatus(DownloadStatus.FAILED) return@withContext Result.failure() } diff --git a/app/src/main/java/com/aurora/store/util/PathUtil.kt b/app/src/main/java/com/aurora/store/util/PathUtil.kt index c11a740e9..f4c65f9a9 100644 --- a/app/src/main/java/com/aurora/store/util/PathUtil.kt +++ b/app/src/main/java/com/aurora/store/util/PathUtil.kt @@ -50,7 +50,7 @@ object PathUtil { sharedLibPackageName: String ): File { return File( - getAppDownloadDir(context, packageName, versionCode).absolutePath, + getAppDownloadDir(context, packageName, versionCode), "$LIBRARIES/$sharedLibPackageName" ) } @@ -61,13 +61,13 @@ object PathUtil { versionCode: Int, file: GPlayFile, sharedLibPackageName: String? = null - ): String { + ): File { val downloadDir = if (!sharedLibPackageName.isNullOrBlank()) { getLibDownloadDir(context, packageName, versionCode, sharedLibPackageName) } else { File(getPackageDirectory(context, packageName), versionCode.toString()) } - return File(downloadDir, file.name).absolutePath + return File(downloadDir, file.name) } fun getZipFile(context: Context, packageName: String, versionCode: Int): File { @@ -84,8 +84,8 @@ object PathUtil { return File(Environment.getExternalStorageDirectory(), "/Android/obb/$packageName") } - fun getObbDownloadFile(packageName: String, file: GPlayFile): String { - return File(getObbDownloadDir(packageName), file.name).absolutePath + fun getObbDownloadFile(packageName: String, file: GPlayFile): File { + return File(getObbDownloadDir(packageName), file.name) } fun needsStorageManagerPerm(fileList: List): Boolean {