|
|
|
|
|
package s3 |
|
|
|
import ( |
|
"context" |
|
"encoding/json" |
|
"strings" |
|
|
|
"github.com/Mikubill/gofakes3" |
|
"github.com/alist-org/alist/v3/internal/conf" |
|
"github.com/alist-org/alist/v3/internal/errs" |
|
"github.com/alist-org/alist/v3/internal/fs" |
|
"github.com/alist-org/alist/v3/internal/model" |
|
"github.com/alist-org/alist/v3/internal/op" |
|
"github.com/alist-org/alist/v3/internal/setting" |
|
) |
|
|
|
type Bucket struct { |
|
Name string `json:"name"` |
|
Path string `json:"path"` |
|
} |
|
|
|
func getAndParseBuckets() ([]Bucket, error) { |
|
var res []Bucket |
|
err := json.Unmarshal([]byte(setting.GetStr(conf.S3Buckets)), &res) |
|
return res, err |
|
} |
|
|
|
func getBucketByName(name string) (Bucket, error) { |
|
buckets, err := getAndParseBuckets() |
|
if err != nil { |
|
return Bucket{}, err |
|
} |
|
for _, b := range buckets { |
|
if b.Name == name { |
|
return b, nil |
|
} |
|
} |
|
return Bucket{}, gofakes3.BucketNotFound(name) |
|
} |
|
|
|
func getDirEntries(path string) ([]model.Obj, error) { |
|
ctx := context.Background() |
|
meta, _ := op.GetNearestMeta(path) |
|
fi, err := fs.Get(context.WithValue(ctx, "meta", meta), path, &fs.GetArgs{}) |
|
if errs.IsNotFoundError(err) { |
|
return nil, gofakes3.ErrNoSuchKey |
|
} else if err != nil { |
|
return nil, gofakes3.ErrNoSuchKey |
|
} |
|
|
|
if !fi.IsDir() { |
|
return nil, gofakes3.ErrNoSuchKey |
|
} |
|
|
|
dirEntries, err := fs.List(context.WithValue(ctx, "meta", meta), path, &fs.ListArgs{}) |
|
if err != nil { |
|
return nil, err |
|
} |
|
|
|
return dirEntries, nil |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getFileHash(node interface{}) string { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return "" |
|
} |
|
|
|
func prefixParser(p *gofakes3.Prefix) (path, remaining string) { |
|
idx := strings.LastIndexByte(p.Prefix, '/') |
|
if idx < 0 { |
|
return "", p.Prefix |
|
} |
|
return p.Prefix[:idx], p.Prefix[idx+1:] |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func authlistResolver() map[string]string { |
|
s3accesskeyid := setting.GetStr(conf.S3AccessKeyId) |
|
s3secretaccesskey := setting.GetStr(conf.S3SecretAccessKey) |
|
if s3accesskeyid == "" && s3secretaccesskey == "" { |
|
return nil |
|
} |
|
authList := make(map[string]string) |
|
authList[s3accesskeyid] = s3secretaccesskey |
|
return authList |
|
} |
|
|