瀏覽代碼

Add `ByteReadChannel.toExternalImage`

Him188 6 年之前
父節點
當前提交
e24617fc64
共有 1 個文件被更改,包括 13 次插入0 次删除
  1. 13 0
      mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/ExternalImageJvm.kt

+ 13 - 0
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/ExternalImageJvm.kt

@@ -11,6 +11,7 @@
 
 package net.mamoe.mirai.utils
 
+import io.ktor.utils.io.ByteReadChannel
 import kotlinx.coroutines.Dispatchers.IO
 import kotlinx.coroutines.withContext
 import kotlinx.io.core.Input
@@ -137,3 +138,15 @@ fun Input.toExternalImage(): ExternalImage {
  * 在 [IO] 中进行 [Input.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()
+}