From 9451d22dbcacdb793029dfe4d2b76e2a741ddb85 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Fri, 21 Jul 2023 15:11:05 +0800 Subject: [PATCH 1/4] fix: msg file content type --- internal/conversation_msg/sdk.go | 15 +++--- pkg/content_type/content_type.go | 91 ++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 pkg/content_type/content_type.go diff --git a/internal/conversation_msg/sdk.go b/internal/conversation_msg/sdk.go index 3a323357d..5a9b35954 100644 --- a/internal/conversation_msg/sdk.go +++ b/internal/conversation_msg/sdk.go @@ -24,6 +24,7 @@ import ( "open_im_sdk/open_im_sdk_callback" "open_im_sdk/pkg/common" "open_im_sdk/pkg/constant" + "open_im_sdk/pkg/content_type" "open_im_sdk/pkg/db/model_struct" "open_im_sdk/pkg/sdkerrs" "path/filepath" @@ -293,8 +294,9 @@ func (c *Conversation) updateMsgStatusAndTriggerConversation(ctx context.Context } func (c *Conversation) fileName(ftype string, id string) string { - return fmt.Sprintf("%s_%s_%s", c.loginUserID, ftype, id) + return fmt.Sprintf("msg_%s_%s", ftype, id) } + func (c *Conversation) checkID(ctx context.Context, s *sdk_struct.MsgStruct, recvID, groupID string, options map[string]bool) (*model_struct.LocalConversation, error) { if recvID == "" && groupID == "" { @@ -517,7 +519,7 @@ func (c *Conversation) SendMessage(ctx context.Context, s *sdk_struct.MsgStruct, go func() { defer wg.Done() res, err := c.file.UploadFile(ctx, &file.UploadFileReq{ - ContentType: s.VideoElem.VideoType, + ContentType: content_type.GetType(s.VideoElem.VideoType, filepath.Ext(s.VideoElem.VideoPath)), Filepath: videoPath, Uuid: s.VideoElem.VideoUUID, Name: c.fileName("video", s.ClientMsgID) + filepath.Ext(videoPath), @@ -540,10 +542,11 @@ func (c *Conversation) SendMessage(ctx context.Context, s *sdk_struct.MsgStruct, break } res, err := c.file.UploadFile(ctx, &file.UploadFileReq{ - Filepath: s.FileElem.FilePath, - Uuid: s.FileElem.UUID, - Name: c.fileName("file", s.ClientMsgID) + filepath.Ext(s.FileElem.FilePath), - Cause: "msg-file", + ContentType: content_type.GetType(s.FileElem.FileType, filepath.Ext(s.FileElem.FilePath), filepath.Ext(s.FileElem.FileName)), + Filepath: s.FileElem.FilePath, + Uuid: s.FileElem.UUID, + Name: c.fileName("file", s.ClientMsgID) + "/" + filepath.Base(s.FileElem.FileName), + Cause: "msg-file", }, NewUploadFileCallback(ctx, callback.OnProgress, s, lc.ConversationID, c.db)) if err != nil { c.updateMsgStatusAndTriggerConversation(ctx, s.ClientMsgID, "", s.CreateTime, constant.MsgStatusSendFailed, s, lc) diff --git a/pkg/content_type/content_type.go b/pkg/content_type/content_type.go new file mode 100644 index 000000000..1fa25afab --- /dev/null +++ b/pkg/content_type/content_type.go @@ -0,0 +1,91 @@ +package content_type + +import ( + "strings" +) + +var ext = map[string]string{ + "html": "text/html", + "htm": "text/html", + "css": "text/css", + "js": "application/javascript", + "json": "application/json", + "xml": "application/xml", + "png": "image/png", + "jpg": "image/jpeg", + "jpeg": "image/jpeg", + "gif": "image/gif", + "bmp": "image/bmp", + "ico": "image/x-icon", + "svg": "image/svg+xml", + "webp": "image/webp", + "mp4": "video/mp4", + "avi": "video/x-msvideo", + "mkv": "video/x-matroska", + "mov": "video/quicktime", + "wmv": "video/x-ms-wmv", + "flv": "video/x-flv", + "webm": "video/webm", + "3gp": "video/3gpp", + "mp3": "audio/mpeg", + "wav": "audio/wav", + "ogg": "audio/ogg", + "aac": "audio/aac", + "wma": "audio/x-ms-wma", + "flac": "audio/flac", + "mid": "audio/midi", + "midi": "audio/midi", + "weba": "audio/webm", + "pdf": "application/pdf", + "doc": "application/msword", + "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "xls": "application/vnd.ms-excel", + "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "ppt": "application/vnd.ms-powerpoint", + "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", + "txt": "text/plain", + "csv": "text/csv", + "zip": "application/zip", + "rar": "application/x-rar-compressed", + "tar": "application/x-tar", + "gz": "application/gzip", + "exe": "application/x-msdownload", + "msi": "application/x-msi", + "deb": "application/x-debian-package", + "rpm": "application/x-redhat-package-manager", + "sh": "application/x-sh", + "bat": "application/bat", + "py": "application/x-python", + "java": "text/x-java-source", + "c": "text/x-csrc", + "cpp": "text/x-c++src", + "h": "text/x-chdr", + "hpp": "text/x-c++hdr", + "php": "application/x-php", + "asp": "application/x-asp", + "jsp": "application/x-jsp", + "dll": "application/x-msdownload", + "jar": "application/java-archive", + "war": "application/java-archive", + "ear": "application/java-archive", +} + +func GetType(val ...string) string { + for _, s := range val { + if len(s) > 1 { + if i := strings.IndexByte(s, '/'); i > 0 && i < len(s)-1 { + return s + } + if strings.HasPrefix(s, ".") { + s = s[1:] + } + if val, ok := ext[s]; ok { + return val + } + if val, ok := ext[strings.ToLower(s)]; ok { + return val + } + } + } + return "" +} From f60ad97128fa930d38eddc00b6a78444980fc041 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Fri, 21 Jul 2023 15:20:18 +0800 Subject: [PATCH 2/4] fix: msg file content type Signed-off-by: withchao <993506633@qq.com> --- internal/conversation_msg/sdk.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/conversation_msg/sdk.go b/internal/conversation_msg/sdk.go index 5a9b35954..b67fede8e 100644 --- a/internal/conversation_msg/sdk.go +++ b/internal/conversation_msg/sdk.go @@ -541,11 +541,18 @@ func (c *Conversation) SendMessage(ctx context.Context, s *sdk_struct.MsgStruct, s.Content = utils.StructToJsonString(s.FileElem) break } + name := s.FileElem.FileName + if name == "" { + name = s.FileElem.FilePath + } + if name == "" { + name = fmt.Sprintf("msg_file_%s.unknown", s.ClientMsgID) + } res, err := c.file.UploadFile(ctx, &file.UploadFileReq{ ContentType: content_type.GetType(s.FileElem.FileType, filepath.Ext(s.FileElem.FilePath), filepath.Ext(s.FileElem.FileName)), Filepath: s.FileElem.FilePath, Uuid: s.FileElem.UUID, - Name: c.fileName("file", s.ClientMsgID) + "/" + filepath.Base(s.FileElem.FileName), + Name: c.fileName("file", s.ClientMsgID) + "/" + filepath.Base(name), Cause: "msg-file", }, NewUploadFileCallback(ctx, callback.OnProgress, s, lc.ConversationID, c.db)) if err != nil { From cebfd4b14f4475c62b63d18a48a9aebf99ae530a Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Tue, 25 Jul 2023 19:56:32 +0800 Subject: [PATCH 3/4] fix: ws conn token error unprocessed Signed-off-by: withchao <993506633@qq.com> --- internal/interaction/long_conn_mgr.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/interaction/long_conn_mgr.go b/internal/interaction/long_conn_mgr.go index e8a181eae..a68a36a97 100644 --- a/internal/interaction/long_conn_mgr.go +++ b/internal/interaction/long_conn_mgr.go @@ -504,7 +504,20 @@ func (c *LongConnMgr) reConn(ctx context.Context, num *int) error { if err := json.Unmarshal(body, &apiResp); err != nil { return err } - c.listener.OnConnectFailed(int32(apiResp.ErrCode), apiResp.ErrMsg) + switch apiResp.ErrCode { + case + errs.TokenExpiredError, + errs.TokenInvalidError, + errs.TokenMalformedError, + errs.TokenNotValidYetError, + errs.TokenUnknownError, + errs.TokenKickedError, + errs.TokenNotExistError: + c.listener.OnUserTokenExpired() + _ = common.TriggerCmdLogOut(ctx, c.loginMgrCh) + default: + c.listener.OnConnectFailed(int32(apiResp.ErrCode), apiResp.ErrMsg) + } log.ZWarn(ctx, "long conn establish failed", sdkerrs.New(apiResp.ErrCode, apiResp.ErrMsg, apiResp.ErrDlt)) return errs.NewCodeError(apiResp.ErrCode, apiResp.ErrMsg).WithDetail(apiResp.ErrDlt).Wrap() } From d93ae4d2e19bf10117f13490fd99adeb1d52f672 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Wed, 26 Jul 2023 11:53:03 +0800 Subject: [PATCH 4/4] chore: optimize image parsing Signed-off-by: withchao <993506633@qq.com> --- go.mod | 4 +--- go.sum | 4 ---- internal/conversation_msg/image.go | 32 ++++++++++++++++++++++++++++++ internal/conversation_msg/sdk.go | 32 ------------------------------ pkg/content_type/content_type.go | 2 ++ 5 files changed, 35 insertions(+), 39 deletions(-) create mode 100644 internal/conversation_msg/image.go diff --git a/go.mod b/go.mod index fa5b45a41..423546bbf 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,6 @@ require ( github.com/mitchellh/mapstructure v1.5.0 github.com/pkg/errors v0.9.1 github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 - github.com/shamsher31/goimgtype v1.0.0 github.com/sirupsen/logrus v1.9.2 github.com/tencentyun/qcloud-cos-sts-sdk v0.0.0-20220106031843-2efeb10ca2f6 google.golang.org/protobuf v1.31.0 // indirect @@ -26,6 +25,7 @@ require golang.org/x/net v0.11.0 require ( github.com/OpenIMSDK/Open-IM-Server v0.0.0-20230712062720-2e6ea7b193c3 github.com/google/go-cmp v0.5.9 + golang.org/x/image v0.3.0 ) require ( @@ -39,7 +39,6 @@ require ( github.com/lestrrat-go/strftime v1.0.6 // indirect github.com/mattn/go-sqlite3 v1.14.12 // indirect github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect - github.com/shamsher31/goimgext v1.0.0 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/scram v1.0.2 // indirect github.com/xdg-go/stringprep v1.0.2 // indirect @@ -49,7 +48,6 @@ require ( go.uber.org/multierr v1.6.0 // indirect go.uber.org/zap v1.24.0 // indirect golang.org/x/crypto v0.10.0 // indirect - golang.org/x/image v0.3.0 // indirect golang.org/x/sync v0.1.0 // indirect golang.org/x/sys v0.9.0 // indirect golang.org/x/text v0.10.0 // indirect diff --git a/go.sum b/go.sum index 75e413fab..024ef3227 100644 --- a/go.sum +++ b/go.sum @@ -99,10 +99,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 h1:mZHayPoR0lNmnHyvtYjDeq0zlVHn9K/ZXoy17ylucdo= github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5/go.mod h1:GEXHk5HgEKCvEIIrSpFI3ozzG5xOKA2DVlEX/gGnewM= -github.com/shamsher31/goimgext v1.0.0 h1:wFKf9GeeE0Xr6UQtliaPgYYgTju2izobM7XpCEgUCC8= -github.com/shamsher31/goimgext v1.0.0/go.mod h1:rYLKgXuTGBIaH49z+jUVSWz7gUWIZmqvYUsdvJbNNOc= -github.com/shamsher31/goimgtype v1.0.0 h1:7nmPz5GEb01sjFUojCbZJcBI68A8tmLXoCFbb5I34u4= -github.com/shamsher31/goimgtype v1.0.0/go.mod h1:OZm3NZQDbK0ezlk9IV5oq3cWQwjF1oVSxKNx19bkf64= github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y= github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/internal/conversation_msg/image.go b/internal/conversation_msg/image.go new file mode 100644 index 000000000..9a0a503a1 --- /dev/null +++ b/internal/conversation_msg/image.go @@ -0,0 +1,32 @@ +package conversation_msg + +import ( + "github.com/OpenIMSDK/Open-IM-Server/pkg/errs" + _ "golang.org/x/image/bmp" + _ "golang.org/x/image/tiff" + _ "golang.org/x/image/webp" + "image" + _ "image/gif" + _ "image/jpeg" + _ "image/png" + "open_im_sdk/sdk_struct" + "os" +) + +func getImageInfo(filePath string) (*sdk_struct.ImageInfo, error) { + file, err := os.Open(filePath) + if err != nil { + return nil, errs.Wrap(err, "image file open err") + } + defer file.Close() + info, err := file.Stat() + if err != nil { + return nil, err + } + img, format, err := image.Decode(file) + if err != nil { + return nil, errs.Wrap(err, "image file decode err") + } + size := img.Bounds().Max + return &sdk_struct.ImageInfo{Width: int32(size.X), Height: int32(size.Y), Type: "image/" + format, Size: info.Size()}, nil +} diff --git a/internal/conversation_msg/sdk.go b/internal/conversation_msg/sdk.go index b67fede8e..0cf7ae0a7 100644 --- a/internal/conversation_msg/sdk.go +++ b/internal/conversation_msg/sdk.go @@ -18,7 +18,6 @@ import ( "bytes" "context" "fmt" - "image" "open_im_sdk/internal/file" "open_im_sdk/internal/util" "open_im_sdk/open_im_sdk_callback" @@ -47,7 +46,6 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/wrapperspb" "github.com/jinzhu/copier" - imgtype "github.com/shamsher31/goimgtype" ) func (c *Conversation) GetAllConversationList(ctx context.Context) ([]*model_struct.LocalConversation, error) { @@ -1048,36 +1046,6 @@ func (c *Conversation) SearchLocalMessages(ctx context.Context, searchParam *sdk func (c *Conversation) SetMessageLocalEx(ctx context.Context, conversationID string, clientMsgID string, localEx string) error { return c.db.UpdateColumnsMessage(ctx, conversationID, clientMsgID, map[string]interface{}{"local_ex": localEx}) } -func getImageInfo(filePath string) (*sdk_struct.ImageInfo, error) { - file, err := os.Open(filePath) - if err != nil { - return nil, utils.Wrap(err, "open file err") - } - defer func() { - if file != nil { - file.Close() - } - }() - - img, _, err := image.Decode(file) - if err != nil { - return nil, utils.Wrap(err, "image file Decode err") - } - - datatype, err := imgtype.Get(filePath) - if err != nil { - return nil, utils.Wrap(err, "image file get type err") - } - fi, err := os.Stat(filePath) - if err != nil { - return nil, utils.Wrap(err, "image file Stat err") - } - - b := img.Bounds() - - return &sdk_struct.ImageInfo{int32(b.Max.X), int32(b.Max.Y), datatype, fi.Size()}, nil - -} func (c *Conversation) initBasicInfo(ctx context.Context, message *sdk_struct.MsgStruct, msgFrom, contentType int32) error { message.CreateTime = utils.GetCurrentTimestampByMill() diff --git a/pkg/content_type/content_type.go b/pkg/content_type/content_type.go index 1fa25afab..76c1788bc 100644 --- a/pkg/content_type/content_type.go +++ b/pkg/content_type/content_type.go @@ -16,6 +16,8 @@ var ext = map[string]string{ "jpeg": "image/jpeg", "gif": "image/gif", "bmp": "image/bmp", + "tif": "image/tiff", + "tiff": "image/tiff", "ico": "image/x-icon", "svg": "image/svg+xml", "webp": "image/webp",