11 Commits

Author SHA1 Message Date
abbac6315c Merge pull request #301 from tobycroft/master
新增get_file方法(算是补全下功能)
2024-03-18 13:34:51 +08:00
0cf10eabd6 fix: set field file_type not required 2024-03-18 13:34:13 +08:00
8c33267887 fileType加入空匹配,可支持空传 2024-03-18 13:30:18 +08:00
f030104ff2 get_record的ws加入单独的md5字段,方便后续get_file拿文件 2024-03-18 13:23:20 +08:00
5e819179b4 get_record的ws加入单独的md5字段,方便后续get_file拿文件 2024-03-18 04:08:07 +08:00
ea206faf4f get_record的ws加入单独的md5字段,方便后续get_file拿文件 2024-03-18 04:07:11 +08:00
5adfc544a2 修正file_type参数不正确问题 2024-03-18 03:46:51 +08:00
bdb75841cf AGP更新 2024-03-18 03:23:11 +08:00
a3dc0d06b2 新增get_file方法,主要解决使用反向websocket的时候获取文件麻烦的问题,目前仅支持base64的type返回,未来将支持更多模式,测试后将发布至文档 2024-03-18 03:17:36 +08:00
3664352f23 新增get_file方法,主要解决使用反向websocket的时候获取文件麻烦的问题,目前仅支持base64的type返回,未来将支持更多模式,测试后将发布至文档 2024-03-18 03:05:28 +08:00
2770979fee 新增get_file方法,主要解决使用反向websocket的时候获取文件麻烦的问题,目前仅支持base64的type返回,未来将支持更多模式,测试后将发布至文档 2024-03-18 02:57:44 +08:00
5 changed files with 69 additions and 12 deletions

View File

@ -1,6 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.android.application") version "8.2.0" apply false
id("com.android.application") version "8.2.1" apply false
id("org.jetbrains.kotlin.android") version "1.9.22" apply false
id("com.android.library") version "8.2.0" apply false
id("com.android.library") version "8.2.1" apply false
}

View File

@ -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.OutResourceByBase64
import moe.fuqiuluo.shamrock.tools.EmptyJsonString
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.getStringOrNull("file_type") ?: "base64"
return invoke(file, fileType, session.echo)
}
operator fun invoke(file: String, fileType: String = "base64", 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()),
targetFile.nameWithoutExtension,
), echo
)
else -> error("only support base64", echo)
}
} else {
error("not found record file from md5", echo)
}
}
override val requiredParams: Array<String> = arrayOf("file")
}

View File

@ -9,8 +9,7 @@ import moe.fuqiuluo.shamrock.tools.EmptyJsonString
import moe.fuqiuluo.shamrock.utils.AudioUtils
import moe.fuqiuluo.symbols.OneBotHandler
@OneBotHandler("get_record")
internal object GetRecord: IActionHandler() {
@OneBotHandler("get_record") internal object GetRecord : IActionHandler() {
override suspend fun internalHandle(session: ActionSession): String {
val file = session.getString("file")
.replace(regex = "[{}\\-]".toRegex(), replacement = "")
@ -22,17 +21,17 @@ internal object GetRecord: IActionHandler() {
operator fun invoke(file: String, format: String, echo: JsonElement = EmptyJsonString): String {
val pttFile = LocalCacheHelper.getCachePttFile(file)
return if(pttFile.exists()) {
return if (pttFile.exists()) {
val isSilk = AudioUtils.isSilk(pttFile)
val audioFile = when(format) {
val audioFile = when (format) {
"amr" -> AudioUtils.audioToAmr(pttFile, isSilk)
else -> AudioUtils.audioToFormat(pttFile, isSilk, format)
}
ok(
OutResource(
audioFile.toString(),
url = "/res/${audioFile.nameWithoutExtension}"
), echo)
audioFile.toString(), url = "/res/${audioFile.nameWithoutExtension}", md5 = audioFile.nameWithoutExtension
), echo
)
} else {
error("not found record file from cache", echo)
}

View File

@ -26,6 +26,12 @@ fun Routing.fetchRes() {
call.respondText(GetRecord(file, format), ContentType.Application.Json)
}
getOrPost("/get_file") {
val file = formatFileName( fetchGetOrThrow("file") )
val fileType = fetchOrThrow("file_type")
call.respondText(GetFile(file, fileType), ContentType.Application.Json)
}
getOrPost("/get_image") {
val file = formatFileName( fetchGetOrThrow("file") )
call.respondText(GetImage(file), ContentType.Application.Json)

View File

@ -1,9 +1,16 @@
package moe.fuqiuluo.shamrock.remote.service.data
import kotlinx.serialization.Serializable
import java.util.Base64
@Serializable
internal data class OutResource(
@Serializable internal data class OutResource(
val file: String,
val url: String
val url: String,
val md5: String,
)
@Serializable internal data class OutResourceByBase64(
val file: String,
val base64String: String,
val md5: String,
)