From e1cb7a0b8e0dfc5fefff68dee4f279f5f553453c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=A5=E6=B5=B7?= Date: Thu, 11 Apr 2024 10:53:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=8F=91=E9=80=81bas?= =?UTF-8?q?e64=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- httpd/wcfrest/controller.go | 50 ++++++++++++++++++++-------------- public/swagger/swagger.json | 17 +++--------- webview/src/openapi/wcfrest.ts | 15 ++++------ 3 files changed, 38 insertions(+), 44 deletions(-) diff --git a/httpd/wcfrest/controller.go b/httpd/wcfrest/controller.go index 1d6858d4..adda7a59 100644 --- a/httpd/wcfrest/controller.go +++ b/httpd/wcfrest/controller.go @@ -2,8 +2,8 @@ package wcfrest import ( "encoding/base64" - "io/ioutil" "net/http" + "os" "strings" "github.com/gin-gonic/gin" @@ -395,7 +395,7 @@ type SendTxtRequest struct { // @Summary 发送图片消息 // @Produce json // @Tags WCF::消息发送 -// @Param body body SendImgRequest true "发送图片消息参数" +// @Param body body SendFileRequest true "发送图片消息参数" // @Success 200 {object} CommonPayload // @Router /wcf/send_img [post] func (wc *Controller) sendImg(c *gin.Context) { @@ -405,17 +405,17 @@ func (wc *Controller) sendImg(c *gin.Context) { c.Set("Error", err) return } - // 判断 ImageBase64Data非null - if req.ImageBase64Data != "" { - decodedData, err := base64.StdEncoding.DecodeString(req.ImageBase64Data) + + // 将 Base64 写入文件 + if req.Base64 != "" { + fileData, err := base64.StdEncoding.DecodeString(req.Base64) if err != nil { - // 解码失败 - } else { - // 写入到文件中 - err = ioutil.WriteFile(req.Path, decodedData, 0644) - if err != nil { - // 写入失败 - } + c.Set("Error", err) + return + } + if err := os.WriteFile(req.Path, fileData, 0644); err != nil { + c.Set("Error", err) + return } } @@ -427,14 +427,7 @@ func (wc *Controller) sendImg(c *gin.Context) { } -type SendImgRequest struct { - // 图片路径 - Path string `json:"path"` - // 图片base64之后的数据 - ImageBase64Data string `json:"imageBase64Data"` - // 接收人或群的 id - Receiver string `json:"receiver"` -} +type SendImgRequest = SendFileRequest // @Summary 发送文件消息 // @Produce json @@ -450,6 +443,19 @@ func (wc *Controller) sendFile(c *gin.Context) { return } + // 将 Base64 数据写入文件 + if req.Base64 != "" { + fileData, err := base64.StdEncoding.DecodeString(req.Base64) + if err != nil { + c.Set("Error", err) + return + } + if err := os.WriteFile(req.Path, fileData, 0644); err != nil { + c.Set("Error", err) + return + } + } + status := wc.CmdClient.SendFile(req.Path, req.Receiver) c.Set("Payload", CommonPayload{ @@ -459,8 +465,10 @@ func (wc *Controller) sendFile(c *gin.Context) { } type SendFileRequest struct { - // 文件路径 + // 文件路径,若提供 base64 则写入此路径 Path string `json:"path"` + // 文件 base64 数据 + Base64 string `json:"base64"` // 接收人或群的 id Receiver string `json:"receiver"` } diff --git a/public/swagger/swagger.json b/public/swagger/swagger.json index b6b31849..009f487c 100644 --- a/public/swagger/swagger.json +++ b/public/swagger/swagger.json @@ -2026,7 +2026,7 @@ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/wcfrest.SendImgRequest" + "$ref": "#/definitions/wcfrest.SendFileRequest" } } ], @@ -3647,21 +3647,12 @@ "wcfrest.SendFileRequest": { "type": "object", "properties": { - "path": { - "description": "文件路径", + "base64": { + "description": "文件 base64 数据", "type": "string" }, - "receiver": { - "description": "接收人或群的 id", - "type": "string" - } - } - }, - "wcfrest.SendImgRequest": { - "type": "object", - "properties": { "path": { - "description": "图片路径", + "description": "文件路径,若提供 base64 则写入此路径", "type": "string" }, "receiver": { diff --git a/webview/src/openapi/wcfrest.ts b/webview/src/openapi/wcfrest.ts index d7ebc259..3c324f08 100644 --- a/webview/src/openapi/wcfrest.ts +++ b/webview/src/openapi/wcfrest.ts @@ -264,10 +264,10 @@ export const WrestApi = { }, /** * @summary 发送图片消息 - * @param {WcfrestSendImgRequest} body 发送图片消息参数 + * @param {WcfrestSendFileRequest} body 发送图片消息参数 * @param {*} [options] Override http request option. */ - sendImg(body: WcfrestSendImgRequest, options: RequestInit = {}): Promise { + sendImg(body: WcfrestSendFileRequest, options: RequestInit = {}): Promise { options = { method: 'POST', body: JSON.stringify(body || {}), ...options }; return httpRequest('/wcf/send_img', options); }, @@ -471,14 +471,9 @@ export interface WcfrestRevokeMsgRequest { } export interface WcfrestSendFileRequest { - // 文件路径 - path: string; - // 接收人或群的 id - receiver: string; -} - -export interface WcfrestSendImgRequest { - // 图片路径 + // 文件 base64 数据 + base64?: string; + // 文件路径,若提供 base64 则写入此路径 path: string; // 接收人或群的 id receiver: string;