ソースを参照

Use url with coroutines

ryoii 2 年 前
コミット
5d3d8e0c3e

+ 3 - 3
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/message.kt

@@ -19,7 +19,7 @@ import net.mamoe.mirai.api.http.adapter.internal.action.*
 import net.mamoe.mirai.api.http.adapter.internal.consts.Paths
 import net.mamoe.mirai.api.http.adapter.internal.dto.EventListRestfulResult
 import net.mamoe.mirai.api.http.adapter.internal.dto.IntRestfulResult
-import java.net.URL
+import net.mamoe.mirai.api.http.util.openAsStream
 
 /**
  * 消息路由
@@ -108,7 +108,7 @@ internal fun Application.messageRouter() = routing {
         val type = parts.value("type")
         val url = parts.valueOrNull("url")
         val stream = if (url != null) {
-            URL(url).openStream()
+            url.openAsStream()
         } else {
             val f = parts.file("img") ?: throw IllegalParamException("缺少参数 img")
             f.streamProvider()
@@ -125,7 +125,7 @@ internal fun Application.messageRouter() = routing {
         val type = parts.value("type")
         val url = parts.valueOrNull("url")
         val stream = if (url != null) {
-            URL(url).openStream()
+            url.openAsStream()
         } else {
             val f = parts.file("voice") ?: throw IllegalParamException("缺少参数 voice")
             f.streamProvider()

+ 0 - 21
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/util/closable.kt

@@ -1,21 +0,0 @@
-/*
- * Copyright 2020 Mamoe Technologies and contributors.
- *
- * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
- * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
- *
- * https://github.com/mamoe/mirai/blob/master/LICENSE
- */
-
-package net.mamoe.mirai.api.http.util
-
-import net.mamoe.mirai.utils.ExternalResource
-import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
-import java.io.InputStream
-import java.net.URL
-
-internal inline fun <R> String.useUrl(consumer: (ExternalResource) -> R)
-    = URL(this).openStream().useStream(consumer)
-
-internal inline fun <R> InputStream.useStream(consumer: (ExternalResource) -> R)
-    = use { toExternalResource().use(consumer) }

+ 36 - 0
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/util/http.kt

@@ -0,0 +1,36 @@
+/*
+ * Copyright 2023 Mamoe Technologies and contributors.
+ *
+ * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
+ * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
+ *
+ * https://github.com/mamoe/mirai/blob/master/LICENSE
+ */
+
+package net.mamoe.mirai.api.http.util
+
+import io.ktor.client.*
+import io.ktor.client.engine.okhttp.*
+import io.ktor.client.request.*
+import io.ktor.client.statement.*
+import io.ktor.utils.io.jvm.javaio.*
+import net.mamoe.mirai.utils.ExternalResource
+import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
+import java.io.InputStream
+
+internal val httpClient = HttpClient(OkHttp) {
+    engine {
+        config {
+            followRedirects(true)
+        }
+    }
+}
+
+internal suspend inline fun <R> String.useUrl(consumer: (ExternalResource) -> R) =
+    openAsStream().useStream(consumer)
+
+internal suspend inline fun String.openAsStream(): InputStream =
+    httpClient.get(this).bodyAsChannel().toInputStream()
+
+internal inline fun <R> InputStream.useStream(consumer: (ExternalResource) -> R) =
+    use { toExternalResource().use(consumer) }