Commit e24617fc authored by Him188's avatar Him188

Add `ByteReadChannel.toExternalImage`

parent 82454b0d
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
package net.mamoe.mirai.utils package net.mamoe.mirai.utils
import io.ktor.utils.io.ByteReadChannel
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.io.core.Input import kotlinx.io.core.Input
...@@ -137,3 +138,15 @@ fun Input.toExternalImage(): ExternalImage { ...@@ -137,3 +138,15 @@ fun Input.toExternalImage(): ExternalImage {
* 在 [IO] 中进行 [Input.toExternalImage] * 在 [IO] 中进行 [Input.toExternalImage]
*/ */
suspend inline fun Input.suspendToExternalImage(): ExternalImage = withContext(IO) { toExternalImage() } suspend inline fun Input.suspendToExternalImage(): ExternalImage = withContext(IO) { toExternalImage() }
/**
* 保存为临时文件然后调用 [File.toExternalImage].
*/
suspend fun ByteReadChannel.toExternalImage(): ExternalImage {
val file = createTempFile().apply { deleteOnExit() }
file.outputStream().use {
this.copyTo(it)
}
return file.suspendToExternalImage()
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment