This commit is contained in:
Liang Ding 2022-11-02 18:21:47 +08:00
parent ba8d07b3d0
commit 272db50b88
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
5 changed files with 38 additions and 49 deletions

View file

@ -21,7 +21,6 @@ import (
"net/http" "net/http"
"github.com/88250/gulu" "github.com/88250/gulu"
"github.com/dustin/go-humanize"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/model" "github.com/siyuan-note/siyuan/kernel/model"
"github.com/siyuan-note/siyuan/kernel/util" "github.com/siyuan-note/siyuan/kernel/util"
@ -31,7 +30,7 @@ func getCloudSpace(c *gin.Context) {
ret := gulu.Ret.NewResult() ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret) defer c.JSON(http.StatusOK, ret)
sync, backup, size, assetSize, totalSize, err := model.GetCloudSpace() sync, backup, size, assetSize, totalSize, hTrafficUploadSize, hTrafficDownloadSize, err := model.GetCloudSpace()
if nil != err { if nil != err {
ret.Code = 1 ret.Code = 1
ret.Msg = err.Error() ret.Msg = err.Error()
@ -39,9 +38,6 @@ func getCloudSpace(c *gin.Context) {
return return
} }
hTrafficUploadSize := humanize.Bytes(uint64(model.Conf.User.UserTrafficUpload))
hTrafficDownloadSize := humanize.Bytes(uint64(model.Conf.User.UserTrafficDownload))
ret.Data = map[string]interface{}{ ret.Data = map[string]interface{}{
"sync": sync, "sync": sync,
"backup": backup, "backup": backup,

View file

@ -36,7 +36,7 @@ require (
github.com/panjf2000/ants/v2 v2.6.0 github.com/panjf2000/ants/v2 v2.6.0
github.com/patrickmn/go-cache v2.1.0+incompatible github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/radovskyb/watcher v1.0.7 github.com/radovskyb/watcher v1.0.7
github.com/siyuan-note/dejavu v0.0.0-20221102075801-148594bd43e2 github.com/siyuan-note/dejavu v0.0.0-20221102101918-9258bfd2c7ca
github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75 github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75
github.com/siyuan-note/eventbus v0.0.0-20220916025349-3ac6e75522da github.com/siyuan-note/eventbus v0.0.0-20220916025349-3ac6e75522da
github.com/siyuan-note/filelock v0.0.0-20221007163134-7e64809023ef github.com/siyuan-note/filelock v0.0.0-20221007163134-7e64809023ef
@ -69,7 +69,7 @@ require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/glog v1.0.0 // indirect github.com/golang/glog v1.0.0 // indirect
github.com/golang/mock v1.6.0 // indirect github.com/golang/mock v1.6.0 // indirect
github.com/google/pprof v0.0.0-20221010195024-131d412537ea // indirect github.com/google/pprof v0.0.0-20221102093814-76f304f74e5e // indirect
github.com/google/uuid v1.3.0 // indirect github.com/google/uuid v1.3.0 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/gorilla/context v1.1.1 // indirect github.com/gorilla/context v1.1.1 // indirect

View file

@ -158,8 +158,8 @@ github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20221010195024-131d412537ea h1:R3VfsTXMMK4JCWZDdxScmnTzu9n9YRsDvguLis0U/b8= github.com/google/pprof v0.0.0-20221102093814-76f304f74e5e h1:F1LLQqQ8WoIbyoxLUY+JUZe1kuHdxThM6CPUATzE6Io=
github.com/google/pprof v0.0.0-20221010195024-131d412537ea/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo= github.com/google/pprof v0.0.0-20221102093814-76f304f74e5e/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@ -353,8 +353,8 @@ github.com/shurcooL/reactions v0.0.0-20181006231557-f2e0b4ca5b82/go.mod h1:TCR1l
github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYEDaXHZDBsXlPCDqdhQuJkuw4NOtaxYe3xii4= github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYEDaXHZDBsXlPCDqdhQuJkuw4NOtaxYe3xii4=
github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw= github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw=
github.com/siyuan-note/dejavu v0.0.0-20221102075801-148594bd43e2 h1:EjflBUJdr+vE56QHL/CaDGIiMmXl6AJLc0Ged9yVDL8= github.com/siyuan-note/dejavu v0.0.0-20221102101918-9258bfd2c7ca h1:yxlwyy/GxuzIBhTadAxjXNGAJYc3yrTjX8Yqq1SlVg4=
github.com/siyuan-note/dejavu v0.0.0-20221102075801-148594bd43e2/go.mod h1:+U86jfsvpacZBThE3Ouf/ZQ4EsB4jGPJsMO2iuRv0LQ= github.com/siyuan-note/dejavu v0.0.0-20221102101918-9258bfd2c7ca/go.mod h1:+U86jfsvpacZBThE3Ouf/ZQ4EsB4jGPJsMO2iuRv0LQ=
github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75 h1:Bi7/7f29LW+Fm0cHc0J1NO1cZqyJwljSWVmfOqVZgaE= github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75 h1:Bi7/7f29LW+Fm0cHc0J1NO1cZqyJwljSWVmfOqVZgaE=
github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75/go.mod h1:H8fyqqAbp9XreANjeSbc72zEdFfKTXYN34tc1TjZwtw= github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75/go.mod h1:H8fyqqAbp9XreANjeSbc72zEdFfKTXYN34tc1TjZwtw=
github.com/siyuan-note/eventbus v0.0.0-20220916025349-3ac6e75522da h1:/jNhl7LC+9BhkWvNxuJDdsNfA/2wvfuj9mqWx4CbV90= github.com/siyuan-note/eventbus v0.0.0-20220916025349-3ac6e75522da h1:/jNhl7LC+9BhkWvNxuJDdsNfA/2wvfuj9mqWx4CbV90=

View file

@ -245,6 +245,15 @@ func InitConf() {
if nil == Conf.Sync.OSS { if nil == Conf.Sync.OSS {
Conf.Sync.OSS = &conf.OSS{} Conf.Sync.OSS = &conf.OSS{}
} }
endpoint := Conf.Sync.OSS.Endpoint
endpoint = strings.TrimSpace(endpoint)
if !strings.HasPrefix(endpoint, "http://") && !strings.HasPrefix(endpoint, "https://") {
endpoint = "http://" + endpoint
}
if !strings.HasSuffix(endpoint, "/") {
endpoint = endpoint + "/"
}
Conf.Sync.OSS.Endpoint = endpoint
if nil == Conf.Api { if nil == Conf.Api {
Conf.Api = conf.NewAPI() Conf.Api = conf.NewAPI()

View file

@ -38,7 +38,6 @@ import (
"github.com/siyuan-note/dejavu/entity" "github.com/siyuan-note/dejavu/entity"
"github.com/siyuan-note/encryption" "github.com/siyuan-note/encryption"
"github.com/siyuan-note/eventbus" "github.com/siyuan-note/eventbus"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging" "github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/cache" "github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/conf" "github.com/siyuan-note/siyuan/kernel/conf"
@ -1054,69 +1053,54 @@ type Sync struct {
SaveDir string `json:"saveDir"` // 本地同步数据存放目录路径 SaveDir string `json:"saveDir"` // 本地同步数据存放目录路径
} }
func GetCloudSpace() (s *Sync, b *Backup, hSize, hAssetSize, hTotalSize string, err error) { func GetCloudSpace() (s *Sync, b *Backup, hSize, hAssetSize, hTotalSize, hTrafficUploadSize, hTrafficDownloadSize string, err error) {
sync, backup, assetSize, err := getCloudSpaceOSS() stat, err := getCloudSpaceOSS()
if nil != err { if nil != err {
err = errors.New(Conf.Language(30) + " " + err.Error()) err = errors.New(Conf.Language(30) + " " + err.Error())
return return
} }
var totalSize, syncSize, backupSize int64 syncSize := stat.Sync.Size
var syncUpdated, backupUpdated string syncUpdated := stat.Sync.Updated
if nil != sync {
syncSize = int64(sync["size"].(float64))
syncUpdated = sync["updated"].(string)
}
s = &Sync{ s = &Sync{
Size: syncSize, Size: syncSize,
HSize: humanize.Bytes(uint64(syncSize)), HSize: humanize.Bytes(uint64(syncSize)),
Updated: syncUpdated, Updated: syncUpdated,
} }
if nil != backup { backupSize := stat.Backup.Size
backupSize = int64(backup["size"].(float64)) backupUpdated := stat.Backup.Updated
backupUpdated = backup["updated"].(string)
}
b = &Backup{ b = &Backup{
Size: backupSize, Size: backupSize,
HSize: humanize.Bytes(uint64(backupSize)), HSize: humanize.Bytes(uint64(backupSize)),
Updated: backupUpdated, Updated: backupUpdated,
} }
totalSize = syncSize + backupSize + assetSize
assetSize := stat.AssetSize
totalSize := syncSize + backupSize + assetSize
hAssetSize = humanize.Bytes(uint64(assetSize)) hAssetSize = humanize.Bytes(uint64(assetSize))
hSize = humanize.Bytes(uint64(totalSize)) hSize = humanize.Bytes(uint64(totalSize))
hTotalSize = "-"
hTrafficUploadSize = "-"
hTrafficDownloadSize = "-"
if conf.ProviderSiYuan == Conf.Sync.Provider {
hTotalSize = humanize.Bytes(uint64(Conf.User.UserSiYuanRepoSize)) hTotalSize = humanize.Bytes(uint64(Conf.User.UserSiYuanRepoSize))
hTrafficUploadSize = humanize.Bytes(uint64(Conf.User.UserTrafficUpload))
hTrafficDownloadSize = humanize.Bytes(uint64(Conf.User.UserTrafficDownload))
}
return return
} }
func getCloudSpaceOSS() (sync, backup map[string]interface{}, assetSize int64, err error) { func getCloudSpaceOSS() (stat *cloud.Stat, err error) {
result := map[string]interface{}{} repo, err := newRepository()
resp, err := httpclient.NewCloudRequest().
SetResult(&result).
SetBody(map[string]string{"token": Conf.User.UserToken}).
Post(util.AliyunServer + "/apis/siyuan/dejavu/getRepoStat?uid=" + Conf.User.UserId)
if nil != err { if nil != err {
logging.LogErrorf("get cloud space failed: %s", err)
err = ErrFailedToConnectCloudServer
return return
} }
if 401 == resp.StatusCode { stat, err = repo.GetCloudRepoStat()
err = errors.New(Conf.Language(31)) if nil != err {
logging.LogErrorf("get cloud repo stat failed: %s", err)
return return
} }
code := result["code"].(float64)
if 0 != code {
logging.LogErrorf("get cloud space failed: %s", result["msg"])
err = errors.New(result["msg"].(string))
return
}
data := result["data"].(map[string]interface{})
sync = data["sync"].(map[string]interface{})
backup = data["backup"].(map[string]interface{})
assetSize = int64(data["assetSize"].(float64))
return return
} }