Skip to content

Commit 5537021

Browse files
Merge pull request #120 from dev-diaries41/fix/backup
fix: ensure index files exist before hashing when backing and handle …
2 parents 151e134 + 40d2b59 commit 5537021

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

app/src/main/java/com/fpf/smartscan/ui/screens/settings/SettingsViewModel.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ class SettingsViewModel(private val application: Application) : AndroidViewModel
142142
val imageIndexFile = File(application.filesDir, ImageIndexer.INDEX_FILENAME)
143143
val videoIndexFile = File(application.filesDir, VideoIndexer.INDEX_FILENAME)
144144
val hashFile = File(application.cacheDir, HASH_FILENAME)
145+
val filesToZip = listOf(imageIndexFile, videoIndexFile, hashFile)
145146
_isBackupLoading.value = true
146147

147148
viewModelScope.launch(Dispatchers.IO){
148149
try {
149-
val imageHash = hashFile(imageIndexFile)
150-
val videoHash = hashFile(videoIndexFile)
151-
hashFile.writeText("$imageHash\n$videoHash")
150+
val hashes: List<String> = listOf(imageIndexFile, videoIndexFile).filter { it.exists() }.map{hashFile(it)}
151+
hashFile.writeText(hashes.joinToString("\n") )
152152

153-
zipFiles(indexZipFile, listOf(imageIndexFile, videoIndexFile, hashFile))
153+
zipFiles(indexZipFile, filesToZip)
154154
copyToUri(application, uri, indexZipFile)
155155
_event.emit("Backup successful")
156156
}catch (e: Exception){
@@ -193,7 +193,7 @@ class SettingsViewModel(private val application: Application) : AndroidViewModel
193193
private suspend fun isValidBackupFile(extractedFiles: List<File>): Boolean{
194194
val hashFile = extractedFiles.find { it.name == HASH_FILENAME }?: return false
195195
val hashesFromFile: List<String> = hashFile.readLines()
196-
if(hashesFromFile.size < 2) return false
196+
if(hashesFromFile.isEmpty()) return false // must have hash for at least 1 of image or video index file
197197

198198
val indexFiles = extractedFiles.filterNot{it.name == HASH_FILENAME}
199199
val indexHashes = indexFiles.map{hashFile(it)}

0 commit comments

Comments
 (0)