pull/241/head
kunfei 5 years ago
parent 09bbad40ed
commit 5e2f4ad06b
  1. 94
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 5
      app/src/main/java/io/legado/app/utils/DocumentUtils.kt
  3. 18
      app/src/main/java/io/legado/app/utils/FileUtils.kt

@ -52,67 +52,101 @@ object BookHelp {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let { DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let {
DocumentUtils.createFileIfNotExist( DocumentUtils.createFileIfNotExist(
it, it,
bookChapterName(bookChapter), "${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book)) subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
) )?.uri?.writeText(App.INSTANCE, content)
} }
} else { } else {
FileUtils.getFolder(getBookFolder(book)).listFiles()?.forEach { FileUtils.createFileIfNotExist(
File(downloadPath),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
).listFiles()?.forEach {
if (it.name.startsWith(String.format("%05d", bookChapter.index))) { if (it.name.startsWith(String.format("%05d", bookChapter.index))) {
it.delete() it.delete()
return@forEach return@forEach
} }
} }
val filePath = getChapterPath(book, bookChapter) FileUtils.createFileIfNotExist(
val file = FileUtils.getFile(filePath) File(downloadPath),
file.writeText(content) "${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
).writeText(content)
} }
} }
fun getChapterCount(book: Book): Int { fun getChapterCount(book: Book): Int {
return FileUtils.getFolder(getBookFolder(book)).list()?.size ?: 0 if (downloadUri.isDocumentUri(App.INSTANCE)) {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let {
return DocumentUtils.createFileIfNotExist(
it,
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)?.listFiles()?.size ?: 0
}
} else {
return FileUtils.createFileIfNotExist(
File(downloadPath),
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
).list()?.size ?: 0
}
return 0
} }
fun hasContent(book: Book, bookChapter: BookChapter): Boolean { fun hasContent(book: Book, bookChapter: BookChapter): Boolean {
val filePath = getChapterPath(book, bookChapter) if (downloadUri.isDocumentUri(App.INSTANCE)) {
runCatching { DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let {
val file = File(filePath) return DocumentUtils.exists(
if (file.exists()) { it,
return true "${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)
} }
} else {
return FileUtils.exists(
File(downloadPath),
"${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)
} }
return false return false
} }
fun getContent(book: Book, bookChapter: BookChapter): String? { fun getContent(book: Book, bookChapter: BookChapter): String? {
val filePath = getChapterPath(book, bookChapter) if (downloadUri.isDocumentUri(App.INSTANCE)) {
runCatching { DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let {
val file = File(filePath) return DocumentUtils.createFileIfNotExist(
if (file.exists()) { it,
return file.readText() "${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)?.uri?.readText(App.INSTANCE)
} }
} else {
return FileUtils.createFileIfNotExist(
File(downloadPath),
"${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
).readText()
} }
return null return null
} }
fun delContent(book: Book, bookChapter: BookChapter) { fun delContent(book: Book, bookChapter: BookChapter) {
val filePath = getChapterPath(book, bookChapter) if (downloadUri.isDocumentUri(App.INSTANCE)) {
kotlin.runCatching { DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let {
val file = File(filePath) DocumentUtils.createFileIfNotExist(
if (file.exists()) { it,
file.delete() "${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)?.delete()
} }
} else {
FileUtils.createFileIfNotExist(
File(downloadPath),
"${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
).delete()
} }
} }
private fun getBookFolder(book: Book): String {
return "${getBookCachePath()}${File.separator}${bookFolderName(book)}"
}
private fun getChapterPath(book: Book, bookChapter: BookChapter): String {
return "${getBookFolder(book)}${File.separator}${bookChapterName(bookChapter)}.nb"
}
private fun formatFolderName(folderName: String): String { private fun formatFolderName(folderName: String): String {
return folderName.replace("[\\\\/:*?\"<>|.]".toRegex(), "") return folderName.replace("[\\\\/:*?\"<>|.]".toRegex(), "")
} }

@ -6,6 +6,11 @@ import androidx.documentfile.provider.DocumentFile
object DocumentUtils { object DocumentUtils {
fun exists(root: DocumentFile, fileName: String, vararg subDirs: String): Boolean {
val parent = getDirDocument(root, *subDirs) ?: return false
return parent.findFile(fileName)?.exists() ?: false
}
fun createFileIfNotExist( fun createFileIfNotExist(
root: DocumentFile, root: DocumentFile,
fileName: String, fileName: String,

@ -13,12 +13,28 @@ import java.io.IOException
object FileUtils { object FileUtils {
fun exists(file: File, fileName: String, vararg subDirs: String): Boolean {
val filePath =
file.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName
return File(filePath).exists()
}
fun createFileIfNotExist(file: File, fileName: String, vararg subDirs: String): File {
val filePath =
file.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName
return getFile(filePath)
}
fun createFileIfNotExist(file: File, vararg subDirs: String): File {
val filePath = file.absolutePath + File.separator + subDirs.joinToString(File.separator)
return getFolder(filePath)
}
fun getCachePath(): String { fun getCachePath(): String {
return App.INSTANCE.externalCacheDir?.absolutePath return App.INSTANCE.externalCacheDir?.absolutePath
?: App.INSTANCE.cacheDir.absolutePath ?: App.INSTANCE.cacheDir.absolutePath
} }
//获取文件夹 //获取文件夹
fun getFolder(filePath: String): File { fun getFolder(filePath: String): File {
val file = File(filePath) val file = File(filePath)

Loading…
Cancel
Save