Skip to content

Commit aed6043

Browse files
committed
Fix filename issue, add method for getDownloadModelById
1 parent d36a416 commit aed6043

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

ketch/src/main/java/com/ketch/Ketch.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ class Ketch private constructor(
180180
* @param headers Optional headers sent when making api call for file download
181181
* @return Unique Download ID associated with current download
182182
*/
183+
@Synchronized
183184
fun download(
184185
url: String,
185186
path: String,
@@ -193,10 +194,15 @@ class Ketch private constructor(
193194
"Missing ${if (url.isEmpty()) "url" else if (path.isEmpty()) "path" else "fileName"}"
194195
}
195196

197+
// This will create a temp file which will be renamed after successful download.
198+
// This will also make sure each file name is unique.
199+
val newFileName = FileUtil.resolveNamingConflicts(fileName, path)
200+
FileUtil.createTempFileIfNotExists(path, newFileName)
201+
196202
val downloadRequest = DownloadRequest(
197203
url = url,
198204
path = path,
199-
fileName = fileName,
205+
fileName = newFileName,
200206
tag = tag,
201207
headers = headers,
202208
metaData = metaData
@@ -418,4 +424,12 @@ class Ketch private constructor(
418424
*/
419425
suspend fun getAllDownloads() = downloadManager.getAllDownloads()
420426

427+
/**
428+
* Suspend function to get download model by id
429+
*
430+
* @param id
431+
* @return [DownloadModel] if present else null
432+
*/
433+
suspend fun getDownloadModelById(id: Int) = downloadManager.getDownloadModelById(id)
434+
421435
}

ketch/src/main/java/com/ketch/internal/download/DownloadManager.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,8 @@ internal class DownloadManager(
492492
}
493493
}
494494

495+
suspend fun getDownloadModelById(id: Int): DownloadModel? {
496+
return downloadDao.find(id)?.toDownloadModel()
497+
}
498+
495499
}

ketch/src/main/java/com/ketch/internal/utils/FileUtil.kt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import kotlin.experimental.and
99

1010
internal object FileUtil {
1111

12-
fun getTempFileForFile(file: File): File{
13-
return File(file.absolutePath+".temp")
12+
fun getTempFileForFile(file: File): File {
13+
return File(file.absolutePath + ".temp")
1414
}
1515

1616
fun getFileNameFromUrl(url: String): String {
@@ -51,4 +51,31 @@ internal object FileUtil {
5151
if (it.exists()) it.delete()
5252
}
5353
}
54+
55+
// If file name already exist at given path, generate new file name with (1), (2) etc. suffix
56+
fun resolveNamingConflicts(fileName: String, path: String): String {
57+
var newFileName = fileName
58+
var file = File(path, newFileName)
59+
var tempFile = getTempFileForFile(file)
60+
var counter = 1
61+
62+
while (file.exists() || tempFile.exists()) {
63+
val name = fileName.substringBeforeLast(".")
64+
val extension = fileName.substringAfterLast(".")
65+
newFileName = "$name ($counter).$extension"
66+
file = File(path, newFileName)
67+
tempFile = getTempFileForFile(file)
68+
counter++
69+
}
70+
71+
return newFileName
72+
}
73+
74+
fun createTempFileIfNotExists(path: String, fileName: String) {
75+
val file = File(path, fileName)
76+
val tempFile = getTempFileForFile(file)
77+
if (!tempFile.exists()) {
78+
tempFile.createNewFile()
79+
}
80+
}
5481
}

0 commit comments

Comments
 (0)