From 5f275dd3f0f5338e14308041127288ded4717093 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Sat, 17 Jul 2021 11:16:26 -0600 Subject: [PATCH] files supports / in keys --- files.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/files.go b/files.go index 6209d87..a35cbba 100755 --- a/files.go +++ b/files.go @@ -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 {