pull/328/head
gedoor 4 years ago
parent 8c2afe51d2
commit 37eaf4fa38
  1. 22
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 2
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  3. 4
      app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt
  4. 6
      app/src/main/java/io/legado/app/utils/FileUtils.kt

@ -9,11 +9,7 @@ import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
import io.legado.app.utils.FileUtils import io.legado.app.utils.*
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.externalFilesDir
import io.legado.app.utils.postEvent
import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -44,7 +40,7 @@ object BookHelp {
fun clearCache() { fun clearCache() {
FileUtils.deleteFile( FileUtils.deleteFile(
FileUtils.getPath(downloadDir, subDirs = *arrayOf(cacheFolderName)) FileUtils.getPath(downloadDir, subDirs = arrayOf(cacheFolderName))
) )
} }
@ -73,7 +69,7 @@ object BookHelp {
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
downloadDir, downloadDir,
formatChapterName(bookChapter), formatChapterName(bookChapter),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = arrayOf(cacheFolderName, bookFolderName(book))
).writeText(content) ).writeText(content)
//保存图片 //保存图片
content.split("\n").forEach { content.split("\n").forEach {
@ -95,7 +91,7 @@ object BookHelp {
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
downloadDir, downloadDir,
"${MD5Utils.md5Encode16(src)}${src.substringAfterLast(".").substringBefore(",")}", "${MD5Utils.md5Encode16(src)}${src.substringAfterLast(".").substringBefore(",")}",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book), cacheImageFolderName) subDirs = arrayOf(cacheFolderName, bookFolderName(book), cacheImageFolderName)
).writeBytes(it) ).writeBytes(it)
} }
} }
@ -104,7 +100,7 @@ object BookHelp {
return FileUtils.getFile( return FileUtils.getFile(
downloadDir, downloadDir,
"${MD5Utils.md5Encode16(src)}${src.substringAfterLast(".").substringBefore(",")}", "${MD5Utils.md5Encode16(src)}${src.substringAfterLast(".").substringBefore(",")}",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book), cacheImageFolderName) subDirs = arrayOf(cacheFolderName, bookFolderName(book), cacheImageFolderName)
) )
} }
@ -112,7 +108,7 @@ object BookHelp {
val fileNameList = arrayListOf<String>() val fileNameList = arrayListOf<String>()
FileUtils.createFolderIfNotExist( FileUtils.createFolderIfNotExist(
downloadDir, downloadDir,
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = arrayOf(cacheFolderName, bookFolderName(book))
).list()?.let { ).list()?.let {
fileNameList.addAll(it) fileNameList.addAll(it)
} }
@ -126,7 +122,7 @@ object BookHelp {
FileUtils.exists( FileUtils.exists(
downloadDir, downloadDir,
formatChapterName(bookChapter), formatChapterName(bookChapter),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = arrayOf(cacheFolderName, bookFolderName(book))
) )
} }
} }
@ -138,7 +134,7 @@ object BookHelp {
val file = FileUtils.getFile( val file = FileUtils.getFile(
downloadDir, downloadDir,
formatChapterName(bookChapter), formatChapterName(bookChapter),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = arrayOf(cacheFolderName, bookFolderName(book))
) )
if (file.exists()) { if (file.exists()) {
return file.readText() return file.readText()
@ -154,7 +150,7 @@ object BookHelp {
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
downloadDir, downloadDir,
formatChapterName(bookChapter), formatChapterName(bookChapter),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = arrayOf(cacheFolderName, bookFolderName(book))
).delete() ).delete()
} }
} }

@ -107,7 +107,7 @@ object Backup {
DocumentUtils.createFileIfNotExist( DocumentUtils.createFileIfNotExist(
treeDoc, treeDoc,
fileName, fileName,
subDirs = *arrayOf("auto") subDirs = arrayOf("auto")
)?.writeBytes(context, file.readBytes()) )?.writeBytes(context, file.readBytes())
} else { } else {
treeDoc.findFile(fileName)?.delete() treeDoc.findFile(fileName)?.delete()

@ -248,7 +248,7 @@ class AnalyzeTxtFile {
val rootFile = App.INSTANCE.getExternalFilesDir(null) val rootFile = App.INSTANCE.getExternalFilesDir(null)
?: App.INSTANCE.externalCacheDir ?: App.INSTANCE.externalCacheDir
?: App.INSTANCE.cacheDir ?: App.INSTANCE.cacheDir
FileUtils.createFolderIfNotExist(rootFile, subDirs = *arrayOf(folderName)) FileUtils.createFolderIfNotExist(rootFile, subDirs = arrayOf(folderName))
} }
fun getContent(book: Book, bookChapter: BookChapter): String { fun getContent(book: Book, bookChapter: BookChapter): String {
@ -264,7 +264,7 @@ class AnalyzeTxtFile {
private fun getBookFile(book: Book): File { private fun getBookFile(book: Book): File {
if (book.bookUrl.isContentPath()) { if (book.bookUrl.isContentPath()) {
val uri = Uri.parse(book.bookUrl) val uri = Uri.parse(book.bookUrl)
val bookFile = FileUtils.getFile(cacheFolder, book.originName, subDirs = *arrayOf()) val bookFile = FileUtils.getFile(cacheFolder, book.originName, subDirs = arrayOf())
if (!bookFile.exists()) { if (!bookFile.exists()) {
bookFile.createNewFile() bookFile.createNewFile()
DocumentUtils.readBytes(App.INSTANCE, uri)?.let { DocumentUtils.readBytes(App.INSTANCE, uri)?.let {

@ -9,7 +9,7 @@ import java.io.IOException
object FileUtils { object FileUtils {
fun exists(root: File, fileName: String, vararg subDirs: String): Boolean { fun exists(root: File, fileName: String, vararg subDirs: String): Boolean {
return getFile(root, fileName, subDirs = *subDirs).exists() return getFile(root, fileName, subDirs = subDirs).exists()
} }
fun createFileIfNotExist(root: File, fileName: String, vararg subDirs: String): File { fun createFileIfNotExist(root: File, fileName: String, vararg subDirs: String): File {
@ -18,7 +18,7 @@ object FileUtils {
} }
fun createFolderIfNotExist(root: File, vararg subDirs: String): File { fun createFolderIfNotExist(root: File, vararg subDirs: String): File {
val filePath = getPath(root, subDirs = *subDirs) val filePath = getPath(root, subDirs = subDirs)
return createFolderIfNotExist(filePath) return createFolderIfNotExist(filePath)
} }
@ -55,7 +55,7 @@ object FileUtils {
} }
fun getDirFile(root: File, vararg subDirs: String): File { fun getDirFile(root: File, vararg subDirs: String): File {
val filePath = getPath(root, subDirs = *subDirs) val filePath = getPath(root, subDirs = subDirs)
return File(filePath) return File(filePath)
} }

Loading…
Cancel
Save