files supports / in keys
parent
e040e04fcb
commit
5f275dd3f0
22
files.go
22
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) {
|
func (b *Files) GetStream(key string, ns ...string) (io.Reader, error) {
|
||||||
namespace := resolve.Namespace(ns)
|
namespace := resolve.Namespace(ns)
|
||||||
path := path.Join(b.root, namespace, key)
|
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 {
|
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 {
|
func (b *Files) Del(key string, ns ...string) error {
|
||||||
log.Println("files.Del", ns, key)
|
log.Println("files.Del", ns, key)
|
||||||
namespace := resolve.Namespace(ns)
|
namespace := resolve.Namespace(ns)
|
||||||
dir := path.Join(b.root, namespace)
|
dir := path.Join(b.root, namespace, path.Dir(key))
|
||||||
path := path.Join(dir, key)
|
path := path.Join(dir, path.Base(key))
|
||||||
err := os.Remove(path)
|
err := os.Remove(path)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
err = nil
|
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 {
|
func (b *Files) SetStream(key string, r io.Reader, ns ...string) error {
|
||||||
log.Println("files.SetStream", ns, key, "to", r, r == nil)
|
log.Println("files.SetStream", ns, key, "to", r, r == nil)
|
||||||
namespace := resolve.Namespace(ns)
|
namespace := resolve.Namespace(ns)
|
||||||
dir := path.Join(b.root, namespace)
|
dir := path.Join(b.root, namespace, path.Dir(key))
|
||||||
path := path.Join(dir, key)
|
path := path.Join(dir, path.Base(key))
|
||||||
if r == nil {
|
if r == nil {
|
||||||
err := os.Remove(path)
|
err := os.Remove(path)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
|
|
@ -100,15 +104,21 @@ func (b *Files) SetStream(key string, r io.Reader, ns ...string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||||
|
log.Printf("failed mkdir: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
f, err := os.Create(path)
|
f, err := os.Create(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("failed create: %v: path=%q, dir=%q, ns=%q", err, path, dir, namespace)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
_, err = io.Copy(f, r)
|
_, 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 {
|
func (b *Files) Close() error {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue