From 307a09dd11a951228f1dce7f4fc57f56df7957da Mon Sep 17 00:00:00 2001 From: Mrs4s <1844812067@qq.com> Date: Fri, 5 Feb 2021 22:24:49 +0800 Subject: [PATCH] feature upload_group_file. --- coolq/api.go | 20 ++++++++++++++++++++ server/http.go | 6 ++++++ server/websocket.go | 3 +++ 3 files changed, 29 insertions(+) diff --git a/coolq/api.go b/coolq/api.go index 626a912..470fba8 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -181,6 +181,26 @@ func (bot *CQBot) CQGetGroupFileUrl(groupId int64, fileId string, busId int32) M }) } +func (bot *CQBot) CQUploadGroupFile(groupId int64, file, name, folder string) MSG { + if !global.PathExists(file) { + log.Errorf("上传群文件 %v 失败: 文件不存在", file) + return Failed(100, "FILE_NOT_FOUND", "文件不存在") + } + fs, err := bot.Client.GetGroupFileSystem(groupId) + if err != nil { + log.Errorf("获取群 %v 文件系统信息失败: %v", groupId, err) + return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error()) + } + if folder == "" { + folder = "/" + } + if err = fs.UploadFile(file, name, folder); err != nil { + log.Errorf("上传群 %v 文件 %v 失败: %v", groupId, file, err) + return Failed(100, "FILE_SYSTEM_UPLOAD_API_ERROR", err.Error()) + } + return OK(nil) +} + func (bot *CQBot) CQGetWordSlices(content string) MSG { slices, err := bot.Client.GetWordSegmentation(content) if err != nil { diff --git a/server/http.go b/server/http.go index f63ce2d..180d9c5 100644 --- a/server/http.go +++ b/server/http.go @@ -215,6 +215,11 @@ func GetGroupFileUrl(s *httpServer, c *gin.Context) { c.JSON(200, s.bot.CQGetGroupFileUrl(gid, fid, int32(busid))) } +func UploadGroupFile(s *httpServer, c *gin.Context) { + gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64) + c.JSON(200, s.bot.CQUploadGroupFile(gid, getParam(c, "file"), getParam(c, "name"), getParam(c, "folder"))) +} + func SendMessage(s *httpServer, c *gin.Context) { if getParam(c, "message_type") == "private" { SendPrivateMessage(s, c) @@ -560,6 +565,7 @@ var httpApi = map[string]func(s *httpServer, c *gin.Context){ "get_group_root_files": GetGroupRootFiles, "get_group_files_by_folder": GetGroupFilesByFolderId, "get_group_file_url": GetGroupFileUrl, + "upload_group_file": UploadGroupFile, "get_essence_msg_list": GetEssenceMsgList, "send_msg": SendMessage, "send_group_msg": SendGroupMessage, diff --git a/server/websocket.go b/server/websocket.go index 6b7a829..bf92628 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -562,6 +562,9 @@ var wsAPI = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{ "get_group_file_url": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { return bot.CQGetGroupFileUrl(p.Get("group_id").Int(), p.Get("file_id").Str, int32(p.Get("busid").Int())) }, + "upload_group_file": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { + return bot.CQUploadGroupFile(p.Get("group_id").Int(), p.Get("file").Str, p.Get("name").Str, p.Get("folder").Str) + }, "get_group_msg_history": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { return bot.CQGetGroupMessageHistory(p.Get("group_id").Int(), p.Get("message_seq").Int()) },