Skip to content

Commit dc5f874

Browse files
committed
Merge branch 'tuoyun'
2 parents d6372d6 + d841d63 commit dc5f874

File tree

113 files changed

+18663
-13062
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+18663
-13062
lines changed

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.PHONY: ios build install
22

3-
BINARY_NAME=open_im_sdk/ws_wrapper/open_im_sdk_server
3+
BINARY_NAME=ws_wrapper/cmd/open_im_sdk_server
44
BIN_DIR=../../bin/
55
LAN_FILE=.go
66
GO_FILE:=${BINARY_NAME}${LAN_FILE}
@@ -20,7 +20,8 @@ reset_remote_branch:
2020
git reset --hard $(remote_branch)
2121
git pull $(remote_branch)
2222

23-
ios: reset_remote_branch
23+
ios:
2424
go get golang.org/x/mobile
25-
rm -rf build/ open_im_sdk/t_friend_sdk.go
26-
GOARCH=arm64 gomobile bind -v -trimpath -ldflags "-s -w" -o build/OpenIMCore.xcframework -target=ios ./open_im_sdk/
25+
rm -rf build/ open_im_sdk/t_friend_sdk.go open_im_sdk/t_group_sdk.go open_im_sdk/ws_wrapper/
26+
go mod download golang.org/x/exp
27+
GOARCH=arm64 gomobile bind -v -trimpath -ldflags "-s -w" -o build/OpenIMCore.xcframework -target=ios ./open_im_sdk/ ./open_im_sdk_callback/

go.mod

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,34 @@ module open_im_sdk
33
go 1.15
44

55
require (
6+
github.com/antonfisher/nested-logrus-formatter v1.3.1
7+
github.com/fatih/structs v1.1.0 // indirect
8+
github.com/go-ini/ini v1.66.4 // indirect
9+
github.com/go-playground/validator/v10 v10.10.0
610
github.com/golang/protobuf v1.5.2
7-
github.com/google/uuid v1.1.2 // indirect
11+
github.com/google/go-cmp v0.5.5
812
github.com/gorilla/websocket v1.4.2
9-
github.com/mattn/go-sqlite3 v1.14.7
10-
github.com/minio/minio-go/v6 v6.0.57
13+
github.com/jinzhu/copier v0.3.4
14+
github.com/jonboulle/clockwork v0.2.2 // indirect
15+
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
16+
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
17+
github.com/lestrrat-go/strftime v1.0.5 // indirect
18+
github.com/minio/minio-go v6.0.14+incompatible // indirect
19+
github.com/minio/minio-go/v7 v7.0.22 // indirect
20+
github.com/mitchellh/go-homedir v1.1.0 // indirect
21+
github.com/mitchellh/mapstructure v1.4.3
22+
github.com/pkg/errors v0.9.1
23+
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
1124
github.com/shamsher31/goimgext v1.0.0 // indirect
1225
github.com/shamsher31/goimgtype v1.0.0
13-
github.com/stretchr/testify v1.7.0 // indirect
14-
github.com/tencentyun/cos-go-sdk-v5 v0.7.25
15-
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
16-
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
17-
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
26+
github.com/sirupsen/logrus v1.8.1
27+
github.com/tencentyun/cos-go-sdk-v5 v0.7.33
28+
github.com/tencentyun/qcloud-cos-sts-sdk v0.0.0-20220106031843-2efeb10ca2f6
29+
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
30+
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
31+
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
32+
golang.org/x/text v0.3.7 // indirect
1833
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
34+
gorm.io/driver/sqlite v1.2.6
35+
gorm.io/gorm v1.22.4
1936
)

go.sum

Lines changed: 131 additions & 40 deletions
Large diffs are not rendered by default.

internal/common/common.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package common
2+
3+
import (
4+
"github.com/golang/protobuf/proto"
5+
api "open_im_sdk/pkg/server_api_params"
6+
"open_im_sdk/pkg/utils"
7+
)
8+
9+
func UnmarshalTips(msg *api.MsgData, detail proto.Message) error {
10+
var tips api.TipsComm
11+
if err := proto.Unmarshal(msg.Content, &tips); err != nil {
12+
return utils.Wrap(err, "")
13+
}
14+
if err := proto.Unmarshal(tips.Detail, detail); err != nil {
15+
return utils.Wrap(err, "")
16+
}
17+
return nil
18+
}

internal/common/cos.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package common
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"open_im_sdk/pkg/log"
8+
9+
"open_im_sdk/pkg/constant"
10+
11+
"github.com/tencentyun/cos-go-sdk-v5"
12+
"math/rand"
13+
"net/http"
14+
"net/url"
15+
ws "open_im_sdk/internal/interaction"
16+
// "open_im_sdk/pkg/constant"
17+
"open_im_sdk/pkg/server_api_params"
18+
"open_im_sdk/pkg/utils"
19+
"path"
20+
"time"
21+
)
22+
23+
type COS struct {
24+
p *ws.PostApi
25+
}
26+
27+
func NewCOS(p *ws.PostApi) *COS {
28+
return &COS{p: p}
29+
}
30+
31+
func (c *COS) tencentCOSCredentials() (*server_api_params.TencentCloudStorageCredentialRespData, error) {
32+
req := server_api_params.TencentCloudStorageCredentialReq{OperationID: utils.OperationIDGenerator()}
33+
var resp server_api_params.TencentCloudStorageCredentialResp
34+
err := c.p.PostReturn(constant.TencentCloudStorageCredentialRouter, req, &resp.CosData)
35+
if err != nil {
36+
return nil, utils.Wrap(err, "")
37+
}
38+
return &resp.CosData, nil
39+
}
40+
41+
func (c *COS) UploadImage(filePath string, onProgressFun func(int)) (string, string, error) {
42+
return c.uploadObj(filePath, "img", onProgressFun)
43+
44+
}
45+
46+
func (c *COS) UploadSound(filePath string, onProgressFun func(int)) (string, string, error) {
47+
return c.uploadObj(filePath, "", onProgressFun)
48+
}
49+
50+
func (c *COS) UploadFile(filePath string, onProgressFun func(int)) (string, string, error) {
51+
return c.uploadObj(filePath, "", onProgressFun)
52+
}
53+
54+
func (c *COS) UploadVideo(videoPath, snapshotPath string, onProgressFun func(int)) (string, string, string, string, error) {
55+
videoURL, videoUUID, err := c.uploadObj(videoPath, "", onProgressFun)
56+
if err != nil {
57+
return "", "", "", "", utils.Wrap(err, "")
58+
}
59+
snapshotURL, snapshotUUID, err := c.uploadObj(snapshotPath, "img", onProgressFun)
60+
if err != nil {
61+
return "", "", "", "", utils.Wrap(err, "")
62+
}
63+
return snapshotURL, snapshotUUID, videoURL, videoUUID, nil
64+
}
65+
66+
func (c *COS) getNewFileNameAndContentType(filePath string, fileType string) (string, string, error) {
67+
suffix := path.Ext(filePath)
68+
if len(suffix) == 0 {
69+
return "", "", utils.Wrap(errors.New("no suffix "), filePath)
70+
}
71+
newName := fmt.Sprintf("%d-%d%s", time.Now().UnixNano(), rand.Int(), suffix)
72+
contentType := ""
73+
if fileType == "img" {
74+
contentType = "image/" + suffix[1:]
75+
}
76+
return newName, contentType, nil
77+
}
78+
79+
func (c *COS) uploadObj(filePath string, fileType string, onProgressFun func(int)) (string, string, error) {
80+
COSResp, err := c.tencentCOSCredentials()
81+
log.Info("upload ", COSResp.Credentials.SessionToken, "bucket ", COSResp.Credentials.TmpSecretID)
82+
if err != nil {
83+
return "", "", utils.Wrap(err, "")
84+
}
85+
dir := fmt.Sprintf("https://%s.cos.%s.myqcloud.com", COSResp.Bucket, COSResp.Region)
86+
u, _ := url.Parse(dir)
87+
b := &cos.BaseURL{BucketURL: u}
88+
89+
client := cos.NewClient(b, &http.Client{
90+
Transport: &cos.AuthorizationTransport{
91+
SecretID: COSResp.Credentials.TmpSecretID,
92+
SecretKey: COSResp.Credentials.TmpSecretKey,
93+
SessionToken: COSResp.Credentials.SessionToken,
94+
},
95+
})
96+
if client == nil {
97+
err := errors.New("client == nil")
98+
return "", "", utils.Wrap(err, "")
99+
}
100+
101+
newName, contentType, err := c.getNewFileNameAndContentType(filePath, fileType)
102+
if err != nil {
103+
return "", "", utils.Wrap(err, "")
104+
}
105+
var lis = &selfListener{}
106+
lis.onProgressFun = onProgressFun
107+
opt := &cos.ObjectPutOptions{ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{Listener: lis}}
108+
if fileType == "img" {
109+
opt.ContentType = contentType
110+
}
111+
_, err = client.Object.PutFromFile(context.Background(), newName, filePath, opt)
112+
if err != nil {
113+
return "", "", utils.Wrap(err, "")
114+
}
115+
return dir + "/" + newName, newName, nil
116+
}
117+
118+
type selfListener struct {
119+
onProgressFun func(int)
120+
}
121+
122+
func (l *selfListener) ProgressChangedCallback(event *cos.ProgressEvent) {
123+
switch event.EventType {
124+
case cos.ProgressDataEvent:
125+
if event.ConsumedBytes == event.TotalBytes {
126+
if l.onProgressFun != nil {
127+
l.onProgressFun(int((event.ConsumedBytes - 1) * 100 / event.TotalBytes))
128+
}
129+
130+
} else {
131+
if l.onProgressFun != nil {
132+
l.onProgressFun(int(event.ConsumedBytes * 100 / event.TotalBytes))
133+
}
134+
}
135+
case cos.ProgressFailedEvent:
136+
}
137+
}

internal/common/minio.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package common
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
minio "github.com/minio/minio-go/v7"
8+
"github.com/minio/minio-go/v7/pkg/credentials"
9+
"math/rand"
10+
"net/url"
11+
_"net/url"
12+
ws "open_im_sdk/internal/interaction"
13+
"open_im_sdk/pkg/constant"
14+
"open_im_sdk/pkg/log"
15+
"open_im_sdk/pkg/server_api_params"
16+
"open_im_sdk/pkg/utils"
17+
"path"
18+
"time"
19+
)
20+
21+
22+
23+
type Minio struct {
24+
p *ws.PostApi
25+
}
26+
27+
func NewMinio(p *ws.PostApi) *Minio {
28+
return &Minio{p:p}
29+
}
30+
31+
32+
func (m *Minio) getMinioCredentials() (*server_api_params.MinioStorageCredentialResp, error){
33+
req := server_api_params.MinioStorageCredentialReq{OperationID: utils.OperationIDGenerator()}
34+
var resp server_api_params.MinioStorageCredentialResp
35+
err := m.p.PostReturn(constant.MinioStorageCredentialRouter, req, &resp)
36+
if err != nil {
37+
log.NewError("0", utils.GetSelfFuncName(), err.Error())
38+
return &resp, utils.Wrap(err, "")
39+
}
40+
return &resp, nil
41+
}
42+
43+
func (m *Minio) upload(filePath, fileType string, onProgressFun func(int)) (string, string, error){
44+
minioResp, err := m.getMinioCredentials()
45+
endPoint, err := url.Parse(minioResp.StsEndpointURL)
46+
if err != nil {
47+
log.NewError("", utils.GetSelfFuncName(), "url parse failed", err.Error())
48+
return "", "", err
49+
}
50+
if err != nil {
51+
log.NewError("0", utils.GetSelfFuncName(), "new minio client failed", err.Error())
52+
}
53+
newName, newType, err := m.getNewFileNameAndContentType(filePath, fileType)
54+
client, err := minio.New(endPoint.Host, &minio.Options{
55+
Creds: credentials.NewStaticV4(minioResp.AccessKeyID, minioResp.SecretAccessKey, minioResp.SessionToken),
56+
Secure: false,
57+
})
58+
if err != nil {
59+
log.NewError("", utils.GetSelfFuncName(), "generate filename and filetype failed", err.Error())
60+
return "", "", utils.Wrap(err, "")
61+
}
62+
_, err = client.FPutObject(context.Background(), minioResp.BucketName, newName, filePath, minio.PutObjectOptions{ContentType:newType})
63+
if err != nil {
64+
log.NewError("0", utils.GetSelfFuncName(), "FPutObject failed", err.Error())
65+
return "", "", utils.Wrap(err, "")
66+
}
67+
// fake callback
68+
onProgressFun(100)
69+
reqParams := make(url.Values)
70+
log.NewInfo("", reqParams)
71+
72+
presignedURL := endPoint.String() + "/" + minioResp.BucketName + "/" + newName
73+
log.NewInfo("", presignedURL)
74+
return presignedURL, newName, nil
75+
}
76+
77+
78+
func (m *Minio) getNewFileNameAndContentType(filePath string, fileType string) (string, string, error) {
79+
suffix := path.Ext(filePath)
80+
if len(suffix) == 0 {
81+
return "", "", utils.Wrap(errors.New("no suffix "), filePath)
82+
}
83+
newName := fmt.Sprintf("%d-%d%s", time.Now().UnixNano(), rand.Int(), suffix)
84+
contentType := ""
85+
if fileType == "img" {
86+
contentType = "image/" + suffix[1:]
87+
}
88+
return newName, contentType, nil
89+
}
90+
91+
func (m *Minio) UploadImage(filePath string, onProgressFun func(int)) (string, string, error) {
92+
return m.upload(filePath, "img ", onProgressFun)
93+
}
94+
95+
96+
func (m *Minio) UploadSound(filePath string, onProgressFun func(int)) (string, string, error) {
97+
return m.upload(filePath, "", onProgressFun)
98+
}
99+
100+
func (m *Minio) UploadFile(filePath string, onProgressFun func(int)) (string, string, error) {
101+
return m.upload(filePath, "", onProgressFun)
102+
}
103+
104+
func (m *Minio) UploadVideo(videoPath, snapshotPath string, onProgressFun func(int)) (string, string, string, string, error) {
105+
videoURL, videoName, err := m.upload(videoPath, "", onProgressFun)
106+
snapshotURL, snapshotUUID, err := m.upload(snapshotPath, "img", onProgressFun)
107+
if err != nil {
108+
return snapshotURL, snapshotUUID, videoURL, videoName, utils.Wrap(err, "")
109+
}
110+
return snapshotURL, snapshotUUID, videoURL, videoName, nil
111+
}
112+

internal/common/object_storage.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package common
2+
3+
import (
4+
_ "image/gif"
5+
_ "image/jpeg"
6+
_ "image/png"
7+
)
8+
9+
type ObjectStorage interface {
10+
UploadImage(filePath string, onProgressFun func(int)) (string, string, error)
11+
UploadSound(filePath string, onProgressFun func(int)) (string, string, error)
12+
UploadFile(filePath string, onProgressFun func(int)) (string, string, error)
13+
UploadVideo(videoPath, snapshotPath string, onProgressFun func(int)) (string, string, string, string, error)
14+
}

0 commit comments

Comments
 (0)