Skip to content

Commit

Permalink
Fix filename issue, add method for getDownloadModelById
Browse files Browse the repository at this point in the history
  • Loading branch information
khushpanchal committed Jan 12, 2025
1 parent d36a416 commit aed6043
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
16 changes: 15 additions & 1 deletion ketch/src/main/java/com/ketch/Ketch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class Ketch private constructor(
* @param headers Optional headers sent when making api call for file download
* @return Unique Download ID associated with current download
*/
@Synchronized
fun download(
url: String,
path: String,
Expand All @@ -193,10 +194,15 @@ class Ketch private constructor(
"Missing ${if (url.isEmpty()) "url" else if (path.isEmpty()) "path" else "fileName"}"
}

// This will create a temp file which will be renamed after successful download.
// This will also make sure each file name is unique.
val newFileName = FileUtil.resolveNamingConflicts(fileName, path)
FileUtil.createTempFileIfNotExists(path, newFileName)

val downloadRequest = DownloadRequest(
url = url,
path = path,
fileName = fileName,
fileName = newFileName,
tag = tag,
headers = headers,
metaData = metaData
Expand Down Expand Up @@ -418,4 +424,12 @@ class Ketch private constructor(
*/
suspend fun getAllDownloads() = downloadManager.getAllDownloads()

/**
* Suspend function to get download model by id
*
* @param id
* @return [DownloadModel] if present else null
*/
suspend fun getDownloadModelById(id: Int) = downloadManager.getDownloadModelById(id)

}
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,8 @@ internal class DownloadManager(
}
}

suspend fun getDownloadModelById(id: Int): DownloadModel? {
return downloadDao.find(id)?.toDownloadModel()
}

}
31 changes: 29 additions & 2 deletions ketch/src/main/java/com/ketch/internal/utils/FileUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import kotlin.experimental.and

internal object FileUtil {

fun getTempFileForFile(file: File): File{
return File(file.absolutePath+".temp")
fun getTempFileForFile(file: File): File {
return File(file.absolutePath + ".temp")
}

fun getFileNameFromUrl(url: String): String {
Expand Down Expand Up @@ -51,4 +51,31 @@ internal object FileUtil {
if (it.exists()) it.delete()
}
}

// If file name already exist at given path, generate new file name with (1), (2) etc. suffix
fun resolveNamingConflicts(fileName: String, path: String): String {
var newFileName = fileName
var file = File(path, newFileName)
var tempFile = getTempFileForFile(file)
var counter = 1

while (file.exists() || tempFile.exists()) {
val name = fileName.substringBeforeLast(".")
val extension = fileName.substringAfterLast(".")
newFileName = "$name ($counter).$extension"
file = File(path, newFileName)
tempFile = getTempFileForFile(file)
counter++
}

return newFileName
}

fun createTempFileIfNotExists(path: String, fileName: String) {
val file = File(path, fileName)
val tempFile = getTempFileForFile(file)
if (!tempFile.exists()) {
tempFile.createNewFile()
}
}
}

0 comments on commit aed6043

Please sign in to comment.