Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add doge driver #6201

Merged
merged 6 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: 调整结构体名字,与driver保持一致
  • Loading branch information
eryajf committed Mar 24, 2024
commit 54d2a60223da5e7189724bdaddcaac04dee8196f
28 changes: 14 additions & 14 deletions drivers/doge/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ import (
log "github.com/sirupsen/logrus"
)

type S3 struct {
type Doge struct {
model.Storage
Addition
Session *session.Session
client *s3.S3
linkClient *s3.S3
}

func (d *S3) Config() driver.Config {
func (d *Doge) Config() driver.Config {
return config
}

func (d *S3) GetAddition() driver.Additional {
func (d *Doge) GetAddition() driver.Additional {
return &d.Addition
}

func (d *S3) Init(ctx context.Context) error {
func (d *Doge) Init(ctx context.Context) error {
if d.Region == "" {
d.Region = "automatic"
}
Expand All @@ -49,18 +49,18 @@ func (d *S3) Init(ctx context.Context) error {
return nil
}

func (d *S3) Drop(ctx context.Context) error {
func (d *Doge) Drop(ctx context.Context) error {
return nil
}

func (d *S3) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
func (d *Doge) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
if d.ListObjectVersion == "v2" {
return d.listV2(dir.GetPath(), args)
}
return d.listV1(dir.GetPath(), args)
}

func (d *S3) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
func (d *Doge) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
path := getKey(file.GetPath(), false)
filename := stdpath.Base(path)
disposition := fmt.Sprintf(`attachment; filename*=UTF-8''%s`, url.PathEscape(filename))
Expand Down Expand Up @@ -95,7 +95,7 @@ func (d *S3) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*mo
}, nil
}

func (d *S3) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
func (d *Doge) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
return d.Put(ctx, &model.Object{
Path: stdpath.Join(parentDir.GetPath(), dirName),
}, &stream.FileStream{
Expand All @@ -108,34 +108,34 @@ func (d *S3) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) e
}, func(float64) {})
}

func (d *S3) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
func (d *Doge) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
err := d.Copy(ctx, srcObj, dstDir)
if err != nil {
return err
}
return d.Remove(ctx, srcObj)
}

func (d *S3) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
func (d *Doge) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
err := d.copy(ctx, srcObj.GetPath(), stdpath.Join(stdpath.Dir(srcObj.GetPath()), newName), srcObj.IsDir())
if err != nil {
return err
}
return d.Remove(ctx, srcObj)
}

func (d *S3) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
func (d *Doge) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
return d.copy(ctx, srcObj.GetPath(), stdpath.Join(dstDir.GetPath(), srcObj.GetName()), srcObj.IsDir())
}

func (d *S3) Remove(ctx context.Context, obj model.Obj) error {
func (d *Doge) Remove(ctx context.Context, obj model.Obj) error {
if obj.IsDir() {
return d.removeDir(ctx, obj.GetPath())
}
return d.removeFile(obj.GetPath())
}

func (d *S3) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
func (d *Doge) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
uploader := s3manager.NewUploader(d.Session)
if stream.GetSize() > s3manager.MaxUploadParts*s3manager.DefaultUploadPartSize {
uploader.PartSize = stream.GetSize() / (s3manager.MaxUploadParts - 1)
Expand All @@ -153,4 +153,4 @@ func (d *S3) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreame
return err
}

var _ driver.Driver = (*S3)(nil)
var _ driver.Driver = (*Doge)(nil)
2 changes: 1 addition & 1 deletion drivers/doge/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ var config = driver.Config{

func init() {
op.RegisterDriver(func() driver.Driver {
return &S3{}
return &Doge{}
})
}
18 changes: 9 additions & 9 deletions drivers/doge/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

// do others that not defined in Driver interface

func (d *S3) initSession() error {
func (d *Doge) initSession() error {
var err error
credentialsTmp, err := getCredentials(d.AccessKeyID, d.SecretAccessKey)
if err != nil {
Expand All @@ -41,7 +41,7 @@ func (d *S3) initSession() error {
return err
}

func (d *S3) getClient(link bool) *s3.S3 {
func (d *Doge) getClient(link bool) *s3.S3 {
client := s3.New(d.Session)
if link && d.CustomHost != "" {
client.Handlers.Build.PushBack(func(r *request.Request) {
Expand Down Expand Up @@ -129,7 +129,7 @@ func getPlaceholderName(placeholder string) string {
return placeholder
}

func (d *S3) listV1(prefix string, args model.ListArgs) ([]model.Obj, error) {
func (d *Doge) listV1(prefix string, args model.ListArgs) ([]model.Obj, error) {
prefix = getKey(prefix, true)
log.Debugf("list: %s", prefix)
files := make([]model.Obj, 0)
Expand Down Expand Up @@ -180,7 +180,7 @@ func (d *S3) listV1(prefix string, args model.ListArgs) ([]model.Obj, error) {
return files, nil
}

func (d *S3) listV2(prefix string, args model.ListArgs) ([]model.Obj, error) {
func (d *Doge) listV2(prefix string, args model.ListArgs) ([]model.Obj, error) {
prefix = getKey(prefix, true)
files := make([]model.Obj, 0)
var continuationToken, startAfter *string
Expand Down Expand Up @@ -238,14 +238,14 @@ func (d *S3) listV2(prefix string, args model.ListArgs) ([]model.Obj, error) {
return files, nil
}

func (d *S3) copy(ctx context.Context, src string, dst string, isDir bool) error {
func (d *Doge) copy(ctx context.Context, src string, dst string, isDir bool) error {
if isDir {
return d.copyDir(ctx, src, dst)
}
return d.copyFile(ctx, src, dst)
}

func (d *S3) copyFile(ctx context.Context, src string, dst string) error {
func (d *Doge) copyFile(ctx context.Context, src string, dst string) error {
srcKey := getKey(src, false)
dstKey := getKey(dst, false)
input := &s3.CopyObjectInput{
Expand All @@ -257,7 +257,7 @@ func (d *S3) copyFile(ctx context.Context, src string, dst string) error {
return err
}

func (d *S3) copyDir(ctx context.Context, src string, dst string) error {
func (d *Doge) copyDir(ctx context.Context, src string, dst string) error {
objs, err := op.List(ctx, d, src, model.ListArgs{S3ShowPlaceholder: true})
if err != nil {
return err
Expand All @@ -277,7 +277,7 @@ func (d *S3) copyDir(ctx context.Context, src string, dst string) error {
return nil
}

func (d *S3) removeDir(ctx context.Context, src string) error {
func (d *Doge) removeDir(ctx context.Context, src string) error {
objs, err := op.List(ctx, d, src, model.ListArgs{})
if err != nil {
return err
Expand All @@ -298,7 +298,7 @@ func (d *S3) removeDir(ctx context.Context, src string) error {
return nil
}

func (d *S3) removeFile(src string) error {
func (d *Doge) removeFile(src string) error {
key := getKey(src, false)
input := &s3.DeleteObjectInput{
Bucket: &d.Bucket,
Expand Down