Skip to content

Commit

Permalink
supports file url from LLOnebot and NapCat
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed May 31, 2024
1 parent 184dde6 commit db61867
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
16 changes: 16 additions & 0 deletions onebot/src/main/kotlin/client/core/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import cn.evolvefield.onebot.sdk.response.misc.*
import cn.evolvefield.onebot.sdk.util.*
import cn.evolvefield.onebot.client.config.BotConfig
import cn.evolvefield.onebot.client.handler.ActionHandler
import cn.evolvefield.onebot.sdk.response.ext.GetFileResp
import com.google.gson.*
import me.him188.kotlin.jvm.blocking.bridge.JvmBlockingBridge
import org.java_websocket.WebSocket
Expand Down Expand Up @@ -1428,4 +1429,19 @@ class Bot(
val result = actionHandler.action(this, action, params)
return result.withClass()
}

/**
* 获取群或好友文件信息
*
* LLOnebot, NapCat
* @param fileId 文件ID
* @return [ActionData] of [GetFileResp]
*/
suspend fun extGetFile(fileId: String): ActionData<GetFileResp> {
val action = ActionPathEnum.EXT_GET_FILE
val params = JsonObject()
params.addProperty("file_id", fileId)
val result = actionHandler.action(this, action, params)
return result.withToken()
}
}
9 changes: 8 additions & 1 deletion onebot/src/main/kotlin/sdk/enums/ActionPathEnum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -291,5 +291,12 @@ enum class ActionPathEnum(
/**
* 获取群文件下载链接
*/
GET_GROUP_FILE_URL("get_group_file_url")
GET_GROUP_FILE_URL("get_group_file_url"),

/**
* 获取文件下载链接
*
* 属于 LLOnebot, NapCat 扩展 API
*/
EXT_GET_FILE("get_file"),
}
20 changes: 20 additions & 0 deletions onebot/src/main/kotlin/sdk/response/ext/GetFileResp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cn.evolvefield.onebot.sdk.response.ext

import com.google.gson.annotations.SerializedName
import lombok.AllArgsConstructor
import lombok.Data
import lombok.NoArgsConstructor

@Data
@AllArgsConstructor
@NoArgsConstructor
class GetFileResp {
@SerializedName("file")
var file: String = ""
@SerializedName("file_name")
var fileName: String = ""
@SerializedName("file_size")
var fileSize: Long = 0
@SerializedName("base64")
var base64: String? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import top.mrxiaom.overflow.internal.message.data.WrappedFileMessage
import top.mrxiaom.overflow.internal.utils.toMiraiFiles
import top.mrxiaom.overflow.internal.utils.toMiraiFolders
import top.mrxiaom.overflow.spi.FileService
import java.net.URLEncoder
import java.util.stream.Stream

internal class RemoteFilesWrapper(
Expand Down Expand Up @@ -205,7 +206,13 @@ internal class FileWrapper(
}

override suspend fun getUrl(): String? {
return impl.getGroupFileUrl(contact.id, id, busid).data?.url
when (contact.bot.appName.lowercase()) {
"llonebot", "napcat" -> {
val file = contact.bot.impl.extGetFile(id).data
return file?.file?.run { "file:///${URLEncoder.encode(this, "UTF-8")}" }
}
else -> return impl.getGroupFileUrl(contact.id, id, busid).data?.url
}
}

override suspend fun moveTo(folder: AbsoluteFolder): Boolean {
Expand Down

0 comments on commit db61867

Please sign in to comment.