pull/1705/head
kunfei 3 years ago
parent f32710001c
commit bd28d402ed
  1. 6
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 8
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  3. 78
      app/src/main/java/io/legado/app/help/RuleBigDataHelp.kt
  4. 8
      app/src/main/java/io/legado/app/help/config/ReadBookConfig.kt
  5. 2
      app/src/main/java/io/legado/app/help/config/ThemeConfig.kt
  6. 2
      app/src/main/java/io/legado/app/help/storage/AppWebDav.kt
  7. 2
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  8. 6
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  9. 2
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  10. 2
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
  11. 6
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  12. 2
      app/src/main/java/io/legado/app/ui/config/OtherConfigFragment.kt
  13. 19
      app/src/main/java/io/legado/app/utils/FileUtils.kt

@ -31,14 +31,14 @@ object BookHelp {
private val downloadImages = CopyOnWriteArraySet<String>() private val downloadImages = CopyOnWriteArraySet<String>()
fun clearCache() { fun clearCache() {
FileUtils.deleteFile( FileUtils.delete(
FileUtils.getPath(downloadDir, cacheFolderName) FileUtils.getPath(downloadDir, cacheFolderName)
) )
} }
fun clearCache(book: Book) { fun clearCache(book: Book) {
val filePath = FileUtils.getPath(downloadDir, cacheFolderName, book.getFolderName()) val filePath = FileUtils.getPath(downloadDir, cacheFolderName, book.getFolderName())
FileUtils.deleteFile(filePath) FileUtils.delete(filePath)
} }
/** /**
@ -53,7 +53,7 @@ object BookHelp {
val file = downloadDir.getFile(cacheFolderName) val file = downloadDir.getFile(cacheFolderName)
file.listFiles()?.forEach { bookFile -> file.listFiles()?.forEach { bookFile ->
if (!bookFolderNames.contains(bookFile.name)) { if (!bookFolderNames.contains(bookFile.name)) {
FileUtils.deleteFile(bookFile.absolutePath) FileUtils.delete(bookFile.absolutePath)
} }
} }
} }

@ -177,7 +177,7 @@ interface JsExtensions {
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
"${MD5Utils.md5Encode16(url)}.${type}" "${MD5Utils.md5Encode16(url)}.${type}"
) )
FileUtils.deleteFile(zipPath) FileUtils.delete(zipPath)
val zipFile = FileUtils.createFileIfNotExist(zipPath) val zipFile = FileUtils.createFileIfNotExist(zipPath)
StringUtils.hexStringToByte(content).let { StringUtils.hexStringToByte(content).let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
@ -363,11 +363,11 @@ interface JsExtensions {
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
FileUtils.getNameExcludeExtension(zipPath) FileUtils.getNameExcludeExtension(zipPath)
) )
FileUtils.deleteFile(unzipPath) FileUtils.delete(unzipPath)
val zipFile = getFile(zipPath) val zipFile = getFile(zipPath)
val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath) val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath)
ZipUtils.unzipFile(zipFile, unzipFolder) ZipUtils.unzipFile(zipFile, unzipFolder)
FileUtils.deleteFile(zipFile.absolutePath) FileUtils.delete(zipFile.absolutePath)
return unzipPath.substring(FileUtils.getCachePath().length) return unzipPath.substring(FileUtils.getCachePath().length)
} }
@ -388,7 +388,7 @@ interface JsExtensions {
contents.deleteCharAt(contents.length - 1) contents.deleteCharAt(contents.length - 1)
} }
} }
FileUtils.deleteFile(unzipFolder.absolutePath) FileUtils.delete(unzipFolder.absolutePath)
return contents.toString() return contents.toString()
} }

@ -1,29 +1,101 @@
package io.legado.app.help package io.legado.app.help
import io.legado.app.utils.FileUtils
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.externalFiles
import splitties.init.appCtx
import java.io.File
object RuleBigDataHelp { object RuleBigDataHelp {
fun putBookVariable(bookUrl: String, key: String, value: String?) { private val ruleDataDir = FileUtils.createFolderIfNotExist(appCtx.externalFiles, "ruleData")
private val bookData = FileUtils.createFolderIfNotExist(ruleDataDir, "book")
private val rssData = FileUtils.createFolderIfNotExist(ruleDataDir, "rss")
fun putBookVariable(bookUrl: String, key: String, value: String?) {
val md5BookUrl = MD5Utils.md5Encode(bookUrl)
val md5Key = MD5Utils.md5Encode(key)
if (value == null) {
FileUtils.delete(FileUtils.getPath(bookData, md5BookUrl, "$md5Key.txt"), true)
} else {
val valueFile = FileUtils.createFileIfNotExist(bookData, md5BookUrl, "$md5Key.txt")
valueFile.writeText(value)
val bookUrlFile = File(FileUtils.getPath(bookData, md5BookUrl, "bookUrl.txt"))
if (!bookUrlFile.exists()) {
bookUrlFile.writeText(bookUrl)
}
}
} }
fun getBookVariable(bookUrl: String, key: String?): String? { fun getBookVariable(bookUrl: String, key: String?): String? {
val md5BookUrl = MD5Utils.md5Encode(bookUrl)
val md5Key = MD5Utils.md5Encode(key)
val file = File(FileUtils.getPath(bookData, md5BookUrl, "$md5Key.txt"))
if (file.exists()) {
return file.readText()
}
return null return null
} }
fun putChapterVariable(bookUrl: String, chapterUrl: String, key: String, value: String?) { fun putChapterVariable(bookUrl: String, chapterUrl: String, key: String, value: String?) {
val md5BookUrl = MD5Utils.md5Encode(bookUrl)
val md5ChapterUrl = MD5Utils.md5Encode(chapterUrl)
val md5Key = MD5Utils.md5Encode(key)
if (value == null) {
FileUtils.delete(FileUtils.getPath(bookData, md5BookUrl, md5ChapterUrl, "$md5Key.txt"))
} else {
val valueFile =
FileUtils.createFileIfNotExist(bookData, md5BookUrl, md5ChapterUrl, "$md5Key.txt")
valueFile.writeText(value)
val bookUrlFile = File(FileUtils.getPath(bookData, md5BookUrl, "bookUrl.txt"))
if (!bookUrlFile.exists()) {
bookUrlFile.writeText(bookUrl)
}
}
} }
fun getChapterVariable(bookUrl: String, chapterUrl: String, key: String): String? { fun getChapterVariable(bookUrl: String, chapterUrl: String, key: String): String? {
val md5BookUrl = MD5Utils.md5Encode(bookUrl)
val md5ChapterUrl = MD5Utils.md5Encode(chapterUrl)
val md5Key = MD5Utils.md5Encode(key)
val file = File(FileUtils.getPath(bookData, md5BookUrl, md5ChapterUrl, "$md5Key.txt"))
if (file.exists()) {
return file.readText()
}
return null return null
} }
fun putRssVariable(origin: String, link: String, key: String, value: String?) { fun putRssVariable(origin: String, link: String, key: String, value: String?) {
val md5Origin = MD5Utils.md5Encode(origin)
val md5Link = MD5Utils.md5Encode(link)
val md5Key = MD5Utils.md5Encode(key)
val filePath = FileUtils.getPath(rssData, md5Origin, md5Link, "$md5Key.txt")
if (value == null) {
FileUtils.delete(filePath)
} else {
val valueFile = FileUtils.createFileIfNotExist(filePath)
valueFile.writeText(value)
val originFile = File(FileUtils.getPath(rssData, md5Origin, "origin.txt"))
if (!originFile.exists()) {
originFile.writeText(origin)
}
val linFile = File(FileUtils.getPath(rssData, md5Origin, md5Link, "origin.txt"))
if (!linFile.exists()) {
linFile.writeText(link)
}
}
} }
fun getRssVariable(origin: String, link: String, key: String): String? { fun getRssVariable(origin: String, link: String, key: String): String? {
val md5Origin = MD5Utils.md5Encode(origin)
val md5Link = MD5Utils.md5Encode(link)
val md5Key = MD5Utils.md5Encode(key)
val filePath = FileUtils.getPath(rssData, md5Origin, md5Link, "$md5Key.txt")
val file = File(filePath)
if (file.exists()) {
return file.readText()
}
return null return null
} }
} }

@ -105,11 +105,11 @@ object ReadBookConfig {
Coroutine.async { Coroutine.async {
synchronized(this) { synchronized(this) {
GSON.toJson(configList).let { GSON.toJson(configList).let {
FileUtils.deleteFile(configFilePath) FileUtils.delete(configFilePath)
FileUtils.createFileIfNotExist(configFilePath).writeText(it) FileUtils.createFileIfNotExist(configFilePath).writeText(it)
} }
GSON.toJson(shareConfig).let { GSON.toJson(shareConfig).let {
FileUtils.deleteFile(shareConfigFilePath) FileUtils.delete(shareConfigFilePath)
FileUtils.createFileIfNotExist(shareConfigFilePath).writeText(it) FileUtils.createFileIfNotExist(shareConfigFilePath).writeText(it)
} }
} }
@ -370,11 +370,11 @@ object ReadBookConfig {
return kotlin.runCatching { return kotlin.runCatching {
withContext(IO) { withContext(IO) {
val configZipPath = FileUtils.getPath(appCtx.externalCache, "readConfig.zip") val configZipPath = FileUtils.getPath(appCtx.externalCache, "readConfig.zip")
FileUtils.deleteFile(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 configDirPath = FileUtils.getPath(appCtx.externalCache, "readConfig")
FileUtils.deleteFile(configDirPath) FileUtils.delete(configDirPath)
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
ZipUtils.unzipFile(zipFile, FileUtils.createFolderIfNotExist(configDirPath)) ZipUtils.unzipFile(zipFile, FileUtils.createFolderIfNotExist(configDirPath))
val configDir = FileUtils.createFolderIfNotExist(configDirPath) val configDir = FileUtils.createFolderIfNotExist(configDirPath)

@ -74,7 +74,7 @@ object ThemeConfig {
fun save() { fun save() {
val json = GSON.toJson(configList) val json = GSON.toJson(configList)
FileUtils.deleteFile(configFilePath) FileUtils.delete(configFilePath)
FileUtils.createFileIfNotExist(configFilePath).writeText(json) FileUtils.createFileIfNotExist(configFilePath).writeText(json)
} }

@ -124,7 +124,7 @@ object AppWebDav {
for (i in 0 until paths.size) { for (i in 0 until paths.size) {
paths[i] = path + File.separator + paths[i] paths[i] = path + File.separator + paths[i]
} }
FileUtils.deleteFile(zipFilePath) FileUtils.delete(zipFilePath)
if (ZipUtils.zipFiles(paths, zipFilePath)) { if (ZipUtils.zipFiles(paths, zipFilePath)) {
val putUrl = "${rootWebDavUrl}${backupFileName}" val putUrl = "${rootWebDavUrl}${backupFileName}"
WebDav(putUrl).upload(zipFilePath) WebDav(putUrl).upload(zipFilePath)

@ -66,7 +66,7 @@ object Backup {
suspend fun backup(context: Context, path: String?, isAuto: Boolean = false) { suspend fun backup(context: Context, path: String?, isAuto: Boolean = false) {
context.putPrefLong(PreferKey.lastBackup, System.currentTimeMillis()) context.putPrefLong(PreferKey.lastBackup, System.currentTimeMillis())
withContext(IO) { withContext(IO) {
FileUtils.deleteFile(backupPath) FileUtils.delete(backupPath)
writeListToJson(appDb.bookDao.all, "bookshelf.json", backupPath) writeListToJson(appDb.bookDao.all, "bookshelf.json", backupPath)
writeListToJson(appDb.bookmarkDao.all, "bookmark.json", backupPath) writeListToJson(appDb.bookmarkDao.all, "bookmark.json", backupPath)
writeListToJson(appDb.bookGroupDao.all, "bookGroup.json", backupPath) writeListToJson(appDb.bookGroupDao.all, "bookGroup.json", backupPath)

@ -124,7 +124,7 @@ object Restore {
val file = val file =
FileUtils.createFileIfNotExist("$path${File.separator}${ThemeConfig.configFileName}") FileUtils.createFileIfNotExist("$path${File.separator}${ThemeConfig.configFileName}")
if (file.exists()) { if (file.exists()) {
FileUtils.deleteFile(ThemeConfig.configFilePath) FileUtils.delete(ThemeConfig.configFilePath)
file.copyTo(File(ThemeConfig.configFilePath)) file.copyTo(File(ThemeConfig.configFilePath))
ThemeConfig.upConfig() ThemeConfig.upConfig()
} }
@ -137,7 +137,7 @@ object Restore {
val file = val file =
FileUtils.createFileIfNotExist("$path${File.separator}${ReadBookConfig.configFileName}") FileUtils.createFileIfNotExist("$path${File.separator}${ReadBookConfig.configFileName}")
if (file.exists()) { if (file.exists()) {
FileUtils.deleteFile(ReadBookConfig.configFilePath) FileUtils.delete(ReadBookConfig.configFilePath)
file.copyTo(File(ReadBookConfig.configFilePath)) file.copyTo(File(ReadBookConfig.configFilePath))
ReadBookConfig.initConfigs() ReadBookConfig.initConfigs()
} }
@ -148,7 +148,7 @@ object Restore {
val file = val file =
FileUtils.createFileIfNotExist("$path${File.separator}${ReadBookConfig.shareConfigFileName}") FileUtils.createFileIfNotExist("$path${File.separator}${ReadBookConfig.shareConfigFileName}")
if (file.exists()) { if (file.exists()) {
FileUtils.deleteFile(ReadBookConfig.shareConfigFilePath) FileUtils.delete(ReadBookConfig.shareConfigFilePath)
file.copyTo(File(ReadBookConfig.shareConfigFilePath)) file.copyTo(File(ReadBookConfig.shareConfigFilePath))
ReadBookConfig.initShareConfig() ReadBookConfig.initShareConfig()
} }

@ -167,7 +167,7 @@ object LocalBook {
val uri = Uri.parse(book.bookUrl) val uri = Uri.parse(book.bookUrl)
DocumentFile.fromSingleUri(appCtx, uri)?.delete() DocumentFile.fromSingleUri(appCtx, uri)?.delete()
} else { } else {
FileUtils.deleteFile(book.bookUrl) FileUtils.delete(book.bookUrl)
} }
} }
} }

@ -264,7 +264,7 @@ class HttpReadAloudService : BaseReadAloudService(),
val titleMd5 = MD5Utils.md5Encode16(textChapter?.title ?: "") val titleMd5 = MD5Utils.md5Encode16(textChapter?.title ?: "")
FileUtils.listDirsAndFiles(ttsFolderPath)?.forEach { FileUtils.listDirsAndFiles(ttsFolderPath)?.forEach {
if (!it.name.startsWith(titleMd5) && Date().time - it.lastModified() > 600000) { if (!it.name.startsWith(titleMd5) && Date().time - it.lastModified() > 600000) {
FileUtils.deleteFile(it.absolutePath) FileUtils.delete(it.absolutePath)
} }
} }
} }

@ -231,10 +231,10 @@ 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 configDirPath = FileUtils.getPath(requireContext().externalCache, "readConfig")
FileUtils.deleteFile(configDirPath) FileUtils.delete(configDirPath)
val configDir = FileUtils.createFolderIfNotExist(configDirPath) val configDir = FileUtils.createFolderIfNotExist(configDirPath)
val configExportPath = FileUtils.getPath(configDir, "readConfig.json") val configExportPath = FileUtils.getPath(configDir, "readConfig.json")
FileUtils.deleteFile(configExportPath) FileUtils.delete(configExportPath)
val configExportFile = FileUtils.createFileIfNotExist(configExportPath) val configExportFile = FileUtils.createFileIfNotExist(configExportPath)
configExportFile.writeText(GSON.toJson(ReadBookConfig.getExportConfig())) configExportFile.writeText(GSON.toJson(ReadBookConfig.getExportConfig()))
exportFiles.add(configExportFile) exportFiles.add(configExportFile)
@ -285,7 +285,7 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
} }
} else { } else {
val exportPath = FileUtils.getPath(File(uri.path!!), exportFileName) val exportPath = FileUtils.getPath(File(uri.path!!), exportFileName)
FileUtils.deleteFile(exportPath) FileUtils.delete(exportPath)
FileUtils.createFileIfNotExist(exportPath) FileUtils.createFileIfNotExist(exportPath)
.writeBytes(File(configZipPath).readBytes()) .writeBytes(File(configZipPath).readBytes())
} }

@ -186,7 +186,7 @@ class OtherConfigFragment : BasePreferenceFragment(),
) { ) {
okButton { okButton {
BookHelp.clearCache() BookHelp.clearCache()
FileUtils.deleteFile(requireActivity().cacheDir.absolutePath) FileUtils.delete(requireActivity().cacheDir.absolutePath)
toastOnUi(R.string.clear_cache_success) toastOnUi(R.string.clear_cache_success)
} }
noButton() noButton()

@ -90,23 +90,6 @@ object FileUtils {
return path.toString() return path.toString()
} }
//递归删除文件夹下的数据
@Synchronized
fun deleteFile(filePath: String) {
val file = File(filePath)
if (!file.exists()) return
if (file.isDirectory) {
val files = file.listFiles()
files?.forEach { subFile ->
val path = subFile.path
deleteFile(path)
}
}
//删除文件
file.delete()
}
fun getCachePath(): String { fun getCachePath(): String {
return appCtx.externalCache.absolutePath return appCtx.externalCache.absolutePath
} }
@ -360,7 +343,7 @@ object FileUtils {
* 删除文件或目录 * 删除文件或目录
*/ */
@JvmOverloads @JvmOverloads
fun delete(path: String, deleteRootDir: Boolean = false): Boolean { fun delete(path: String, deleteRootDir: Boolean = true): Boolean {
val file = File(path) val file = File(path)
return if (file.exists()) { return if (file.exists()) {

Loading…
Cancel
Save