From 5edcd9e6e4c0b3488d4d4e3061929a0de55c6e7b Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Mon, 17 Jul 2023 17:08:31 +0800 Subject: [PATCH] refactor: upload file add content type --- internal/conversation_msg/create_message.go | 3 +++ internal/conversation_msg/sdk.go | 29 ++++++++++++--------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/internal/conversation_msg/create_message.go b/internal/conversation_msg/create_message.go index 0b4d13649..fb84237ca 100644 --- a/internal/conversation_msg/create_message.go +++ b/internal/conversation_msg/create_message.go @@ -23,6 +23,8 @@ import ( "open_im_sdk/pkg/utils" "open_im_sdk/sdk_struct" "os" + "path/filepath" + "strings" ) func (c *Conversation) CreateTextMessage(ctx context.Context, text string) (*sdk_struct.MsgStruct, error) { @@ -274,6 +276,7 @@ func (c *Conversation) CreateSoundMessageFromFullPath(ctx context.Context, sound SoundPath: soundPath, Duration: duration, DataSize: fi.Size(), + SoundType: strings.Replace(filepath.Ext(fi.Name()), ".", "", 1), } return &s, nil } diff --git a/internal/conversation_msg/sdk.go b/internal/conversation_msg/sdk.go index 960bc968f..71fbd0fbe 100644 --- a/internal/conversation_msg/sdk.go +++ b/internal/conversation_msg/sdk.go @@ -424,7 +424,7 @@ func (c *Conversation) SendMessage(ctx context.Context, s *sdk_struct.MsgStruct, res, err := c.file.UploadFile(ctx, &file.UploadFileReq{ //PutID: s.ClientMsgID, - ContentType: "image/jpeg", + ContentType: "image/" + s.PictureElem.SourcePicture.Type, Filepath: sourcePath, Uuid: s.PictureElem.SourcePicture.UUID, Name: c.fileName("picture", s.ClientMsgID) + filepath.Ext(sourcePath), @@ -463,10 +463,11 @@ func (c *Conversation) SendMessage(ctx context.Context, s *sdk_struct.MsgStruct, // log.Info("", "file", sourcePath, delFile) res, err := c.file.UploadFile(ctx, &file.UploadFileReq{ - Filepath: sourcePath, - Uuid: s.SoundElem.UUID, - Name: c.fileName("voice", s.ClientMsgID) + filepath.Ext(sourcePath), - Cause: "msg-voice", + ContentType: "audio/" + s.SoundElem.SoundType, + Filepath: sourcePath, + Uuid: s.SoundElem.UUID, + Name: c.fileName("voice", s.ClientMsgID) + filepath.Ext(sourcePath), + Cause: "msg-voice", }, NewUploadFileCallback(ctx, callback.OnProgress, s, lc.ConversationID, c.db)) if err != nil { c.updateMsgStatusAndTriggerConversation(ctx, s.ClientMsgID, "", s.CreateTime, constant.MsgStatusSendFailed, s, lc) @@ -501,10 +502,11 @@ func (c *Conversation) SendMessage(ctx context.Context, s *sdk_struct.MsgStruct, go func() { defer wg.Done() snapRes, err := c.file.UploadFile(ctx, &file.UploadFileReq{ - Filepath: snapPath, - Uuid: s.VideoElem.SnapshotUUID, - Name: c.fileName("videoSnapshot", s.ClientMsgID) + filepath.Ext(snapPath), - Cause: "msg-video-snapshot", + ContentType: "image/" + s.VideoElem.SnapshotType, + Filepath: snapPath, + Uuid: s.VideoElem.SnapshotUUID, + Name: c.fileName("videoSnapshot", s.ClientMsgID) + filepath.Ext(snapPath), + Cause: "msg-video-snapshot", }, nil) if err != nil { c.updateMsgStatusAndTriggerConversation(ctx, s.ClientMsgID, "", s.CreateTime, constant.MsgStatusSendFailed, s, lc) @@ -518,10 +520,11 @@ func (c *Conversation) SendMessage(ctx context.Context, s *sdk_struct.MsgStruct, go func() { defer wg.Done() res, err := c.file.UploadFile(ctx, &file.UploadFileReq{ - Filepath: videoPath, - Uuid: s.VideoElem.VideoUUID, - Name: c.fileName("video", s.ClientMsgID) + filepath.Ext(videoPath), - Cause: "msg-video", + ContentType: "video/" + s.VideoElem.VideoType, + Filepath: videoPath, + Uuid: s.VideoElem.VideoUUID, + Name: c.fileName("video", s.ClientMsgID) + filepath.Ext(videoPath), + Cause: "msg-video", }, NewUploadFileCallback(ctx, callback.OnProgress, s, lc.ConversationID, c.db)) if err != nil { c.updateMsgStatusAndTriggerConversation(ctx, s.ClientMsgID, "", s.CreateTime, constant.MsgStatusSendFailed, s, lc)