implement file tree
parent
be25dee55d
commit
3ea5d7e284
2
db.go
2
db.go
|
|
@ -32,6 +32,8 @@ func New(key Type, params ...string) (db DB, err error) {
|
|||
err = nil
|
||||
case RCLONE:
|
||||
db, err = NewRClone(params[0], params[1])
|
||||
case FILES:
|
||||
db, err = NewFiles(params[0])
|
||||
case BOLT:
|
||||
db, err = NewBolt(params[0])
|
||||
case MINIO:
|
||||
|
|
|
|||
|
|
@ -96,6 +96,12 @@ func TestImplementations(t *testing.T) {
|
|||
cases = append(cases, bolt)
|
||||
}
|
||||
|
||||
if files, err := NewFiles(path.Join(dir, "files")); err != nil {
|
||||
t.Errorf("cannot make files: %v", err)
|
||||
} else {
|
||||
cases = append(cases, files)
|
||||
}
|
||||
|
||||
if leveldb, err := NewLevelDB(path.Join(dir, "leveldb")); err != nil {
|
||||
t.Errorf("cannot make leveldb: %v", err)
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,70 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Files struct {
|
||||
root string
|
||||
}
|
||||
|
||||
func NewFiles(root string) (*Files, error) {
|
||||
_, err := os.Stat(path.Dir(root))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
root, err = filepath.Abs(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Files{
|
||||
root: root,
|
||||
}, os.MkdirAll(root, os.ModePerm)
|
||||
}
|
||||
|
||||
func (b *Files) List(ns []string, limits ...string) ([]string, error) {
|
||||
namespace := resolveNamespace(ns)
|
||||
files := make([]string, 0)
|
||||
err := filepath.Walk(b.root, func(p string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if !strings.HasPrefix(strings.TrimPrefix(p, b.root+"/"), namespace) {
|
||||
return nil
|
||||
}
|
||||
files = append(files, path.Join(b.root, p))
|
||||
return nil
|
||||
})
|
||||
return files, err
|
||||
/*
|
||||
namespace := resolveNamespace(ns)
|
||||
limits = resolveLimits(limits)
|
||||
*/
|
||||
}
|
||||
|
||||
func (b *Files) Get(key string, ns ...string) ([]byte, error) {
|
||||
namespace := resolveNamespace(ns)
|
||||
path := path.Join(b.root, namespace, key)
|
||||
return ioutil.ReadFile(path)
|
||||
}
|
||||
|
||||
func (b *Files) Set(key string, value []byte, ns ...string) error {
|
||||
namespace := resolveNamespace(ns)
|
||||
dir := path.Join(b.root, namespace)
|
||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
path := path.Join(dir, key)
|
||||
return ioutil.WriteFile(path, value, os.ModePerm)
|
||||
}
|
||||
|
||||
func (b *Files) Close() error {
|
||||
return nil
|
||||
}
|
||||
3
type.go
3
type.go
|
|
@ -11,6 +11,7 @@ const (
|
|||
REDIS = Type(iota)
|
||||
DYNOMITE = Type(iota)
|
||||
BOLT = Type(iota)
|
||||
FILES = Type(iota)
|
||||
COCKROACH = Type(iota)
|
||||
CACHE = Type(iota)
|
||||
LEVELDB = Type(iota)
|
||||
|
|
@ -33,6 +34,8 @@ func (t Type) String() string {
|
|||
return "rclone"
|
||||
case COCKROACH:
|
||||
return "cockroach"
|
||||
case FILES:
|
||||
return "files"
|
||||
case BOLT:
|
||||
return "bolt"
|
||||
case MINIO:
|
||||
|
|
|
|||
Loading…
Reference in New Issue