Skip to content

Commit

Permalink
🎨 调整数据同步接入第三方存储服务 siyuan-note/siyuan#6530
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Nov 9, 2022
1 parent 180c1ff commit 2ce1b3b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
6 changes: 3 additions & 3 deletions cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ type Cloud interface {
UploadObject(filePath string, overwrite bool) (err error)

// DownloadObject 用于下载对象数据 data。
DownloadObject(key string) (data []byte, err error)
DownloadObject(filePath string) (data []byte, err error)

// RemoveObject 用于删除对象。
RemoveObject(key string) (err error)
RemoveObject(filePath string) (err error)

// GetTags 用于获取快照标记列表。
GetTags() (tags []*Ref, err error)
Expand Down Expand Up @@ -155,7 +155,7 @@ func (baseCloud *BaseCloud) UploadObject(filePath string, overwrite bool) (err e
return
}

func (baseCloud *BaseCloud) DownloadObject(key string) (data []byte, err error) {
func (baseCloud *BaseCloud) DownloadObject(filePath string) (data []byte, err error) {
err = ErrUnsupported
return
}
Expand Down
7 changes: 3 additions & 4 deletions cloud/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,21 @@ func (s3 *S3) UploadObject(filePath string, overwrite bool) (err error) {
return
}
defer file.Close()
key := path.Join("repo", filePath)
_, err = svc.PutObjectWithContext(ctx, &as3.PutObjectInput{
Bucket: aws.String(s3.Conf.Bucket),
Key: aws.String(key),
Key: aws.String(path.Join("repo", filePath)),
Body: file,
})
return
}

func (s3 *S3) DownloadObject(key string) (data []byte, err error) {
func (s3 *S3) DownloadObject(filePath string) (data []byte, err error) {
svc := s3.getService()
ctx, cancelFn := context.WithTimeout(context.Background(), 30*time.Second)
defer cancelFn()
input := &as3.GetObjectInput{
Bucket: aws.String(s3.Conf.Bucket),
Key: aws.String(key),
Key: aws.String(path.Join("repo", filePath)),
}
resp, err := svc.GetObjectWithContext(ctx, input)
if nil != err {
Expand Down
6 changes: 4 additions & 2 deletions cloud/siyuan.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func (siyuan *SiYuan) UploadObject(filePath string, overwrite bool) (err error)
return
}

func (siyuan *SiYuan) DownloadObject(key string) (ret []byte, err error) {
func (siyuan *SiYuan) DownloadObject(filePath string) (ret []byte, err error) {
key := path.Join("siyuan", siyuan.Conf.UserID, "repo", siyuan.Conf.Dir, filePath)
resp, err := httpclient.NewCloudFileRequest15s().Get(siyuan.Endpoint + key)
if nil != err {
err = fmt.Errorf("download object [%s] failed: %s", key, err)
Expand All @@ -104,12 +105,13 @@ func (siyuan *SiYuan) DownloadObject(key string) (ret []byte, err error) {
return
}

func (siyuan *SiYuan) RemoveObject(key string) (err error) {
func (siyuan *SiYuan) RemoveObject(filePath string) (err error) {
userId := siyuan.Conf.UserID
dir := siyuan.Conf.Dir
token := siyuan.Conf.Token
server := siyuan.Conf.Server

key := path.Join("siyuan", userId, "repo", dir, filePath)
result := gulu.Ret.NewResult()
request := httpclient.NewCloudRequest()
resp, err := request.
Expand Down
26 changes: 9 additions & 17 deletions sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,9 +999,7 @@ func (repo *Repo) latestSync() (ret *entity.Index) {
func (repo *Repo) downloadCloudChunk(id string, context map[string]interface{}) (length int64, ret *entity.Chunk, err error) {
eventbus.Publish(eventbus.EvtCloudBeforeDownloadChunk, context, id)

userId := repo.cloud.GetConf().UserID
dir := repo.cloud.GetConf().Dir
key := path.Join("siyuan", userId, "repo", dir, "objects", id[:2], id[2:])
key := path.Join("objects", id[:2], id[2:])
data, err := repo.downloadCloudObject(key)
if nil != err {
logging.LogErrorf("download cloud chunk [%s] failed: %s", id, err)
Expand All @@ -1015,9 +1013,7 @@ func (repo *Repo) downloadCloudChunk(id string, context map[string]interface{})
func (repo *Repo) downloadCloudFile(id string, context map[string]interface{}) (length int64, ret *entity.File, err error) {
eventbus.Publish(eventbus.EvtCloudBeforeDownloadFile, context, id)

userId := repo.cloud.GetConf().UserID
dir := repo.cloud.GetConf().Dir
key := path.Join("siyuan", userId, "repo", dir, "objects", id[:2], id[2:])
key := path.Join("objects", id[:2], id[2:])
data, err := repo.downloadCloudObject(key)
if nil != err {
logging.LogErrorf("download cloud file [%s] failed: %s", id, err)
Expand All @@ -1029,17 +1025,17 @@ func (repo *Repo) downloadCloudFile(id string, context map[string]interface{}) (
return
}

func (repo *Repo) downloadCloudObject(key string) (ret []byte, err error) {
data, err := repo.cloud.DownloadObject(key)
func (repo *Repo) downloadCloudObject(filePath string) (ret []byte, err error) {
data, err := repo.cloud.DownloadObject(filePath)
if nil != err {
return
}

ret, err = repo.decodeDownloadedData(key, data)
ret, err = repo.decodeDownloadedData(filePath, data)
if nil != err {
return
}
//logging.LogInfof("downloaded object [%s]", key)
//logging.LogInfof("downloaded object [%s]", filePath)
return
}

Expand All @@ -1065,9 +1061,7 @@ func (repo *Repo) downloadCloudIndex(id string, context map[string]interface{})
eventbus.Publish(eventbus.EvtCloudBeforeDownloadIndex, context, id)
index = &entity.Index{}

userId := repo.cloud.GetConf().UserID
dir := repo.cloud.GetConf().Dir
key := path.Join("siyuan", userId, "repo", dir, "indexes", id)
key := path.Join("indexes", id)
data, err := repo.downloadCloudObject(key)
if nil != err {
return
Expand All @@ -1083,9 +1077,7 @@ func (repo *Repo) downloadCloudIndex(id string, context map[string]interface{})
func (repo *Repo) downloadCloudLatest(context map[string]interface{}) (downloadBytes int64, index *entity.Index, err error) {
index = &entity.Index{}

userId := repo.cloud.GetConf().UserID
dir := repo.cloud.GetConf().Dir
key := path.Join("siyuan", userId, "repo", dir, "refs", "latest")
key := path.Join("refs", "latest")
eventbus.Publish(eventbus.EvtCloudBeforeDownloadRef, context, "refs/latest")
data, err := repo.downloadCloudObject(key)
if nil != err {
Expand All @@ -1096,7 +1088,7 @@ func (repo *Repo) downloadCloudLatest(context map[string]interface{}) (downloadB
return
}
latestID := string(data)
key = path.Join("siyuan", userId, "repo", dir, "indexes", latestID)
key = path.Join("indexes", latestID)
eventbus.Publish(eventbus.EvtCloudBeforeDownloadIndex, context, latestID)
data, err = repo.downloadCloudObject(key)
if nil != err {
Expand Down

0 comments on commit 2ce1b3b

Please sign in to comment.