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