implement file tree
This commit is contained in:
70
files.go
Executable file
70
files.go
Executable file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user