move resolve into package
This commit is contained in:
16
files.go
16
files.go
@@ -4,6 +4,8 @@ import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"local/storage/resolve"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
@@ -29,7 +31,7 @@ func NewFiles(root string) (*Files, error) {
|
||||
}
|
||||
|
||||
func (b *Files) List(ns []string, limits ...string) ([]string, error) {
|
||||
namespace := resolveNamespace(ns)
|
||||
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 {
|
||||
@@ -46,7 +48,7 @@ func (b *Files) List(ns []string, limits ...string) ([]string, error) {
|
||||
})
|
||||
return files, err
|
||||
/*
|
||||
limits = resolveLimits(limits)
|
||||
limits = resolve.Limits(limits)
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -59,12 +61,13 @@ func (b *Files) Get(key string, ns ...string) ([]byte, error) {
|
||||
}
|
||||
|
||||
func (b *Files) GetStream(key string, ns ...string) (io.Reader, error) {
|
||||
namespace := resolveNamespace(ns)
|
||||
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 {
|
||||
r = nil
|
||||
@@ -73,10 +76,12 @@ func (b *Files) Set(key string, value []byte, ns ...string) error {
|
||||
}
|
||||
|
||||
func (b *Files) SetStream(key string, r io.Reader, ns ...string) error {
|
||||
namespace := resolveNamespace(ns)
|
||||
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.Join(dir, key))
|
||||
err := os.Remove(path)
|
||||
if os.IsNotExist(err) {
|
||||
err = nil
|
||||
}
|
||||
@@ -85,7 +90,6 @@ func (b *Files) SetStream(key string, r io.Reader, ns ...string) error {
|
||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
path := path.Join(dir, key)
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user