files supports / in keys

master
Bel LaPointe 2021-07-17 11:16:26 -06:00
parent e040e04fcb
commit 5f275dd3f0
1 changed files with 16 additions and 6 deletions

View File

@ -63,7 +63,11 @@ 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)
path := path.Join(b.root, namespace, key)
return os.Open(path)
r, err := os.Open(path)
if os.IsNotExist(err) {
return nil, ErrNotFound
}
return r, err
}
func (b *Files) Set(key string, value []byte, ns ...string) error {
@ -78,8 +82,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)
dir := path.Join(b.root, namespace)
path := path.Join(dir, key)
dir := path.Join(b.root, namespace, path.Dir(key))
path := path.Join(dir, path.Base(key))
err := os.Remove(path)
if os.IsNotExist(err) {
err = nil
@ -90,8 +94,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)
dir := path.Join(b.root, namespace)
path := path.Join(dir, key)
dir := path.Join(b.root, namespace, path.Dir(key))
path := path.Join(dir, path.Base(key))
if r == nil {
err := os.Remove(path)
if os.IsNotExist(err) {
@ -100,15 +104,21 @@ func (b *Files) SetStream(key string, r io.Reader, ns ...string) error {
return err
}
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
log.Printf("failed mkdir: %v", err)
return err
}
f, err := os.Create(path)
if err != nil {
log.Printf("failed create: %v: path=%q, dir=%q, ns=%q", err, path, dir, namespace)
return err
}
defer f.Close()
_, err = io.Copy(f, r)
return err
if err != nil {
log.Printf("failed copy: %v", err)
return err
}
return nil
}
func (b *Files) Close() error {