This commit is contained in:
Horis 2023-11-27 21:58:52 +08:00
parent 8df1cb8ab6
commit 53aeac8d51
2 changed files with 27 additions and 17 deletions

View File

@ -249,10 +249,11 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
val fontPath = ReadBookConfig.textFont
if (fontPath.isNotEmpty()) {
val fontName = FileUtils.getName(fontPath)
val fontBytes = fontPath.parseToUri().readBytes(requireContext())
fontBytes.let {
val fontInputStream =
fontPath.parseToUri().inputStream(requireContext()).getOrNull()
fontInputStream?.use {
val fontExportFile = FileUtils.createFileIfNotExist(configDir, fontName)
fontExportFile.writeBytes(it)
it.copyTo(fontExportFile.outputStream())
exportFiles.add(fontExportFile)
}
}
@ -261,8 +262,10 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
val bgFile = File(ReadBookConfig.durConfig.bgStr)
if (bgFile.exists()) {
val bgExportFile = File(FileUtils.getPath(configDir, bgName))
bgFile.copyTo(bgExportFile)
exportFiles.add(bgExportFile)
if (!bgExportFile.exists()) {
bgFile.copyTo(bgExportFile)
exportFiles.add(bgExportFile)
}
}
}
if (ReadBookConfig.durConfig.bgTypeNight == 2) {
@ -270,8 +273,10 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
val bgFile = File(ReadBookConfig.durConfig.bgStrNight)
if (bgFile.exists()) {
val bgExportFile = File(FileUtils.getPath(configDir, bgName))
bgFile.copyTo(bgExportFile)
exportFiles.add(bgExportFile)
if (!bgExportFile.exists()) {
bgFile.copyTo(bgExportFile)
exportFiles.add(bgExportFile)
}
}
}
if (ReadBookConfig.durConfig.bgTypeEInk == 2) {
@ -279,8 +284,10 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
val bgFile = File(ReadBookConfig.durConfig.bgStrEInk)
if (bgFile.exists()) {
val bgExportFile = File(FileUtils.getPath(configDir, bgName))
bgFile.copyTo(bgExportFile)
exportFiles.add(bgExportFile)
if (!bgExportFile.exists()) {
bgFile.copyTo(bgExportFile)
exportFiles.add(bgExportFile)
}
}
}
val configZipPath = FileUtils.getPath(requireContext().externalCache, configFileName)
@ -288,14 +295,17 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
if (uri.isContentScheme()) {
DocumentFile.fromTreeUri(requireContext(), uri)?.let { treeDoc ->
treeDoc.findFile(exportFileName)?.delete()
treeDoc.createFile("", exportFileName)
?.writeBytes(requireContext(), File(configZipPath).readBytes())
val out = treeDoc.createFile("", exportFileName)?.openOutputStream()
out?.use {
File(configZipPath).inputStream().use {
it.copyTo(out)
}
}
}
} else {
val exportPath = FileUtils.getPath(File(uri.path!!), exportFileName)
FileUtils.delete(exportPath)
FileUtils.createFileIfNotExist(exportPath)
.writeBytes(File(configZipPath).readBytes())
File(configZipPath).copyTo(FileUtils.createFileIfNotExist(exportPath))
}
}
}.onSuccess {

View File

@ -212,7 +212,7 @@ fun FileDoc.createFileIfNotExist(
vararg subDirs: String
): FileDoc {
return if (uri.isContentScheme()) {
val documentFile = DocumentFile.fromTreeUri(appCtx, uri)!!
val documentFile = asDocumentFile()!!
val tmp = DocumentUtils.createFileIfNotExist(documentFile, fileName, *subDirs)!!
FileDoc.fromDocumentFile(tmp)
} else {
@ -226,7 +226,7 @@ fun FileDoc.createFolderIfNotExist(
vararg subDirs: String
): FileDoc {
return if (uri.isContentScheme()) {
val documentFile = DocumentFile.fromTreeUri(appCtx, uri)!!
val documentFile = asDocumentFile()!!
val tmp = DocumentUtils.createFolderIfNotExist(documentFile, *subDirs)!!
FileDoc.fromDocumentFile(tmp)
} else {
@ -257,7 +257,7 @@ fun FileDoc.exists(
vararg subDirs: String
): Boolean {
return if (uri.isContentScheme()) {
DocumentUtils.exists(DocumentFile.fromTreeUri(appCtx, uri)!!, fileName, *subDirs)
DocumentUtils.exists(asDocumentFile()!!, fileName, *subDirs)
} else {
val path = FileUtils.getPath(uri.path!!, *subDirs) + File.separator + fileName
FileUtils.exist(path)
@ -266,7 +266,7 @@ fun FileDoc.exists(
fun FileDoc.exists(): Boolean {
return if (uri.isContentScheme()) {
DocumentFile.fromTreeUri(appCtx, uri)!!.exists()
asDocumentFile()!!.exists()
} else {
FileUtils.exist(uri.path!!)
}