Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Jan 31, 2023
1 parent 7d77d62 commit ae8847c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 24 deletions.
12 changes: 5 additions & 7 deletions app/src/main/java/io/legado/app/help/JsExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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("排版配置格式错误")
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/java/io/legado/app/help/storage/Backup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 14 additions & 2 deletions app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/io/legado/app/utils/FileExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("unused")

package io.legado.app.utils

import android.net.Uri
Expand Down Expand Up @@ -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
}

0 comments on commit ae8847c

Please sign in to comment.