feat: incr sync version.
This commit is contained in:
85
go/chao-sdk-core/internal/third/log.go
Normal file
85
go/chao-sdk-core/internal/third/log.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package third
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/openimsdk/openim-sdk-core/v3/internal/file"
|
||||
"github.com/openimsdk/openim-sdk-core/v3/internal/util"
|
||||
"github.com/openimsdk/openim-sdk-core/v3/pkg/ccontext"
|
||||
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
|
||||
"github.com/openimsdk/protocol/third"
|
||||
"io"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (c *Third) UploadLogs(ctx context.Context, progress Progress) error {
|
||||
logFilePath := c.LogFilePath
|
||||
entrys, err := os.ReadDir(logFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
files := make([]string, 0, len(entrys))
|
||||
for _, entry := range entrys {
|
||||
if (!entry.IsDir()) && (!strings.HasSuffix(entry.Name(), ".zip")) && checkLogPath(entry.Name()) {
|
||||
files = append(files, filepath.Join(logFilePath, entry.Name()))
|
||||
}
|
||||
}
|
||||
if len(files) == 0 {
|
||||
return errors.New("not found log file")
|
||||
}
|
||||
zippath := filepath.Join(logFilePath, fmt.Sprintf("%d_%d.zip", time.Now().UnixMilli(), rand.Uint32()))
|
||||
defer os.Remove(zippath)
|
||||
if err := zipFiles(zippath, files); err != nil {
|
||||
return err
|
||||
}
|
||||
reqUpload := &file.UploadFileReq{Filepath: zippath, Name: fmt.Sprintf("sdk_log_%s_%s", c.loginUserID, filepath.Base(zippath)), Cause: "sdklog", ContentType: "application/zip"}
|
||||
resp, err := c.fileUploader.UploadFile(ctx, reqUpload, &progressConvert{ctx: ctx, p: progress})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ccontext.Info(ctx)
|
||||
reqLog := &third.UploadLogsReq{
|
||||
Platform: c.platformID,
|
||||
SystemType: c.systemType,
|
||||
Version: c.version,
|
||||
FileURLs: []*third.FileURL{{Filename: zippath, URL: resp.URL}},
|
||||
}
|
||||
_, err = util.CallApi[third.UploadLogsResp](ctx, constant.UploadLogsRouter, reqLog)
|
||||
return err
|
||||
}
|
||||
|
||||
func checkLogPath(logPath string) bool {
|
||||
if len(logPath) < len("open-im-sdk-core.yyyy-mm-dd") {
|
||||
return false
|
||||
}
|
||||
logTime := logPath[len(logPath)-len(".yyyy-mm-dd"):]
|
||||
if _, err := time.Parse(".2006-01-02", logTime); err != nil {
|
||||
return false
|
||||
}
|
||||
if !strings.HasPrefix(logPath, "open-im-sdk-core.") {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *Third) fileCopy(src, dst string) error {
|
||||
_ = os.RemoveAll(dst)
|
||||
srcFile, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer srcFile.Close()
|
||||
dstFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dstFile.Close()
|
||||
_, err = io.Copy(dstFile, srcFile)
|
||||
return err
|
||||
}
|
||||
74
go/chao-sdk-core/internal/third/log_test.go
Normal file
74
go/chao-sdk-core/internal/third/log_test.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package third
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLogMatch(t *testing.T) {
|
||||
|
||||
filenames := []string{
|
||||
"log1.txt",
|
||||
"log2.log",
|
||||
"log3.log.txt",
|
||||
"log4.log.2022-01-01",
|
||||
"log5.log.2022-01-01.txt",
|
||||
"log20230918.log",
|
||||
"OpenIM.CronTask.log.all.2023-09-18", "OpenIM.log.all.2023-09-18",
|
||||
}
|
||||
|
||||
expected := []string{
|
||||
"OpenIM.CronTask.log.all.2023-09-18", "OpenIM.log.all.2023-09-18",
|
||||
}
|
||||
|
||||
var actual []string
|
||||
for _, filename := range filenames {
|
||||
if checkLogPath(filename) {
|
||||
actual = append(actual, filename)
|
||||
}
|
||||
}
|
||||
|
||||
if len(actual) != len(expected) {
|
||||
t.Errorf("Expected %d matches, but got %d", len(expected), len(actual))
|
||||
}
|
||||
|
||||
for i := range expected {
|
||||
if actual[i] != expected[i] {
|
||||
t.Errorf("Expected match %d to be %q, but got %q", i, expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
dir := `C:\Users\openIM\Desktop\testlog`
|
||||
|
||||
dirs, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, entry := range dirs {
|
||||
if !entry.IsDir() {
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(entry.Name(), info.Size(), info.ModTime())
|
||||
}
|
||||
}
|
||||
|
||||
if true {
|
||||
return
|
||||
}
|
||||
|
||||
files := []string{
|
||||
//filepath.Join(dir, "open-im-sdk-core.2023-10-13"),
|
||||
filepath.Join(dir, "open-im-sdk-core.2023-11-15"),
|
||||
//filepath.Join(dir, "open-im-sdk-core.2023-11-17"),
|
||||
}
|
||||
|
||||
if err := zipFiles(filepath.Join(dir, "test1.zip"), files); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
37
go/chao-sdk-core/internal/third/progress.go
Normal file
37
go/chao-sdk-core/internal/third/progress.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package third
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Progress interface {
|
||||
OnProgress(current int64, size int64)
|
||||
}
|
||||
|
||||
type progressConvert struct {
|
||||
ctx context.Context
|
||||
p Progress
|
||||
}
|
||||
|
||||
func (p *progressConvert) Open(size int64) {
|
||||
p.p.OnProgress(0, size)
|
||||
}
|
||||
|
||||
func (p *progressConvert) PartSize(partSize int64, num int) {}
|
||||
|
||||
func (p *progressConvert) HashPartProgress(index int, size int64, partHash string) {}
|
||||
|
||||
func (p *progressConvert) HashPartComplete(partsHash string, fileHash string) {}
|
||||
|
||||
func (p *progressConvert) UploadID(uploadID string) {}
|
||||
|
||||
func (p *progressConvert) UploadPartComplete(index int, partSize int64, partHash string) {}
|
||||
|
||||
func (p *progressConvert) UploadComplete(fileSize int64, streamSize int64, storageSize int64) {
|
||||
//log.ZDebug(p.ctx, "upload log progress", "fileSize", fileSize, "current", streamSize)
|
||||
p.p.OnProgress(streamSize, fileSize)
|
||||
}
|
||||
|
||||
func (p *progressConvert) Complete(size int64, url string, typ int) {
|
||||
p.p.OnProgress(size, size)
|
||||
}
|
||||
57
go/chao-sdk-core/internal/third/third.go
Normal file
57
go/chao-sdk-core/internal/third/third.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// Copyright © 2023 OpenIM SDK. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package third
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/openimsdk/openim-sdk-core/v3/internal/util"
|
||||
"github.com/openimsdk/openim-sdk-core/v3/pkg/constant"
|
||||
"github.com/openimsdk/protocol/third"
|
||||
|
||||
"github.com/openimsdk/openim-sdk-core/v3/internal/file"
|
||||
)
|
||||
|
||||
type Third struct {
|
||||
platformID int32
|
||||
loginUserID string
|
||||
version string
|
||||
systemType string
|
||||
LogFilePath string
|
||||
fileUploader *file.File
|
||||
}
|
||||
|
||||
func NewThird(platformID int32, loginUserID, version, systemType, LogFilePath string, fileUploader *file.File) *Third {
|
||||
return &Third{platformID: platformID, loginUserID: loginUserID, version: version, systemType: systemType, LogFilePath: LogFilePath, fileUploader: fileUploader}
|
||||
}
|
||||
|
||||
func (c *Third) UpdateFcmToken(ctx context.Context, fcmToken string, expireTime int64) error {
|
||||
req := third.FcmUpdateTokenReq{
|
||||
PlatformID: c.platformID,
|
||||
FcmToken: fcmToken,
|
||||
Account: c.loginUserID,
|
||||
ExpireTime: expireTime}
|
||||
_, err := util.CallApi[third.FcmUpdateTokenResp](ctx, constant.FcmUpdateTokenRouter, &req)
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
func (c *Third) SetAppBadge(ctx context.Context, appUnreadCount int32) error {
|
||||
req := third.SetAppBadgeReq{
|
||||
UserID: c.loginUserID,
|
||||
AppUnreadCount: appUnreadCount,
|
||||
}
|
||||
_, err := util.CallApi[third.SetAppBadgeResp](ctx, constant.SetAppBadgeRouter, &req)
|
||||
return err
|
||||
}
|
||||
75
go/chao-sdk-core/internal/third/zip.go
Normal file
75
go/chao-sdk-core/internal/third/zip.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package third
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func (c *Third) addFileToZip(zipWriter *zip.Writer, filename string) error {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
info, err := file.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
header, err := zip.FileInfoHeader(info)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
header.Name = filepath.Base(filename)
|
||||
header.Method = zip.Deflate
|
||||
writer, err := zipWriter.CreateHeader(header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = io.Copy(writer, io.LimitReader(file, info.Size()))
|
||||
return err
|
||||
}
|
||||
|
||||
func zipFiles(zipPath string, files []string) error {
|
||||
zipFile, err := os.Create(zipPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer zipFile.Close()
|
||||
zipWriter := zip.NewWriter(zipFile)
|
||||
defer zipWriter.Close()
|
||||
addFileToZip := func(fp string) error {
|
||||
file, err := os.Open(fp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
info, err := file.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
header, err := zip.FileInfoHeader(info)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
header.Name = filepath.Base(file.Name())
|
||||
header.Method = zip.Deflate
|
||||
writer, err := zipWriter.CreateHeader(header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = io.Copy(writer, io.LimitReader(file, info.Size()))
|
||||
return err
|
||||
}
|
||||
for _, file := range files {
|
||||
err := addFileToZip(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := zipWriter.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user