mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-21 07:29:13 +08:00
Move external resource extensions to their correct companions
This commit is contained in:
parent
fc3d880e19
commit
0406a6dc1b
@ -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 : Contact> C.sendImage(resource: ExternalResource): MessageReceipt<C> =
|
||||
public suspend fun <C : Contact> C.sendImage(resource: ExternalResource): MessageReceipt<C> =
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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 <C : Contact> InputStream.sendAsImageTo(contact: C): MessageReceipt<C> =
|
||||
runBIO {
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
toExternalResource("png")
|
||||
}.withUse { sendAsImageTo(contact) }
|
||||
|
||||
/**
|
||||
* 将文件作为图片发送到指定联系人
|
||||
* @throws OverFileSizeMaxException
|
||||
*/
|
||||
@Throws(OverFileSizeMaxException::class)
|
||||
@JvmStatic
|
||||
@JvmBlockingBridge
|
||||
@JvmName("sendAsImage")
|
||||
public suspend fun <C : Contact> File.sendAsImageTo(contact: C): MessageReceipt<C> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 <C : Contact> InputStream.sendAsImageTo(contact: C): MessageReceipt<C> =
|
||||
runBIO {
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
toExternalResource("png")
|
||||
}.withUse { sendAsImageTo(contact) }
|
||||
|
||||
/**
|
||||
* 将文件作为图片发送到指定联系人
|
||||
* @throws OverFileSizeMaxException
|
||||
*/
|
||||
@Throws(OverFileSizeMaxException::class)
|
||||
@JvmSynthetic
|
||||
public suspend inline fun <C : Contact> File.sendAsImageTo(contact: C): MessageReceipt<C> {
|
||||
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
|
Loading…
Reference in New Issue
Block a user