mirror of
https://github.com/whitechi73/OpenShamrock.git
synced 2024-08-14 13:12:17 +08:00
Merge pull request #303 from tobycroft/master
GetFile的type新增gzip,会将数据流压缩后再b64降低带宽占用
This commit is contained in:
commit
7782feb6ac
@ -7,10 +7,11 @@ import moe.fuqiuluo.shamrock.remote.service.data.OutResourceByBase64
|
||||
import moe.fuqiuluo.shamrock.tools.EmptyJsonString
|
||||
import moe.fuqiuluo.shamrock.utils.FileUtils
|
||||
import moe.fuqiuluo.symbols.OneBotHandler
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.util.Base64
|
||||
import java.util.zip.GZIPOutputStream
|
||||
|
||||
@OneBotHandler("get_file")
|
||||
internal object GetFile : IActionHandler() {
|
||||
@OneBotHandler("get_file") internal object GetFile : IActionHandler() {
|
||||
override suspend fun internalHandle(session: ActionSession): String {
|
||||
val file = session.getString("file")
|
||||
.replace(regex = "[{}\\-]".toRegex(), replacement = "")
|
||||
@ -33,7 +34,15 @@ internal object GetFile : IActionHandler() {
|
||||
), echo
|
||||
)
|
||||
|
||||
else -> error("only support base64", echo)
|
||||
"gzip" -> ok(
|
||||
OutResourceByBase64(
|
||||
"/res/${targetFile.nameWithoutExtension}",
|
||||
compressAndEncode(targetFile.readBytes()),
|
||||
targetFile.nameWithoutExtension,
|
||||
), echo
|
||||
)
|
||||
|
||||
else -> error("file_type error", echo)
|
||||
}
|
||||
|
||||
} else {
|
||||
@ -41,5 +50,19 @@ internal object GetFile : IActionHandler() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun compressAndEncode(input: ByteArray): String {
|
||||
// 压缩数据
|
||||
val outputStream = ByteArrayOutputStream()
|
||||
val gzip = GZIPOutputStream(outputStream)
|
||||
gzip.write(input)
|
||||
gzip.close()
|
||||
val compressedBytes = outputStream.toByteArray()
|
||||
|
||||
// 编码为 Base64 字符串
|
||||
return Base64.getEncoder()
|
||||
.encodeToString(compressedBytes)
|
||||
}
|
||||
|
||||
override val requiredParams: Array<String> = arrayOf("file")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user