|
|
@ -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(), "") |
|
|
|
} |
|
|
|
} |
|
|
|