File size: 3,746 Bytes
7107f0b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
// Credits: https://pkg.go.dev/github.com/rclone/[email protected]/cmd/serve/s3
// Package s3 implements a fake s3 server for alist
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 getFileHashByte(node interface{}) []byte {
// b, err := hex.DecodeString(getFileHash(node))
// if err != nil {
// return nil
// }
// return b
// }
func getFileHash(node interface{}) string {
// var o fs.Object
// switch b := node.(type) {
// case vfs.Node:
// fsObj, ok := b.DirEntry().(fs.Object)
// if !ok {
// fs.Debugf("serve s3", "File uploading - reading hash from VFS cache")
// in, err := b.Open(os.O_RDONLY)
// if err != nil {
// return ""
// }
// defer func() {
// _ = in.Close()
// }()
// h, err := hash.NewMultiHasherTypes(hash.NewHashSet(hash.MD5))
// if err != nil {
// return ""
// }
// _, err = io.Copy(h, in)
// if err != nil {
// return ""
// }
// return h.Sums()[hash.MD5]
// }
// o = fsObj
// case fs.Object:
// o = b
// }
// hash, err := o.Hash(context.Background(), hash.MD5)
// if err != nil {
// return ""
// }
// return hash
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:]
}
// // FIXME this could be implemented by VFS.MkdirAll()
// func mkdirRecursive(path string, VFS *vfs.VFS) error {
// path = strings.Trim(path, "/")
// dirs := strings.Split(path, "/")
// dir := ""
// for _, d := range dirs {
// dir += "/" + d
// if _, err := VFS.Stat(dir); err != nil {
// err := VFS.Mkdir(dir, 0777)
// if err != nil {
// return err
// }
// }
// }
// return nil
// }
// func rmdirRecursive(p string, VFS *vfs.VFS) {
// dir := path.Dir(p)
// if !strings.ContainsAny(dir, "/\\") {
// // might be bucket(root)
// return
// }
// if _, err := VFS.Stat(dir); err == nil {
// err := VFS.Remove(dir)
// if err != nil {
// return
// }
// rmdirRecursive(dir, VFS)
// }
// }
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
}
|