Implement Read Only mode

This commit is contained in:
Las Zenow 2017-05-21 10:16:16 +00:00
parent 6464d92dd4
commit e1bd235785
19 changed files with 544 additions and 335 deletions

42
lib/storage/fs.go Normal file
View file

@ -0,0 +1,42 @@
package storage
import (
p "path"
"io"
"os"
)
type fsStore struct {
path string
}
func (st *fsStore) 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 *fsStore) 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 *fsStore) Get(id string, name string) (File, error) {
path := idPath(st.path, id)
return os.Open(p.Join(path, name))
}
func (st *fsStore) Delete(id string) error {
path := idPath(st.path, id)
return os.RemoveAll(path)
}

26
lib/storage/ro.go Normal file
View file

@ -0,0 +1,26 @@
package storage
import (
"errors"
"io"
)
type roStore struct {
store Store
}
func (st *roStore) Create(id string, name string) (io.WriteCloser, error) {
return nil, errors.New("Can't create, RO storage")
}
func (st *roStore) Store(id string, file io.Reader, name string) (size int64, err error) {
return 0, errors.New("Can't store, RO storage")
}
func (st *roStore) Get(id string, name string) (File, error) {
return st.store.Get(id, name)
}
func (st *roStore) Delete(id string) error {
return errors.New("Can't delete, RO storage")
}

View file

@ -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}
}

View file

@ -25,11 +25,11 @@ const (
)
func TestInit(t *testing.T) {
st, err := Init(test_path)
_, err := Init(test_path)
if err != nil {
t.Fatal("An error ocurred initializing the store =>", err)
}
defer st.del()
defer del()
info, err := os.Stat(test_path)
if err != nil {
@ -50,7 +50,7 @@ func TestInit(t *testing.T) {
func TestStore(t *testing.T) {
st, err := Init(test_path)
defer st.del()
defer del()
_, err = st.Store(test_id, strings.NewReader(test_book), "epub")
if err != nil {
@ -72,7 +72,7 @@ func TestStore(t *testing.T) {
func TestCreate(t *testing.T) {
st, err := Init(test_path)
defer st.del()
defer del()
f, err := st.Create(test_id, "img")
if err != nil {
@ -95,7 +95,7 @@ func TestCreate(t *testing.T) {
func TestDelete(t *testing.T) {
st, err := Init(test_path)
defer st.del()
defer del()
_, err = st.Store(test_id, strings.NewReader(test_book), "epub")
if err != nil {
@ -111,3 +111,7 @@ func TestDelete(t *testing.T) {
t.Fatal("Retrieve book without error.")
}
}
func del() {
os.RemoveAll(test_path)
}