diff --git a/cmd/openim-msggateway/main.go b/cmd/openim-msggateway/main.go index f6870694b..6d212e467 100644 --- a/cmd/openim-msggateway/main.go +++ b/cmd/openim-msggateway/main.go @@ -23,6 +23,7 @@ func main() { msgGatewayCmd.AddWsPortFlag() msgGatewayCmd.AddPortFlag() msgGatewayCmd.AddPrometheusPortFlag() + if err := msgGatewayCmd.Exec(); err != nil { panic(err.Error()) } diff --git a/internal/api/msg.go b/internal/api/msg.go index 38e207cfb..ca26f8a02 100644 --- a/internal/api/msg.go +++ b/internal/api/msg.go @@ -19,9 +19,9 @@ import ( "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "github.com/mitchellh/mapstructure" - "github.com/openimsdk/open-im-server/v3/pkg/authverify" "github.com/openimsdk/open-im-server/v3/pkg/common/config" + "strings" "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/msg" @@ -150,19 +150,44 @@ func (m *MessageApi) DeleteMsgPhysicalBySeq(c *gin.Context) { func (m *MessageApi) DeleteMsgPhysical(c *gin.Context) { a2r.Call(msg.MsgClient.DeleteMsgPhysical, m.Client, c) } - +func isValidFileExtension(fileURL string, validExtensions []string) bool { + for _, ext := range validExtensions { + if strings.HasSuffix(strings.ToLower(fileURL), ext) { + return true + } + } + return false +} func (m *MessageApi) getSendMsgReq(c *gin.Context, req apistruct.SendMsg) (sendMsgReq *msg.SendMsgReq, err error) { var data interface{} log.ZDebug(c, "getSendMsgReq", "req", req.Content) switch req.ContentType { case constant.Text: data = apistruct.TextElem{} + case constant.Picture: data = apistruct.PictureElem{} + validPictureExtensions := []string{".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".heic", ".webp", ".svg"} + if !isValidFileExtension(data.(apistruct.PictureElem).SnapshotPicture.Url, validPictureExtensions) { + return nil, errs.ErrArgs.WithDetail("SnapshotPicture file must be in a valid format (e.g., .jpg, .jpeg, .png, .gif, .bmp, .tiff, .heic, .webp, .svg)") + } + if !isValidFileExtension(data.(apistruct.PictureElem).BigPicture.Url, validPictureExtensions) { + return nil, errs.ErrArgs.WithDetail("BigPicture file must be in a valid format (e.g., .jpg, .jpeg, .png, .gif, .bmp, .tiff, .heic, .webp, .svg)") + } + if !isValidFileExtension(data.(apistruct.PictureElem).SourcePicture.Url, validPictureExtensions) { + return nil, errs.ErrArgs.WithDetail("SourcePicture file must be in a valid format (e.g., .jpg, .jpeg, .png, .gif, .bmp, .tiff, .heic, .webp, .svg)") + } case constant.Voice: data = apistruct.SoundElem{} + if !isValidFileExtension(data.(apistruct.PictureElem).SourcePicture.Url, validPictureExtensions) { + return nil, errs.ErrArgs.WithDetail("picture file must be in a valid format (e.g., .jpg, .jpeg, .png, .gif, .bmp, .tiff, .heic, .webp, .svg)") + } case constant.Video: data = apistruct.VideoElem{} + validVideoExtensions := []string{".mp4", ".avi", ".mov", ".wmv", ".flv", ".mkv"} + if !isValidFileExtension(data.(apistruct.VideoElem).VideoURL, validVideoExtensions) { + return nil, errs.ErrArgs.WithDetail("video file must be in a valid format (e.g., .mp4, .avi, .mov, .wmv, .flv, .mkv)") + } case constant.File: data = apistruct.FileElem{} case constant.Custom: diff --git a/internal/msggateway/n_ws_server.go b/internal/msggateway/n_ws_server.go index 10dd988d1..736894708 100644 --- a/internal/msggateway/n_ws_server.go +++ b/internal/msggateway/n_ws_server.go @@ -117,6 +117,7 @@ func (ws *WsServer) UnRegister(c *Client) { } func (ws *WsServer) Validate(s interface{}) error { + //?question? return nil } diff --git a/pkg/apistruct/msg.go b/pkg/apistruct/msg.go index 12cf253a0..ab4e3ade0 100644 --- a/pkg/apistruct/msg.go +++ b/pkg/apistruct/msg.go @@ -16,56 +16,56 @@ package apistruct type PictureBaseInfo struct { UUID string `mapstructure:"uuid"` - Type string `mapstructure:"type"` + Type string `mapstructure:"type" validate:"required"` Size int64 `mapstructure:"size"` - Width int32 `mapstructure:"width"` - Height int32 `mapstructure:"height"` - Url string `mapstructure:"url"` + Width int32 `mapstructure:"width" validate:"required"` + Height int32 `mapstructure:"height" validate:"required"` + Url string `mapstructure:"url" validate:"required"` } type PictureElem struct { SourcePath string `mapstructure:"sourcePath"` - SourcePicture PictureBaseInfo `mapstructure:"sourcePicture"` - BigPicture PictureBaseInfo `mapstructure:"bigPicture"` - SnapshotPicture PictureBaseInfo `mapstructure:"snapshotPicture"` + SourcePicture PictureBaseInfo `mapstructure:"sourcePicture" validate:"required"` + BigPicture PictureBaseInfo `mapstructure:"bigPicture" validate:"required"` + SnapshotPicture PictureBaseInfo `mapstructure:"snapshotPicture" validate:"required"` } type SoundElem struct { UUID string `mapstructure:"uuid"` SoundPath string `mapstructure:"soundPath"` - SourceURL string `mapstructure:"sourceUrl"` + SourceURL string `mapstructure:"sourceUrl" validate:"required"` DataSize int64 `mapstructure:"dataSize"` - Duration int64 `mapstructure:"duration"` + Duration int64 `mapstructure:"duration" validate:"required,min=1"` } type VideoElem struct { - VideoPath string `mapstructure:"videoPath"` + VideoPath string `mapstructure:"videoPath" ` VideoUUID string `mapstructure:"videoUUID"` - VideoURL string `mapstructure:"videoUrl"` - VideoType string `mapstructure:"videoType"` - VideoSize int64 `mapstructure:"videoSize"` - Duration int64 `mapstructure:"duration"` + VideoURL string `mapstructure:"videoUrl" validate:"required"` + VideoType string `mapstructure:"videoType" validate:"required"` + VideoSize int64 `mapstructure:"videoSize" validate:"required"` + Duration int64 `mapstructure:"duration" validate:"required"` SnapshotPath string `mapstructure:"snapshotPath"` SnapshotUUID string `mapstructure:"snapshotUUID"` SnapshotSize int64 `mapstructure:"snapshotSize"` - SnapshotURL string `mapstructure:"snapshotUrl"` - SnapshotWidth int32 `mapstructure:"snapshotWidth"` - SnapshotHeight int32 `mapstructure:"snapshotHeight"` + SnapshotURL string `mapstructure:"snapshotUrl" validate:"required"` + SnapshotWidth int32 `mapstructure:"snapshotWidth" validate:"required"` + SnapshotHeight int32 `mapstructure:"snapshotHeight"validate:"required"` } type FileElem struct { - FilePath string `mapstructure:"filePath"` + FilePath string `mapstructure:"filePath" ` UUID string `mapstructure:"uuid"` - SourceURL string `mapstructure:"sourceUrl"` - FileName string `mapstructure:"fileName"` - FileSize int64 `mapstructure:"fileSize"` + SourceURL string `mapstructure:"sourceUrl" validate:"required"` + FileName string `mapstructure:"fileName" validate:"required"` + FileSize int64 `mapstructure:"fileSize" validate:"required"` } type AtElem struct { Text string `mapstructure:"text"` - AtUserList []string `mapstructure:"atUserList"` + AtUserList []string `mapstructure:"atUserList" validate:"required,max=1000"` IsAtSelf bool `mapstructure:"isAtSelf"` } type LocationElem struct { - Description string `mapstructure:"description"` - Longitude float64 `mapstructure:"longitude"` - Latitude float64 `mapstructure:"latitude"` + Description string `mapstructure:"description" ` + Longitude float64 `mapstructure:"longitude" validate:"required"` + Latitude float64 `mapstructure:"latitude" validate:"required"` } type CustomElem struct { Data string `mapstructure:"data" validate:"required"`