From 0406a6dc1b989d0e87e6595c010102c794ad3ca0 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 27 Dec 2020 20:34:35 +0800 Subject: [PATCH] Move external resource extensions to their correct companions --- .../src/commonMain/kotlin/contact/Contact.kt | 37 ++++- .../commonMain/kotlin/event/events/message.kt | 1 + .../kotlin/utils/ExternalResource.kt | 76 +++++++++++ .../kotlin/utils/SendImageUtilsJvmKt.kt | 126 ------------------ 4 files changed, 113 insertions(+), 127 deletions(-) delete mode 100644 mirai-core-api/src/commonMain/kotlin/utils/SendImageUtilsJvmKt.kt diff --git a/mirai-core-api/src/commonMain/kotlin/contact/Contact.kt b/mirai-core-api/src/commonMain/kotlin/contact/Contact.kt index 632c74a2d..2697abc31 100644 --- a/mirai-core-api/src/commonMain/kotlin/contact/Contact.kt +++ b/mirai-core-api/src/commonMain/kotlin/contact/Contact.kt @@ -22,6 +22,7 @@ import net.mamoe.mirai.message.MessageReceipt.Companion.recall import net.mamoe.mirai.message.data.* import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.ExternalResource.Companion.sendAsImageTo +import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage import java.io.File import java.io.InputStream @@ -123,8 +124,42 @@ public interface Contact : ContactOrBot, CoroutineScope { */ @JvmBlockingBridge @JvmStatic - public suspend inline fun C.sendImage(resource: ExternalResource): MessageReceipt = + public suspend fun C.sendImage(resource: ExternalResource): MessageReceipt = resource.sendAsImageTo(this) + + + /** + * 读取 [InputStream] 到临时文件并将其作为图片上传, 但不发送 + * + * 注意:本函数不会关闭流 + * + * @throws OverFileSizeMaxException + */ + @Throws(OverFileSizeMaxException::class) + @JvmStatic + @JvmBlockingBridge + public suspend fun Contact.uploadImage(imageStream: InputStream): Image = + imageStream.uploadAsImage(this@uploadImage) + + /** + * 将文件作为图片上传, 但不发送 + * @throws OverFileSizeMaxException + */ + @Throws(OverFileSizeMaxException::class) + @JvmStatic + @JvmBlockingBridge + public suspend fun Contact.uploadImage(file: File): Image = file.uploadAsImage(this) + + /** + * 将文件作为图片上传, 但不发送 + * @throws OverFileSizeMaxException + */ + @Throws(OverFileSizeMaxException::class) + @JvmStatic + @JvmBlockingBridge + @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "EXTENSION_SHADOWED_BY_MEMBER") + @kotlin.internal.LowPriorityInOverloadResolution // for better Java API + public suspend fun Contact.uploadImage(resource: ExternalResource): Image = this.uploadImage(resource) } } diff --git a/mirai-core-api/src/commonMain/kotlin/event/events/message.kt b/mirai-core-api/src/commonMain/kotlin/event/events/message.kt index abe9c6ee5..d7998e1d0 100644 --- a/mirai-core-api/src/commonMain/kotlin/event/events/message.kt +++ b/mirai-core-api/src/commonMain/kotlin/event/events/message.kt @@ -17,6 +17,7 @@ import net.mamoe.kjbb.JvmBlockingBridge import net.mamoe.mirai.Bot import net.mamoe.mirai.contact.* import net.mamoe.mirai.contact.Contact.Companion.sendImage +import net.mamoe.mirai.contact.Contact.Companion.uploadImage import net.mamoe.mirai.event.* import net.mamoe.mirai.event.events.ImageUploadEvent.Failed import net.mamoe.mirai.event.events.ImageUploadEvent.Succeed diff --git a/mirai-core-api/src/commonMain/kotlin/utils/ExternalResource.kt b/mirai-core-api/src/commonMain/kotlin/utils/ExternalResource.kt index efaf10a2c..d922a7630 100644 --- a/mirai-core-api/src/commonMain/kotlin/utils/ExternalResource.kt +++ b/mirai-core-api/src/commonMain/kotlin/utils/ExternalResource.kt @@ -15,10 +15,12 @@ import net.mamoe.kjbb.JvmBlockingBridge import net.mamoe.mirai.Mirai import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.Contact.Companion.sendImage +import net.mamoe.mirai.contact.Contact.Companion.uploadImage import net.mamoe.mirai.contact.Group import net.mamoe.mirai.contact.User import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.data.Image +import net.mamoe.mirai.message.data.Voice import net.mamoe.mirai.message.data.sendTo import net.mamoe.mirai.utils.ExternalResource.Companion.sendAsImageTo import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource @@ -153,6 +155,36 @@ public interface ExternalResource : Closeable { else -> error("unreachable") } + /** + * 读取 [InputStream] 到临时文件并将其作为图片发送到指定联系人 + * + * 注意:本函数不会关闭流 + * + * @throws OverFileSizeMaxException + */ + @Throws(OverFileSizeMaxException::class) + @JvmStatic + @JvmBlockingBridge + @JvmName("sendAsImage") + public suspend fun InputStream.sendAsImageTo(contact: C): MessageReceipt = + runBIO { + @Suppress("BlockingMethodInNonBlockingContext") + toExternalResource("png") + }.withUse { sendAsImageTo(contact) } + + /** + * 将文件作为图片发送到指定联系人 + * @throws OverFileSizeMaxException + */ + @Throws(OverFileSizeMaxException::class) + @JvmStatic + @JvmBlockingBridge + @JvmName("sendAsImage") + public suspend fun File.sendAsImageTo(contact: C): MessageReceipt { + require(this.exists() && this.canRead()) + return toExternalResource("png").withUse { sendAsImageTo(contact) } + } + /** * 上传图片并构造 [Image]. * 这个函数可能需消耗一段时间. @@ -170,6 +202,50 @@ public interface ExternalResource : Closeable { is User -> contact.uploadImage(this) else -> error("unreachable") } + + /** + * 读取 [InputStream] 到临时文件并将其作为图片上传后构造 [Image] + * + * 注意:本函数不会关闭流 + * + * @throws OverFileSizeMaxException + */ + @Throws(OverFileSizeMaxException::class) + @JvmStatic + @JvmBlockingBridge + public suspend fun InputStream.uploadAsImage(contact: Contact): Image = + @Suppress("BlockingMethodInNonBlockingContext") + runBIO { toExternalResource("png") }.withUse { uploadAsImage(contact) } + + /** + * 将文件作为图片上传后构造 [Image] + * @throws OverFileSizeMaxException + */ + @Throws(OverFileSizeMaxException::class) + @JvmStatic + @JvmBlockingBridge + public suspend fun File.uploadAsImage(contact: Contact): Image { + require(this.isFile && this.exists() && this.canRead()) { "file ${this.path} is not readable" } + return toExternalResource("png").withUse { uploadAsImage(contact) } + } + + + /** + * 将文件作为语音上传后构造 [Voice] + * + * - 请手动关闭输入流 + * - 请使用 amr 或 silk 格式 + * + * @suppress 注意,这只是个实验性功能且随时可能会删除 + * @throws OverFileSizeMaxException + */ + @Throws(OverFileSizeMaxException::class) + @MiraiExperimentalApi("语音支持处于实验性阶段") + @JvmBlockingBridge + @JvmStatic + public suspend fun InputStream.uploadAsGroupVoice(group: Group): Voice { + return group.uploadVoice(this) + } } } diff --git a/mirai-core-api/src/commonMain/kotlin/utils/SendImageUtilsJvmKt.kt b/mirai-core-api/src/commonMain/kotlin/utils/SendImageUtilsJvmKt.kt deleted file mode 100644 index a8afd54d6..000000000 --- a/mirai-core-api/src/commonMain/kotlin/utils/SendImageUtilsJvmKt.kt +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 2019-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 - */ - -/** - * 为 Kotlin 使用者实现的发送图片的一些扩展函数. - */ - -@file:Suppress("unused") -@file:JvmMultifileClass -@file:JvmName("SendResourceUtilsJvmKt") - -package net.mamoe.mirai.utils - -import net.mamoe.mirai.contact.Contact -import net.mamoe.mirai.contact.Group -import net.mamoe.mirai.message.MessageReceipt -import net.mamoe.mirai.message.data.Image -import net.mamoe.mirai.message.data.Voice -import net.mamoe.mirai.utils.ExternalResource.Companion.sendAsImageTo -import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource -import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage -import java.io.File -import java.io.InputStream - -// region IMAGE.sendAsImageTo(Contact) - -/** - * 读取 [InputStream] 到临时文件并将其作为图片发送到指定联系人 - * - * 注意:本函数不会关闭流 - * - * @throws OverFileSizeMaxException - */ -@Throws(OverFileSizeMaxException::class) -@JvmSynthetic -public suspend inline fun InputStream.sendAsImageTo(contact: C): MessageReceipt = - runBIO { - @Suppress("BlockingMethodInNonBlockingContext") - toExternalResource("png") - }.withUse { sendAsImageTo(contact) } - -/** - * 将文件作为图片发送到指定联系人 - * @throws OverFileSizeMaxException - */ -@Throws(OverFileSizeMaxException::class) -@JvmSynthetic -public suspend inline fun File.sendAsImageTo(contact: C): MessageReceipt { - require(this.exists() && this.canRead()) - return toExternalResource("png").withUse { sendAsImageTo(contact) } -} - -// endregion - -// region IMAGE.Upload(Contact): Image - -/** - * 读取 [InputStream] 到临时文件并将其作为图片上传后构造 [Image] - * - * 注意:本函数不会关闭流 - * - * @throws OverFileSizeMaxException - */ -@Throws(OverFileSizeMaxException::class) -@JvmSynthetic -public suspend inline fun InputStream.uploadAsImage(contact: Contact): Image = - @Suppress("BlockingMethodInNonBlockingContext") - runBIO { toExternalResource("png") }.withUse { uploadAsImage(contact) } - -/** - * 将文件作为图片上传后构造 [Image] - * @throws OverFileSizeMaxException - */ -@Throws(OverFileSizeMaxException::class) -@JvmSynthetic -public suspend inline fun File.uploadAsImage(contact: Contact): Image { - require(this.isFile && this.exists() && this.canRead()) { "file ${this.path} is not readable" } - return toExternalResource("png").withUse { uploadAsImage(contact) } -} - -/** - * 将文件作为语音上传后构造 [Voice] - * - * - 请手动关闭输入流 - * - 请使用 amr 或 silk 格式 - * - * @suppress 注意,这只是个实验性功能且随时可能会删除 - * @throws OverFileSizeMaxException - */ -@Throws(OverFileSizeMaxException::class) -@MiraiExperimentalApi("语音支持处于实验性阶段") -public suspend inline fun InputStream.uploadAsGroupVoice(group: Group): Voice { - return group.uploadVoice(this) -} - -// endregion - -// region Contact.uploadImage(IMAGE) - -/** - * 读取 [InputStream] 到临时文件并将其作为图片上传, 但不发送 - * - * 注意:本函数不会关闭流 - * - * @throws OverFileSizeMaxException - */ -@Throws(OverFileSizeMaxException::class) -@JvmSynthetic -public suspend inline fun Contact.uploadImage(imageStream: InputStream): Image = - imageStream.uploadAsImage(this@uploadImage) - -/** - * 将文件作为图片上传, 但不发送 - * @throws OverFileSizeMaxException - */ -@Throws(OverFileSizeMaxException::class) -@JvmSynthetic -public suspend inline fun Contact.uploadImage(file: File): Image = file.uploadAsImage(this) - -// endregion