files use ext but will fallback
parent
5f275dd3f0
commit
036c327db4
20
files.go
20
files.go
|
|
@ -12,6 +12,10 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
fileExt = ".file"
|
||||
)
|
||||
|
||||
type Files struct {
|
||||
root string
|
||||
}
|
||||
|
|
@ -43,7 +47,10 @@ func (b *Files) List(ns []string, limits ...string) ([]string, error) {
|
|||
if !strings.HasPrefix(strings.TrimPrefix(p, b.root+"/"), namespace+"/") {
|
||||
return nil
|
||||
}
|
||||
files = append(files, strings.TrimPrefix(p, path.Join(b.root, namespace)+"/"))
|
||||
filedir := path.Join(b.root, namespace)
|
||||
file := strings.TrimPrefix(p, filedir+"/")
|
||||
file = strings.TrimSuffix(file, fileExt)
|
||||
files = append(files, file)
|
||||
return nil
|
||||
})
|
||||
return files, err
|
||||
|
|
@ -62,8 +69,12 @@ func (b *Files) Get(key string, ns ...string) ([]byte, error) {
|
|||
|
||||
func (b *Files) GetStream(key string, ns ...string) (io.Reader, error) {
|
||||
namespace := resolve.Namespace(ns)
|
||||
key += fileExt
|
||||
path := path.Join(b.root, namespace, key)
|
||||
r, err := os.Open(path)
|
||||
r, err := os.Open(path + fileExt)
|
||||
if os.IsNotExist(err) {
|
||||
r, err = os.Open(path)
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
|
@ -71,7 +82,6 @@ func (b *Files) GetStream(key string, ns ...string) (io.Reader, error) {
|
|||
}
|
||||
|
||||
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...)
|
||||
|
|
@ -80,8 +90,8 @@ func (b *Files) Set(key string, value []byte, ns ...string) error {
|
|||
}
|
||||
|
||||
func (b *Files) Del(key string, ns ...string) error {
|
||||
log.Println("files.Del", ns, key)
|
||||
namespace := resolve.Namespace(ns)
|
||||
key += fileExt
|
||||
dir := path.Join(b.root, namespace, path.Dir(key))
|
||||
path := path.Join(dir, path.Base(key))
|
||||
err := os.Remove(path)
|
||||
|
|
@ -92,8 +102,8 @@ func (b *Files) Del(key string, ns ...string) error {
|
|||
}
|
||||
|
||||
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)
|
||||
key += fileExt
|
||||
dir := path.Join(b.root, namespace, path.Dir(key))
|
||||
path := path.Join(dir, path.Base(key))
|
||||
if r == nil {
|
||||
|
|
|
|||
Loading…
Reference in New Issue