storage/files.go

117 lines
2.3 KiB
Go
Executable File

package storage
import (
"bytes"
"io"
"io/ioutil"
"local/storage/resolve"
"log"
"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 := resolve.Namespace(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, strings.TrimPrefix(p, path.Join(b.root, namespace)+"/"))
return nil
})
return files, err
/*
limits = resolve.Limits(limits)
*/
}
func (b *Files) Get(key string, ns ...string) ([]byte, error) {
r, err := b.GetStream(key, ns...)
if err != nil {
return nil, err
}
return ioutil.ReadAll(r)
}
func (b *Files) GetStream(key string, ns ...string) (io.Reader, error) {
namespace := resolve.Namespace(ns)
path := path.Join(b.root, namespace, key)
return os.Open(path)
}
func (b *Files) Set(key string, value []byte, ns ...string) error {
log.Println("files.Set", ns, key, "to", len(value), value == nil)
r := bytes.NewReader(value)
if value == nil {
return b.Del(key, ns...)
}
return b.SetStream(key, r, ns...)
}
func (b *Files) Del(key string, ns ...string) error {
log.Println("files.Del", ns, key)
namespace := resolve.Namespace(ns)
dir := path.Join(b.root, namespace)
path := path.Join(dir, key)
err := os.Remove(path)
if os.IsNotExist(err) {
err = nil
}
return err
}
func (b *Files) SetStream(key string, r io.Reader, ns ...string) error {
log.Println("files.SetStream", ns, key, "to", r, r == nil)
namespace := resolve.Namespace(ns)
dir := path.Join(b.root, namespace)
path := path.Join(dir, key)
if r == nil {
err := os.Remove(path)
if os.IsNotExist(err) {
err = nil
}
return err
}
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, r)
return err
}
func (b *Files) Close() error {
return nil
}