mirror of
https://github.com/whitechi73/OpenShamrock.git
synced 2024-08-14 13:12:17 +08:00
新增get_file方法,主要解决使用反向websocket的时候获取文件麻烦的问题,目前仅支持base64的type返回,未来将支持更多模式,测试后将发布至文档
This commit is contained in:
parent
be58c368e9
commit
2770979fee
@ -0,0 +1,45 @@
|
|||||||
|
package moe.fuqiuluo.shamrock.remote.action.handlers
|
||||||
|
|
||||||
|
import kotlinx.serialization.json.JsonElement
|
||||||
|
import moe.fuqiuluo.shamrock.remote.action.ActionSession
|
||||||
|
import moe.fuqiuluo.shamrock.remote.action.IActionHandler
|
||||||
|
import moe.fuqiuluo.shamrock.remote.service.data.OutResource
|
||||||
|
import moe.fuqiuluo.shamrock.remote.service.data.OutResourceByBase64
|
||||||
|
import moe.fuqiuluo.shamrock.tools.EmptyJsonString
|
||||||
|
import moe.fuqiuluo.shamrock.utils.AudioUtils
|
||||||
|
import moe.fuqiuluo.shamrock.utils.FileUtils
|
||||||
|
import moe.fuqiuluo.symbols.OneBotHandler
|
||||||
|
import java.util.Base64
|
||||||
|
|
||||||
|
@OneBotHandler("get_file") internal object GetFile : IActionHandler() {
|
||||||
|
override suspend fun internalHandle(session: ActionSession): String {
|
||||||
|
val file = session.getString("file")
|
||||||
|
.replace(regex = "[{}\\-]".toRegex(), replacement = "")
|
||||||
|
.replace(" ", "")
|
||||||
|
.split(".")[0].lowercase()
|
||||||
|
val fileType = session.getString("file_type")
|
||||||
|
return invoke(file, fileType, session.echo)
|
||||||
|
}
|
||||||
|
|
||||||
|
operator fun invoke(file: String, fileType: String, echo: JsonElement = EmptyJsonString): String {
|
||||||
|
val targetFile = FileUtils.getFileByMd5(file)
|
||||||
|
return if (targetFile.exists()) {
|
||||||
|
when (fileType) {
|
||||||
|
"base64" -> ok(
|
||||||
|
OutResourceByBase64(
|
||||||
|
"/res/${targetFile.nameWithoutExtension}",
|
||||||
|
Base64.getEncoder()
|
||||||
|
.encodeToString(targetFile.readBytes()),
|
||||||
|
), echo
|
||||||
|
)
|
||||||
|
|
||||||
|
else -> error("only support base64", echo)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
error("not found record file from md5", echo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override val requiredParams: Array<String> = arrayOf("file", "file_type")
|
||||||
|
}
|
@ -1,9 +1,15 @@
|
|||||||
package moe.fuqiuluo.shamrock.remote.service.data
|
package moe.fuqiuluo.shamrock.remote.service.data
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
import java.util.Base64
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
internal data class OutResource(
|
internal data class OutResource(
|
||||||
val file: String,
|
val file: String,
|
||||||
val url: String
|
val url: String
|
||||||
|
)
|
||||||
|
|
||||||
|
internal data class OutResourceByBase64(
|
||||||
|
val file: String,
|
||||||
|
val base64String: String,
|
||||||
)
|
)
|
Loading…
x
Reference in New Issue
Block a user