|
|
@ -21,14 +21,6 @@ object Backup { |
|
|
|
FileUtils.getDirFile(App.INSTANCE.filesDir, "backup").absolutePath |
|
|
|
FileUtils.getDirFile(App.INSTANCE.filesDir, "backup").absolutePath |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val legadoPath by lazy { |
|
|
|
|
|
|
|
FileUtils.getSdCardPath() + File.separator + "YueDu3.0" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val exportPath by lazy { |
|
|
|
|
|
|
|
legadoPath + File.separator + "Export" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val backupFileNames by lazy { |
|
|
|
val backupFileNames by lazy { |
|
|
|
arrayOf( |
|
|
|
arrayOf( |
|
|
|
"bookshelf.json", "bookGroup.json", "bookSource.json", "rssSource.json", |
|
|
|
"bookshelf.json", "bookGroup.json", "bookSource.json", "rssSource.json", |
|
|
@ -42,16 +34,13 @@ object Backup { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
Coroutine.async { |
|
|
|
Coroutine.async { |
|
|
|
val backupPath = context.getPrefString(PreferKey.backupPath) |
|
|
|
context.getPrefString(PreferKey.backupPath)?.let { |
|
|
|
if (backupPath.isNullOrEmpty()) { |
|
|
|
backup(context, it, true) |
|
|
|
backup(context) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
backup(context, backupPath) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
suspend fun backup(context: Context, path: String = legadoPath) { |
|
|
|
suspend fun backup(context: Context, path: String, isAuto: Boolean = false) { |
|
|
|
context.putPrefLong(PreferKey.lastBackup, System.currentTimeMillis()) |
|
|
|
context.putPrefLong(PreferKey.lastBackup, System.currentTimeMillis()) |
|
|
|
withContext(IO) { |
|
|
|
withContext(IO) { |
|
|
|
synchronized(this@Backup) { |
|
|
|
synchronized(this@Backup) { |
|
|
@ -81,9 +70,9 @@ object Backup { |
|
|
|
} |
|
|
|
} |
|
|
|
WebDavHelp.backUpWebDav(backupPath) |
|
|
|
WebDavHelp.backUpWebDav(backupPath) |
|
|
|
if (path.isContentPath()) { |
|
|
|
if (path.isContentPath()) { |
|
|
|
copyBackup(context, Uri.parse(path)) |
|
|
|
copyBackup(context, Uri.parse(path), isAuto) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
copyBackup(File(path)) |
|
|
|
copyBackup(File(path), isAuto) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -97,28 +86,39 @@ object Backup { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Throws(java.lang.Exception::class) |
|
|
|
@Throws(java.lang.Exception::class) |
|
|
|
private fun copyBackup(context: Context, uri: Uri) { |
|
|
|
private fun copyBackup(context: Context, uri: Uri, isAuto: Boolean) { |
|
|
|
|
|
|
|
|
|
|
|
DocumentFile.fromTreeUri(context, uri)?.let { treeDoc -> |
|
|
|
DocumentFile.fromTreeUri(context, uri)?.let { treeDoc -> |
|
|
|
for (fileName in backupFileNames) { |
|
|
|
for (fileName in backupFileNames) { |
|
|
|
val file = File(backupPath + File.separator + fileName) |
|
|
|
val file = File(backupPath + File.separator + fileName) |
|
|
|
if (file.exists()) { |
|
|
|
if (file.exists()) { |
|
|
|
treeDoc.findFile(fileName)?.delete() |
|
|
|
if (isAuto) { |
|
|
|
treeDoc.createFile("", fileName) |
|
|
|
treeDoc.findFile("auto")?.findFile(fileName)?.delete() |
|
|
|
?.writeBytes(context, file.readBytes()) |
|
|
|
DocumentUtils.createFileIfNotExist( |
|
|
|
|
|
|
|
treeDoc, |
|
|
|
|
|
|
|
fileName, |
|
|
|
|
|
|
|
subDirs = *arrayOf("auto") |
|
|
|
|
|
|
|
)?.writeBytes(context, file.readBytes()) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
treeDoc.findFile(fileName)?.delete() |
|
|
|
|
|
|
|
treeDoc.createFile("", fileName) |
|
|
|
|
|
|
|
?.writeBytes(context, file.readBytes()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Throws(java.lang.Exception::class) |
|
|
|
@Throws(java.lang.Exception::class) |
|
|
|
private fun copyBackup(rootFile: File) { |
|
|
|
private fun copyBackup(rootFile: File, isAuto: Boolean) { |
|
|
|
for (fileName in backupFileNames) { |
|
|
|
for (fileName in backupFileNames) { |
|
|
|
val file = File(backupPath + File.separator + fileName) |
|
|
|
val file = File(backupPath + File.separator + fileName) |
|
|
|
if (file.exists()) { |
|
|
|
if (file.exists()) { |
|
|
|
file.copyTo( |
|
|
|
file.copyTo( |
|
|
|
FileUtils.createFileIfNotExist(rootFile, fileName), |
|
|
|
if (isAuto) { |
|
|
|
true |
|
|
|
FileUtils.createFileIfNotExist(rootFile, fileName, "auto") |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
FileUtils.createFileIfNotExist(rootFile, fileName) |
|
|
|
|
|
|
|
}, true |
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|