pull/83/head
kunfei 5 years ago
parent 15cb3feee4
commit c5f86e0ea3
  1. 12
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 43
      app/src/main/java/io/legado/app/utils/FileUtils.kt

@ -36,18 +36,18 @@ object BookHelp {
return String.format("%05d-%s", bookChapter.index, MD5Utils.md5Encode(bookChapter.title)) return String.format("%05d-%s", bookChapter.index, MD5Utils.md5Encode(bookChapter.title))
} }
private fun getBookCachePath(): String {
return "$downloadPath${File.separator}$cacheFolderName"
}
fun clearCache() { fun clearCache() {
if (downloadUri.isDocumentUri(App.INSTANCE)) { if (downloadUri.isDocumentUri(App.INSTANCE)) {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri) DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)
?.findFile(cacheFolderName) ?.findFile(cacheFolderName)
?.delete() ?.delete()
} else { } else {
FileUtils.deleteFile(getBookCachePath()) FileUtils.deleteFile(
FileUtils.createFolderIfNotExist(getBookCachePath()) FileUtils.getPath(
File(downloadPath),
subDirs = *arrayOf(cacheFolderName)
)
)
} }
} }

@ -13,28 +13,20 @@ import java.io.IOException
object FileUtils { object FileUtils {
fun exists(file: File, fileName: String, vararg subDirs: String): Boolean { fun exists(root: File, fileName: String, vararg subDirs: String): Boolean {
val filePath = return getFile(root, fileName, subDirs = *subDirs).exists()
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 { fun createFileIfNotExist(root: File, fileName: String, vararg subDirs: String): File {
val filePath = val filePath = getPath(root, fileName, *subDirs)
file.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName
return createFileIfNotExist(filePath) return createFileIfNotExist(filePath)
} }
fun createFileIfNotExist(file: File, vararg subDirs: String): File { fun createFileIfNotExist(root: File, vararg subDirs: String): File {
val filePath = file.absolutePath + File.separator + subDirs.joinToString(File.separator) val filePath = root.absolutePath + File.separator + subDirs.joinToString(File.separator)
return createFolderIfNotExist(filePath) return createFolderIfNotExist(filePath)
} }
fun getCachePath(): String {
return App.INSTANCE.externalCacheDir?.absolutePath
?: App.INSTANCE.cacheDir.absolutePath
}
fun createFolderIfNotExist(filePath: String): File { fun createFolderIfNotExist(filePath: String): File {
val file = File(filePath) val file = File(filePath)
//如果文件夹不存在,就创建它 //如果文件夹不存在,就创建它
@ -62,6 +54,24 @@ object FileUtils {
return file return file
} }
fun getFile(file: File, fileName: String, vararg subDirs: String): File {
val filePath = getPath(file, fileName, *subDirs)
return File(filePath)
}
fun getFile(root: File, vararg subDirs: String): File {
val filePath = getPath(root, subDirs = *subDirs)
return File(filePath)
}
fun getPath(root: File, fileName: String? = null, vararg subDirs: String): String {
return if (fileName.isNullOrEmpty()) {
root.absolutePath + File.separator + subDirs.joinToString(File.separator)
} else {
root.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName
}
}
//递归删除文件夹下的数据 //递归删除文件夹下的数据
@Synchronized @Synchronized
fun deleteFile(filePath: String) { fun deleteFile(filePath: String) {
@ -79,6 +89,11 @@ object FileUtils {
file.delete() file.delete()
} }
fun getCachePath(): String {
return App.INSTANCE.externalCacheDir?.absolutePath
?: App.INSTANCE.cacheDir.absolutePath
}
fun getSdCardPath(): String { fun getSdCardPath(): String {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
var sdCardDirectory = Environment.getExternalStorageDirectory().absolutePath var sdCardDirectory = Environment.getExternalStorageDirectory().absolutePath

Loading…
Cancel
Save