diff --git a/app/src/main/java/io/legado/app/help/coroutine/OrderCoroutine.kt b/app/src/main/java/io/legado/app/help/coroutine/OrderCoroutine.kt index 3b0989588..315da7c34 100644 --- a/app/src/main/java/io/legado/app/help/coroutine/OrderCoroutine.kt +++ b/app/src/main/java/io/legado/app/help/coroutine/OrderCoroutine.kt @@ -2,30 +2,24 @@ package io.legado.app.help.coroutine import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.sync.withLock import java.util.concurrent.ConcurrentHashMap -import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.PriorityBlockingQueue +import java.util.concurrent.atomic.AtomicInteger class OrderCoroutine(val threadCount: Int) { - private val taskList = ConcurrentLinkedQueue T>() + private val taskList = ArrayList T>() private val taskResultMap = ConcurrentHashMap() private val finishTaskIndex = PriorityBlockingQueue() - private val mutex = Mutex() private suspend fun start() = coroutineScope { - var taskIndex = 0 + val taskIndex = AtomicInteger(0) + val tasks = taskList.toList() for (i in 1..threadCount) { launch { while (true) { ensureActive() - val task: suspend CoroutineScope.() -> T - val curIndex: Int - mutex.withLock { - task = taskList.poll() ?: return@launch - curIndex = taskIndex++ - } + val curIndex = taskIndex.getAndIncrement() + val task = tasks.getOrNull(curIndex) ?: return@launch taskResultMap[curIndex] = task.invoke(this) finishTaskIndex.add(curIndex) } diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt index 068116e60..c50b73bfc 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt @@ -29,7 +29,11 @@ class GroupEditDialog() : BaseDialogFragment(R.layout.dialog_book_group_edit) { val selectImage = registerForActivityResult(SelectImageContract()) { readUri(it?.uri) { fileDoc, inputStream -> var file = requireContext().externalFiles - file = FileUtils.createFileIfNotExist(file, "covers", fileDoc.name) + val suffix = fileDoc.name.substringAfterLast(".") + val fileName = it.uri!!.inputStream(requireContext())!!.use { + MD5Utils.md5Encode(it) + ".$suffix" + } + file = FileUtils.createFileIfNotExist(file, "covers", fileName) FileOutputStream(file).use { outputStream -> inputStream.copyTo(outputStream) } diff --git a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt index ad31c4bcc..c48954998 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt @@ -118,7 +118,11 @@ class BookInfoEditActivity : readUri(uri) { fileDoc, inputStream -> inputStream.use { var file = this.externalFiles - file = FileUtils.createFileIfNotExist(file, "covers", fileDoc.name) + val suffix = fileDoc.name.substringAfterLast(".") + val fileName = uri.inputStream(this)!!.use { + MD5Utils.md5Encode(it) + ".$suffix" + } + file = FileUtils.createFileIfNotExist(file, "covers", fileName) FileOutputStream(file).use { outputStream -> inputStream.copyTo(outputStream) } diff --git a/app/src/main/java/io/legado/app/ui/config/CoverConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/CoverConfigFragment.kt index 6931ed155..1866b5edb 100644 --- a/app/src/main/java/io/legado/app/ui/config/CoverConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/CoverConfigFragment.kt @@ -140,7 +140,11 @@ class CoverConfigFragment : PreferenceFragment(), private fun setCoverFromUri(preferenceKey: String, uri: Uri) { readUri(uri) { fileDoc, inputStream -> var file = requireContext().externalFiles - file = FileUtils.createFileIfNotExist(file, "covers", fileDoc.name) + val suffix = fileDoc.name.substringAfterLast(".") + val fileName = uri.inputStream(requireContext())!!.use { + MD5Utils.md5Encode(it) + ".$suffix" + } + file = FileUtils.createFileIfNotExist(file, "covers", fileName) FileOutputStream(file).use { inputStream.copyTo(it) } diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index 5863fa600..a38f71d5c 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -304,7 +304,11 @@ class ThemeConfigFragment : PreferenceFragment(), private fun setBgFromUri(uri: Uri, preferenceKey: String, success: () -> Unit) { readUri(uri) { fileDoc, inputStream -> var file = requireContext().externalFiles - file = FileUtils.createFileIfNotExist(file, preferenceKey, fileDoc.name) + val suffix = fileDoc.name.substringAfterLast(".") + val fileName = uri.inputStream(requireContext())!!.use { + MD5Utils.md5Encode(it) + ".$suffix" + } + file = FileUtils.createFileIfNotExist(file, preferenceKey, fileName) FileOutputStream(file).use { inputStream.copyTo(it) } diff --git a/app/src/main/java/io/legado/app/ui/config/WelcomeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/WelcomeConfigFragment.kt index 33b1b1689..680890c9b 100644 --- a/app/src/main/java/io/legado/app/ui/config/WelcomeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/WelcomeConfigFragment.kt @@ -120,7 +120,11 @@ class WelcomeConfigFragment : PreferenceFragment(), private fun setCoverFromUri(preferenceKey: String, uri: Uri) { readUri(uri) { fileDoc, inputStream -> var file = requireContext().externalFiles - file = FileUtils.createFileIfNotExist(file, "covers", fileDoc.name) + val suffix = fileDoc.name.substringAfterLast(".") + val fileName = uri.inputStream(requireContext())!!.use { + MD5Utils.md5Encode(it) + ".$suffix" + } + file = FileUtils.createFileIfNotExist(file, "covers", fileName) FileOutputStream(file).use { inputStream.copyTo(it) } diff --git a/app/src/main/java/io/legado/app/utils/UriExtensions.kt b/app/src/main/java/io/legado/app/utils/UriExtensions.kt index db2d62f79..aea7beff2 100644 --- a/app/src/main/java/io/legado/app/utils/UriExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/UriExtensions.kt @@ -53,6 +53,9 @@ fun AppCompatActivity.readUri( } catch (e: Exception) { e.printOnDebug() toastOnUi(e.localizedMessage ?: "read uri error") + if (e is SecurityException) { + throw e + } } }