mirror of
https://github.com/gedoor/legado.git
synced 2024-07-06 23:47:49 +08:00
优化
This commit is contained in:
parent
7d77d62903
commit
ae8847cb4c
@ -220,12 +220,11 @@ interface JsExtensions : JsEncodeUtils {
|
||||
val analyzeUrl = AnalyzeUrl(url, source = getSource())
|
||||
val type = analyzeUrl.type ?: "zip"
|
||||
val path = FileUtils.getPath(
|
||||
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
|
||||
File(FileUtils.getCachePath()),
|
||||
"${MD5Utils.md5Encode16(url)}.${type}"
|
||||
)
|
||||
FileUtils.delete(path)
|
||||
val file = File(path).createFileReplace()
|
||||
analyzeUrl.getInputStream().use { iStream ->
|
||||
val file = FileUtils.createFileIfNotExist(path)
|
||||
FileOutputStream(file).use { oStream ->
|
||||
iStream.copyTo(oStream)
|
||||
}
|
||||
@ -246,8 +245,8 @@ interface JsExtensions : JsEncodeUtils {
|
||||
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
|
||||
"${MD5Utils.md5Encode16(url)}.${type}"
|
||||
)
|
||||
FileUtils.delete(path)
|
||||
val file = FileUtils.createFileIfNotExist(path)
|
||||
val file = File(path)
|
||||
file.createFileReplace()
|
||||
HexUtil.decodeHex(content).let {
|
||||
if (it.isNotEmpty()) {
|
||||
file.writeBytes(it)
|
||||
@ -492,9 +491,8 @@ interface JsExtensions : JsEncodeUtils {
|
||||
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
|
||||
FileUtils.getNameExcludeExtension(zipPath)
|
||||
)
|
||||
FileUtils.delete(unzipPath)
|
||||
val unzipFolder = File(unzipPath).createFolderReplace()
|
||||
val zipFile = getFile(zipPath)
|
||||
val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath)
|
||||
ZipUtils.unzipFile(zipFile, unzipFolder)
|
||||
FileUtils.delete(zipFile.absolutePath)
|
||||
return unzipPath.substring(FileUtils.getCachePath().length)
|
||||
|
@ -386,11 +386,10 @@ object ReadBookConfig {
|
||||
FileUtils.delete(configZipPath)
|
||||
val zipFile = FileUtils.createFileIfNotExist(configZipPath)
|
||||
zipFile.writeBytes(byteArray)
|
||||
val configDirPath = FileUtils.getPath(appCtx.externalCache, "readConfig")
|
||||
FileUtils.delete(configDirPath)
|
||||
val configDir = appCtx.externalCache.getFile("readConfig")
|
||||
configDir.createFolderReplace()
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
ZipUtils.unzipFile(zipFile, FileUtils.createFolderIfNotExist(configDirPath))
|
||||
val configDir = FileUtils.createFolderIfNotExist(configDirPath)
|
||||
ZipUtils.unzipFile(zipFile, configDir)
|
||||
val configFile = configDir.getFile(configFileName)
|
||||
val config: Config = GSON.fromJsonObject<Config>(configFile.readText()).getOrThrow()
|
||||
?: throw NoStackTraceException("排版配置格式错误")
|
||||
|
@ -27,9 +27,7 @@ import java.util.concurrent.TimeUnit
|
||||
object Backup {
|
||||
|
||||
val backupPath: String by lazy {
|
||||
val path = appCtx.filesDir.getFile("backup").absolutePath
|
||||
FileUtils.createFolderIfNotExist(path)
|
||||
path
|
||||
appCtx.filesDir.getFile("backup").createFolderIfNotExist().absolutePath
|
||||
}
|
||||
|
||||
val backupFileNames by lazy {
|
||||
|
@ -93,6 +93,18 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
||||
}.getOrDefault("${book.name} 作者:${book.getRealAuthor()}")
|
||||
}
|
||||
|
||||
fun exportFileExist(path: String, book: Book): Boolean {
|
||||
val fileName = getExportFileName(book)
|
||||
return if (path.isContentScheme()) {
|
||||
val uri = Uri.parse(path)
|
||||
val doc = DocumentFile.fromTreeUri(context, uri) ?: return false
|
||||
doc.findFile(fileName) ?: return false
|
||||
return true
|
||||
} else {
|
||||
File(path).exists(fileName)
|
||||
}
|
||||
}
|
||||
|
||||
fun export(path: String, book: Book) {
|
||||
if (exportProgress.contains(book.bookUrl)) return
|
||||
exportProgress[book.bookUrl] = 0
|
||||
@ -111,7 +123,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
||||
?: throw NoStackTraceException("获取导出文档失败")
|
||||
export(doc, book)
|
||||
} else {
|
||||
export(FileUtils.createFolderIfNotExist(path), book)
|
||||
export(File(path).createFolderIfNotExist(), book)
|
||||
}
|
||||
}.onError {
|
||||
exportProgress.remove(book.bookUrl)
|
||||
@ -274,7 +286,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
||||
?: throw NoStackTraceException("获取导出文档失败")
|
||||
exportEpub(doc, book)
|
||||
} else {
|
||||
exportEpub(FileUtils.createFolderIfNotExist(path), book)
|
||||
exportEpub(File(path).createFolderIfNotExist(), book)
|
||||
}
|
||||
}.onError {
|
||||
exportProgress.remove(book.bookUrl)
|
||||
|
@ -234,14 +234,12 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
|
||||
}
|
||||
execute {
|
||||
val exportFiles = arrayListOf<File>()
|
||||
val configDirPath = FileUtils.getPath(requireContext().externalCache, "readConfig")
|
||||
FileUtils.delete(configDirPath)
|
||||
val configDir = FileUtils.createFolderIfNotExist(configDirPath)
|
||||
val configExportPath = FileUtils.getPath(configDir, "readConfig.json")
|
||||
FileUtils.delete(configExportPath)
|
||||
val configExportFile = FileUtils.createFileIfNotExist(configExportPath)
|
||||
configExportFile.writeText(GSON.toJson(ReadBookConfig.getExportConfig()))
|
||||
exportFiles.add(configExportFile)
|
||||
val configDir = requireContext().externalCache.getFile("readConfig")
|
||||
configDir.createFolderReplace()
|
||||
val configFile = configDir.getFile("readConfig.json")
|
||||
configFile.createFileReplace()
|
||||
configFile.writeText(GSON.toJson(ReadBookConfig.getExportConfig()))
|
||||
exportFiles.add(configFile)
|
||||
val fontPath = ReadBookConfig.textFont
|
||||
if (fontPath.isNotEmpty()) {
|
||||
val fontName = FileUtils.getName(fontPath)
|
||||
|
@ -1,3 +1,5 @@
|
||||
@file:Suppress("unused")
|
||||
|
||||
package io.legado.app.utils
|
||||
|
||||
import android.net.Uri
|
||||
@ -31,4 +33,36 @@ fun File.listFileDocs(filter: FileDocFilter? = null): ArrayList<FileDoc> {
|
||||
return docList
|
||||
}
|
||||
|
||||
fun File.createFileIfNotExist(): File {
|
||||
if (!exists()) {
|
||||
parentFile?.createFileIfNotExist()
|
||||
createNewFile()
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
fun File.createFileReplace(): File {
|
||||
if (!exists()) {
|
||||
parentFile?.createFileIfNotExist()
|
||||
createNewFile()
|
||||
} else {
|
||||
delete()
|
||||
createNewFile()
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
fun File.createFolderIfNotExist(): File {
|
||||
if (!exists()) {
|
||||
mkdirs()
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
fun File.createFolderReplace(): File {
|
||||
if (exists()) {
|
||||
FileUtils.delete(this, true)
|
||||
}
|
||||
mkdirs()
|
||||
return this
|
||||
}
|
Loading…
Reference in New Issue
Block a user