Implement Read Only mode
This commit is contained in:
parent
6464d92dd4
commit
e1bd235785
19 changed files with 544 additions and 335 deletions
|
@ -1,14 +1,15 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
p "path"
|
||||
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
path string
|
||||
type Store interface {
|
||||
Create(id string, name string) (io.WriteCloser, error)
|
||||
Store(id string, file io.Reader, name string) (size int64, err error)
|
||||
Get(id string, name string) (File, error)
|
||||
Delete(id string) error
|
||||
}
|
||||
|
||||
type File interface {
|
||||
|
@ -17,8 +18,8 @@ type File interface {
|
|||
Stat() (fi os.FileInfo, err error)
|
||||
}
|
||||
|
||||
func Init(path string) (*Store, error) {
|
||||
st := new(Store)
|
||||
func Init(path string) (Store, error) {
|
||||
st := new(fsStore)
|
||||
st.path = path
|
||||
|
||||
_, err := os.Stat(path)
|
||||
|
@ -28,36 +29,6 @@ func Init(path string) (*Store, error) {
|
|||
return st, err
|
||||
}
|
||||
|
||||
func (st *Store) Create(id string, name string) (io.WriteCloser, error) {
|
||||
path := idPath(st.path, id)
|
||||
err := os.MkdirAll(path, os.ModePerm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return os.Create(p.Join(path, name))
|
||||
}
|
||||
|
||||
func (st *Store) Store(id string, file io.Reader, name string) (size int64, err error) {
|
||||
dest, err := st.Create(id, name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer dest.Close()
|
||||
|
||||
return io.Copy(dest, file)
|
||||
}
|
||||
|
||||
func (st *Store) Get(id string, name string) (File, error) {
|
||||
path := idPath(st.path, id)
|
||||
return os.Open(p.Join(path, name))
|
||||
}
|
||||
|
||||
func (st *Store) Delete(id string) error {
|
||||
path := idPath(st.path, id)
|
||||
return os.RemoveAll(path)
|
||||
}
|
||||
|
||||
func (st *Store) del() {
|
||||
os.RemoveAll(st.path)
|
||||
func RO(st Store) Store {
|
||||
return &roStore{st}
|
||||
}
|
||||
|
|
Reference in a new issue