修复了一些逻辑漏洞
This commit is contained in:
@@ -5,7 +5,9 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"xk-websocket-v2/internal/model"
|
||||
"xk-websocket-v2/internal/service"
|
||||
"xk-websocket-v2/internal/utils"
|
||||
@@ -32,17 +34,21 @@ func UploadAttachmentHandler(c *gin.Context) {
|
||||
fileType := c.PostForm("type")
|
||||
if fileType == "" {
|
||||
// 根据文件扩展名推断类型
|
||||
ext := file.Filename[len(file.Filename)-4:]
|
||||
if ext == ".jpg" || ext == ".png" || ext == ".gif" || ext == "webp" || ext == "jpeg" {
|
||||
ext := strings.ToLower(filepath.Ext(file.Filename))
|
||||
if ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".gif" || ext == ".webp" {
|
||||
fileType = "image"
|
||||
} else {
|
||||
} else if ext == ".mp4" || ext == ".avi" || ext == ".mov" || ext == ".wmv" || ext == ".flv" || ext == ".mkv" {
|
||||
fileType = "video"
|
||||
} else if ext == ".mp3" || ext == ".wav" || ext == ".m4a" || ext == ".webm" || ext == ".ogg" || ext == ".aac" {
|
||||
fileType = "audio"
|
||||
} else {
|
||||
fileType = "file"
|
||||
}
|
||||
}
|
||||
|
||||
// 验证文件类型
|
||||
if fileType != "image" && fileType != "video" {
|
||||
utils.BadRequest(c, "文件类型必须是image或video")
|
||||
if fileType != "image" && fileType != "video" && fileType != "audio" && fileType != "file" {
|
||||
utils.BadRequest(c, "文件类型必须是image、video、audio或file")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -55,6 +61,14 @@ func UploadAttachmentHandler(c *gin.Context) {
|
||||
utils.BadRequest(c, "视频大小不能超过500MB")
|
||||
return
|
||||
}
|
||||
if fileType == "audio" && file.Size > model.MaxAudioSize {
|
||||
utils.BadRequest(c, "音频大小不能超过50MB")
|
||||
return
|
||||
}
|
||||
if fileType == "file" && file.Size > model.MaxFileSize {
|
||||
utils.BadRequest(c, "文件大小不能超过100MB")
|
||||
return
|
||||
}
|
||||
|
||||
// 打开文件
|
||||
src, err := file.Open()
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"xk-websocket-v2/internal/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -119,4 +120,30 @@ func DeleteConversationHandler(c *gin.Context) {
|
||||
utils.Success(c, "删除成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* GetConversationByRoomHandler
|
||||
* 功能:根据 room_id 获取或创建会话
|
||||
* 路径:GET /api/conversations/by-room/:room_id
|
||||
*/
|
||||
func GetConversationByRoomHandler(c *gin.Context) {
|
||||
userID, _ := c.Get("user_id")
|
||||
roomID := c.Param("room_id")
|
||||
|
||||
if roomID == "" {
|
||||
utils.BadRequest(c, "room_id 不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
conv, err := service.ConversationSvc.GetOrCreateConversationByRoom(userID.(string), roomID)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
utils.NotFound(c, "房间不存在或无权访问")
|
||||
} else {
|
||||
utils.InternalError(c, "查询失败")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
utils.SuccessWithData(c, conv, "获取成功")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user