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,65 +52,99 @@ object BookHelp {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let {
DocumentUtils.createFileIfNotExist(
it,
bookChapterName(bookChapter),
"${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)
)?.uri?.writeText(App.INSTANCE, content)
}
} 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))) {
it.delete()
return@forEach
}
}
val filePath = getChapterPath(book, bookChapter)
val file = FileUtils.getFile(filePath)
file.writeText(content)
FileUtils.createFileIfNotExist(
File(downloadPath),
"${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
).writeText(content)
}
}
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 {
val filePath = getChapterPath(book, bookChapter)
runCatching {
val file = File(filePath)
if (file.exists()) {
return true
if (downloadUri.isDocumentUri(App.INSTANCE)) {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let {
return DocumentUtils.exists(
it,
"${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)
}
} else {
return FileUtils.exists(
File(downloadPath),
"${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)
}
return false
}
fun getContent(book: Book, bookChapter: BookChapter): String? {
val filePath = getChapterPath(book, bookChapter)
runCatching {
val file = File(filePath)
if (file.exists()) {
return file.readText()
if (downloadUri.isDocumentUri(App.INSTANCE)) {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let {
return DocumentUtils.createFileIfNotExist(
it,
"${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
}
fun delContent(book: Book, bookChapter: BookChapter) {
val filePath = getChapterPath(book, bookChapter)
kotlin.runCatching {
val file = File(filePath)
if (file.exists()) {
file.delete()
}
}
if (downloadUri.isDocumentUri(App.INSTANCE)) {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)?.let {
DocumentUtils.createFileIfNotExist(
it,
"${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
)?.delete()
}
private fun getBookFolder(book: Book): String {
return "${getBookCachePath()}${File.separator}${bookFolderName(book)}"
} else {
FileUtils.createFileIfNotExist(
File(downloadPath),
"${bookChapterName(bookChapter)}.nb",
subDirs = *arrayOf(cacheFolderName, bookFolderName(book))
).delete()
}
private fun getChapterPath(book: Book, bookChapter: BookChapter): String {
return "${getBookFolder(book)}${File.separator}${bookChapterName(bookChapter)}.nb"
}
private fun formatFolderName(folderName: String): String {

@ -6,6 +6,11 @@ import androidx.documentfile.provider.DocumentFile
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(
root: DocumentFile,
fileName: String,

@ -13,12 +13,28 @@ import java.io.IOException
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 {
return App.INSTANCE.externalCacheDir?.absolutePath
?: App.INSTANCE.cacheDir.absolutePath
}
//获取文件夹
fun getFolder(filePath: String): File {
val file = File(filePath)

Loading…
Cancel
Save