This commit is contained in:
Horis
2025-02-23 14:47:47 +08:00
parent f6bd4a1704
commit 688f8f4338
3 changed files with 20 additions and 15 deletions
@@ -48,7 +48,6 @@ import io.legado.app.utils.flowWithLifecycleAndDatabaseChange
import io.legado.app.utils.iconItemOnLongClick import io.legado.app.utils.iconItemOnLongClick
import io.legado.app.utils.isContentScheme import io.legado.app.utils.isContentScheme
import io.legado.app.utils.observeEvent import io.legado.app.utils.observeEvent
import io.legado.app.utils.parseToUri
import io.legado.app.utils.showDialogFragment import io.legado.app.utils.showDialogFragment
import io.legado.app.utils.startService import io.legado.app.utils.startService
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
@@ -184,6 +183,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
CacheBook.stop(this@CacheActivity) CacheBook.stop(this@CacheActivity)
} }
} }
R.id.menu_download_all -> { R.id.menu_download_all -> {
if (!CacheBook.isRun) { if (!CacheBook.isRun) {
adapter.getItems().forEach { book -> adapter.getItems().forEach { book ->
@@ -321,9 +321,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
override fun export(position: Int) { override fun export(position: Int) {
val path = ACache.get().getAsString(exportBookPathKey) val path = ACache.get().getAsString(exportBookPathKey)
if (path.isNullOrEmpty()) { if (path.isNullOrEmpty() || !FileDoc.fromDir(path).checkWrite()) {
selectExportFolder(position)
} else if (FileDoc.fromUri(path.parseToUri(), true).checkWrite() != true) {
selectExportFolder(position) selectExportFolder(position)
} else if (enableCustomExport()) {// 启用自定义导出 and 导出类型为Epub } else if (enableCustomExport()) {// 启用自定义导出 and 导出类型为Epub
configExportSection(path, position) configExportSection(path, position)
@@ -304,14 +304,14 @@ fun FileDoc.delete() {
asDocumentFile()?.delete() asDocumentFile()?.delete()
} }
fun FileDoc.checkWrite(): Boolean? { fun FileDoc.checkWrite(): Boolean {
if (!isDir) { if (!isDir) {
throw NoStackTraceException("只能检查目录") throw NoStackTraceException("只能检查目录")
} }
asFile()?.let { asFile()?.let {
return it.checkWrite() return it.checkWrite()
} }
return asDocumentFile()?.checkWrite() return asDocumentFile()!!.checkWrite()
} }
/** /**
@@ -358,17 +358,22 @@ fun DocumentFile.readBytes(context: Context): ByteArray {
} }
fun DocumentFile.checkWrite(): Boolean { fun DocumentFile.checkWrite(): Boolean {
var file: DocumentFile? = null
return try { return try {
val filename = System.currentTimeMillis().toString() val filename = System.currentTimeMillis().toString()
createFile(FileUtils.getMimeType(filename), filename)?.let { file = createFile(FileUtils.getMimeType(filename), filename)
it.openOutputStream()?.let { out -> file?.openOutputStream()?.let { out ->
out.use { } out.bufferedWriter().use { it.write(filename) }
it.delete() file.openInputStream()?.let { input ->
return true input.bufferedReader().use {
return it.readText() == filename
}
} }
} }
false false
} catch (e: Exception) { } catch (e: Exception) {
false false
} finally {
file?.delete()
} }
} }
@@ -70,14 +70,16 @@ fun File.createFolderReplace(): File {
} }
fun File.checkWrite(): Boolean { fun File.checkWrite(): Boolean {
var file: File? = null
return try { return try {
val filename = System.currentTimeMillis().toString() val filename = System.currentTimeMillis().toString()
val file = FileUtils.createFileIfNotExist(this, filename) file = FileUtils.createFileIfNotExist(this, filename)
file.outputStream().use { } file.outputStream().bufferedWriter().use { it.write(filename) }
file.delete() file.inputStream().bufferedReader().use { it.readText() == filename }
true
} catch (e: Exception) { } catch (e: Exception) {
false false
} finally {
file?.delete()
} }
} }