1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-09 02:10:10 +08:00

Fix ReusableInput resources releasing

fix 
This commit is contained in:
Karlatemp 2020-11-14 12:06:47 +08:00
parent 0ebffb735d
commit 525830739c
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
6 changed files with 24 additions and 10 deletions
mirai-core-api/src
mirai-core/src/commonMain/kotlin/contact

View File

@ -16,4 +16,5 @@ internal expect class DeferredReusableInput(input: Any, extraArg: Any?) : Reusab
suspend fun init(strategy: FileCacheStrategy)
}

View File

@ -23,4 +23,6 @@ internal interface ReusableInput {
* Remember to close.
*/
fun asInput(): Input
fun release()
}

View File

@ -57,5 +57,9 @@ internal actual class DeferredReusableInput actual constructor(
return delegate?.asInput() ?: error("DeferredReusableInput not yet initialized")
}
override fun release() {
return delegate?.release() ?: error("DeferredReusableInput not yet initialized")
}
actual val initialized: Boolean get() = delegate != null
}

View File

@ -51,6 +51,10 @@ internal actual fun ByteArray.asReusableInput(): ReusableInput {
override fun asInput(): Input {
return ByteArrayInputStream(this@asReusableInput).asInput()
}
override fun release() {
// nothing to do
}
}
}
@ -69,7 +73,6 @@ internal fun File.asReusableInput(deleteOnClose: Boolean): ReusableInput {
override fun close() {
stream.close()
if (deleteOnClose) this@asReusableInput.delete()
}
}
}
@ -81,6 +84,11 @@ internal fun File.asReusableInput(deleteOnClose: Boolean): ReusableInput {
override fun asInput(): Input {
return inputStream().asInput()
}
override fun release() {
if (deleteOnClose) this@asReusableInput.delete()
}
}
}
@ -99,7 +107,6 @@ internal fun File.asReusableInput(deleteOnClose: Boolean, md5: ByteArray): Reusa
override fun close() {
stream.close()
if (deleteOnClose) this@asReusableInput.delete()
}
}
}
@ -111,6 +118,10 @@ internal fun File.asReusableInput(deleteOnClose: Boolean, md5: ByteArray): Reusa
override fun asInput(): Input {
return inputStream().asInput()
}
override fun release() {
if (deleteOnClose) this@asReusableInput.delete()
}
}
}

View File

@ -22,7 +22,6 @@ import kotlinx.atomicfu.AtomicInt
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.io.core.Closeable
import net.mamoe.mirai.LowLevelApi
import net.mamoe.mirai.contact.Friend
import net.mamoe.mirai.data.FriendInfo
@ -110,8 +109,8 @@ internal class FriendImpl(
}
@JvmSynthetic
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
override suspend fun uploadImage(image: ExternalImage): Image = try {
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
if (image.input is net.mamoe.mirai.utils.internal.DeferredReusableInput) {
image.input.init(bot.configuration.fileCacheStrategy)
}
@ -178,7 +177,6 @@ internal class FriendImpl(
}
}
} finally {
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
(image.input as? Closeable)?.close()
image.input.release()
}
}

View File

@ -15,7 +15,6 @@ package net.mamoe.mirai.internal.contact
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.io.core.Closeable
import net.mamoe.mirai.LowLevelApi
import net.mamoe.mirai.Mirai
import net.mamoe.mirai.contact.*
@ -403,11 +402,10 @@ internal class GroupImpl(
return result.getOrThrow()
}
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@OptIn(ExperimentalTime::class)
@JvmSynthetic
override suspend fun uploadImage(image: ExternalImage): Image = try {
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
if (image.input is net.mamoe.mirai.utils.internal.DeferredReusableInput) {
image.input.init(bot.configuration.fileCacheStrategy)
}
@ -451,7 +449,7 @@ internal class GroupImpl(
}
}
} finally {
(image.input as? Closeable)?.close()
image.input.release()
}
/**