Skip to content

Commit

Permalink
Update backup
Browse files Browse the repository at this point in the history
  • Loading branch information
SeniorZhai committed Dec 3, 2024
1 parent fd9a2b0 commit f460dd6
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions app/src/main/java/one/mixin/android/util/backup/Backup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import one.mixin.android.extension.getDisplayPath
import one.mixin.android.extension.getLegacyBackupPath
import one.mixin.android.extension.getMediaPath
import one.mixin.android.extension.getOldBackupPath
import one.mixin.android.util.database.databaseFile
import one.mixin.android.util.database.dbDir
import timber.log.Timber
import java.io.File
Expand All @@ -37,7 +38,7 @@ suspend fun backup(
context: Context,
callback: (Result) -> Unit,
) = coroutineScope {
val dbFile = context.getDatabasePath(DB_NAME) ?: dbDir(context)
val dbFile = databaseFile(context)
if (dbFile == null) {
withContext(Dispatchers.Main) {
Timber.e("No database files found")
Expand Down Expand Up @@ -194,15 +195,18 @@ suspend fun backupApi29(
}
return@withContext
}
val dbFile = context.getDatabasePath(DB_NAME) ?: dbDir(context)
if (dbFile == null) {
val dbFile = databaseFile(context)
if (!dbFile.exists() && dbFile.length() <= 0) {
Timber.e("No database files found")
withContext(Dispatchers.Main) {
callback(Result.NOT_FOUND)
}
return@withContext
}
val tmpFile = File(context.getMediaPath(), DB_NAME)
if (tmpFile.parentFile?.exists() != true) {
tmpFile.parentFile?.mkdirs()
}
try {
val inputStream = dbFile.inputStream()
MixinDatabase.checkPoint()
Expand Down Expand Up @@ -260,10 +264,7 @@ suspend fun restore(
val target =
internalFindBackup(context, coroutineContext)
?: return@withContext callback(Result.NOT_FOUND)
var file = context.getDatabasePath(DB_NAME)
if (!file.exists()){
file = dbDir(context)
}
var file = dbDir(context)
try {
if (file.exists()) {
file.delete()
Expand Down Expand Up @@ -313,7 +314,7 @@ suspend fun restoreApi29(
}
return@withContext
}
val file = dbDir(context) ?: return@withContext
val file = databaseFile(context)
try {
val inputStream = context.contentResolver.openInputStream(backupDb.uri)
if (inputStream == null) {
Expand Down Expand Up @@ -411,7 +412,7 @@ suspend fun findBackupApi29(
return@withContext null
}
val backupChildDirectory = backupDirectory.findFile(BACKUP_DIR_NAME)
val dbFile = backupChildDirectory?.findFile("mixin.db")
val dbFile = backupChildDirectory?.findFile(DB_NAME)
if (backupChildDirectory == null || !backupChildDirectory.exists() || backupChildDirectory.length() <= 0 || dbFile == null || !dbFile.exists() || dbFile.length() <= 0) {
return@withContext null
}
Expand Down

0 comments on commit f460dd6

Please sign in to comment.