mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-12 14:00:12 +08:00
FileCacheStrategy
, ReusableInput
fundamental
This commit is contained in:
parent
9ed1025e30
commit
41fb08f3f1
46
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Mirai.kt
Normal file
46
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Mirai.kt
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
import io.ktor.utils.io.ByteReadChannel
|
||||
import kotlinx.io.core.Input
|
||||
import net.mamoe.mirai.utils.ExternalImage
|
||||
import net.mamoe.mirai.utils.MiraiExperimentalAPI
|
||||
import net.mamoe.mirai.utils.SinceMirai
|
||||
import net.mamoe.mirai.utils.internal.InputStream
|
||||
import kotlin.jvm.JvmStatic
|
||||
|
||||
/**
|
||||
* Mirai 全局环境.
|
||||
*/
|
||||
@SinceMirai("1.0.0")
|
||||
expect object Mirai {
|
||||
|
||||
@JvmStatic
|
||||
var fileCacheStrategy: FileCacheStrategy
|
||||
|
||||
/**
|
||||
* 缓存策略.
|
||||
*
|
||||
* 图片上传时默认使用文件缓存.
|
||||
*/
|
||||
interface FileCacheStrategy {
|
||||
@MiraiExperimentalAPI
|
||||
fun newImageCache(input: Input): ExternalImage
|
||||
|
||||
@MiraiExperimentalAPI
|
||||
fun newImageCache(input: ByteReadChannel): ExternalImage
|
||||
|
||||
@MiraiExperimentalAPI
|
||||
fun newImageCache(input: InputStream): ExternalImage
|
||||
|
||||
companion object Default : FileCacheStrategy
|
||||
}
|
||||
}
|
@ -24,6 +24,7 @@ import net.mamoe.mirai.message.data.Image
|
||||
import net.mamoe.mirai.message.data.OfflineImage
|
||||
import net.mamoe.mirai.message.data.sendTo
|
||||
import net.mamoe.mirai.message.data.toLongUnsigned
|
||||
import net.mamoe.mirai.utils.internal.asReusableInput
|
||||
import kotlin.jvm.JvmSynthetic
|
||||
|
||||
/**
|
||||
@ -34,33 +35,43 @@ import kotlin.jvm.JvmSynthetic
|
||||
* @see ExternalImage.sendTo 上传图片并以纯图片消息发送给联系人
|
||||
* @See ExternalImage.upload 上传图片并得到 [Image] 消息
|
||||
*/
|
||||
class ExternalImage private constructor(
|
||||
@OptIn(MiraiInternalAPI::class)
|
||||
class ExternalImage internal constructor(
|
||||
val md5: ByteArray,
|
||||
val input: Any, // Input from kotlinx.io, InputStream from kotlinx.io MPP, ByteReadChannel from ktor
|
||||
val inputSize: Long // dont be greater than Int.MAX
|
||||
@Suppress("EXPOSED_PROPERTY_TYPE_IN_CONSTRUCTOR")
|
||||
@MiraiInternalAPI("unstable API")
|
||||
val input: ReusableInput, // Input from kotlinx.io, InputStream from kotlinx.io MPP, ByteReadChannel from ktor
|
||||
@MiraiInternalAPI("unstable API")
|
||||
val inputSize: Int // dont be greater than Int.MAX
|
||||
) {
|
||||
@SinceMirai("1.0.0")
|
||||
internal interface ReusableInput {
|
||||
fun input(): Input
|
||||
}
|
||||
|
||||
|
||||
constructor(
|
||||
md5: ByteArray,
|
||||
input: ByteReadChannel,
|
||||
inputSize: Long // dont be greater than Int.MAX
|
||||
) : this(md5, input as Any, inputSize)
|
||||
) : this(md5, input.asReusableInput(), inputSize.coerceAtMost(Int.MAX_VALUE.toLongUnsigned()).toInt())
|
||||
|
||||
constructor(
|
||||
md5: ByteArray,
|
||||
input: Input,
|
||||
inputSize: Long // dont be greater than Int.MAX
|
||||
) : this(md5, input as Any, inputSize)
|
||||
) : this(md5, input.asReusableInput(), inputSize.coerceAtMost(Int.MAX_VALUE.toLongUnsigned()).toInt())
|
||||
|
||||
constructor(
|
||||
md5: ByteArray,
|
||||
input: ByteReadPacket
|
||||
) : this(md5, input as Any, input.remaining)
|
||||
) : this(md5, input.asReusableInput(), input.remaining.coerceAtMost(Int.MAX_VALUE.toLongUnsigned()).toInt())
|
||||
|
||||
@OptIn(InternalSerializationApi::class)
|
||||
constructor(
|
||||
md5: ByteArray,
|
||||
input: InputStream
|
||||
) : this(md5, input as Any, input.available().toLongUnsigned())
|
||||
) : this(md5, input.asReusableInput(), input.available())
|
||||
|
||||
init {
|
||||
require(inputSize < 30L * 1024 * 1024) { "file is too big. Maximum is about 20MB" }
|
||||
|
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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.utils.internal
|
||||
|
||||
import kotlinx.coroutines.io.ByteReadChannel
|
||||
import kotlinx.io.InputStream
|
||||
import kotlinx.io.core.Input
|
||||
import kotlinx.serialization.InternalSerializationApi
|
||||
import net.mamoe.mirai.utils.ExternalImage
|
||||
|
||||
|
||||
internal expect fun ByteReadChannel.asReusableInput(): ExternalImage.ReusableInput
|
||||
internal expect fun Input.asReusableInput(): ExternalImage.ReusableInput
|
||||
|
||||
@OptIn(InternalSerializationApi::class)
|
||||
internal expect fun InputStream.asReusableInput(): ExternalImage.ReusableInput
|
15
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/Mirai.kt
Normal file
15
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/Mirai.kt
Normal file
@ -0,0 +1,15 @@
|
||||
package net.mamoe.mirai
|
||||
|
||||
/**
|
||||
* Mirai 全局环境.
|
||||
*/
|
||||
actual object Mirai {
|
||||
actual var fileCacheStrategy: FileCacheStrategy
|
||||
get() = TODO("Not yet implemented")
|
||||
set(value) {}
|
||||
|
||||
actual interface FileCacheStrategy {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package net.mamoe.mirai.utils.internal
|
||||
|
||||
import kotlinx.coroutines.io.ByteReadChannel
|
||||
import kotlinx.io.core.Input
|
||||
import net.mamoe.mirai.utils.ExternalImage
|
||||
import java.io.InputStream
|
||||
|
||||
internal actual fun ByteReadChannel.asReusableInput(): ExternalImage.ReusableInput {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
internal actual fun Input.asReusableInput(): ExternalImage.ReusableInput {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
internal actual fun InputStream.asReusableInput(): ExternalImage.ReusableInput {
|
||||
TODO("Not yet implemented")
|
||||
}
|
Loading…
Reference in New Issue
Block a user