Skip to content

Commit

Permalink
feat: 支持发送base64文件
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Apr 11, 2024
1 parent def7da2 commit e1cb7a0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 44 deletions.
50 changes: 29 additions & 21 deletions httpd/wcfrest/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package wcfrest

import (
"encoding/base64"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
}

Expand All @@ -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
Expand All @@ -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{
Expand All @@ -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"`
}
Expand Down
17 changes: 4 additions & 13 deletions public/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2026,7 +2026,7 @@
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/wcfrest.SendImgRequest"
"$ref": "#/definitions/wcfrest.SendFileRequest"
}
}
],
Expand Down Expand Up @@ -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": {
Expand Down
15 changes: 5 additions & 10 deletions webview/src/openapi/wcfrest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WcfrestCommonPayload> {
sendImg(body: WcfrestSendFileRequest, options: RequestInit = {}): Promise<WcfrestCommonPayload> {
options = { method: 'POST', body: JSON.stringify(body || {}), ...options };
return httpRequest('/wcf/send_img', options);
},
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e1cb7a0

Please sign in to comment.