Skip to content

Commit

Permalink
refactor: upload file add content type
Browse files Browse the repository at this point in the history
  • Loading branch information
FGadvancer committed Jul 17, 2023
1 parent 79d3843 commit 5edcd9e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
3 changes: 3 additions & 0 deletions internal/conversation_msg/create_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
29 changes: 16 additions & 13 deletions internal/conversation_msg/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 5edcd9e

Please sign in to comment.