2014-12-08 17:25:57 -05:00
|
|
|
package fsstorage
|
2014-11-14 20:22:50 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2014-12-08 17:25:57 -05:00
|
|
|
type FileSystemStorage struct {
|
2014-11-14 20:22:50 -05:00
|
|
|
RootDir string
|
|
|
|
}
|
|
|
|
|
2014-12-09 06:32:31 -05:00
|
|
|
func (storage FileSystemStorage) GetList() ([]byte, error) {
|
|
|
|
fileInfos, err := ioutil.ReadDir(storage.RootDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var list []byte
|
|
|
|
for _, fi := range fileInfos {
|
|
|
|
list = append(list, "{"+fi.Name()+"}\n"...)
|
|
|
|
}
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2014-12-08 17:25:57 -05:00
|
|
|
func (storage FileSystemStorage) Get(objectPath string) ([]byte, error) {
|
2014-11-14 20:22:50 -05:00
|
|
|
return ioutil.ReadFile(path.Join(storage.RootDir, objectPath))
|
|
|
|
}
|
|
|
|
|
2014-12-08 17:25:57 -05:00
|
|
|
func (storage FileSystemStorage) Put(objectPath string, object []byte) error {
|
2014-12-09 06:32:31 -05:00
|
|
|
err := os.MkdirAll(filepath.Dir(path.Join(storage.RootDir, objectPath)), 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-14 20:22:50 -05:00
|
|
|
return ioutil.WriteFile(path.Join(storage.RootDir, objectPath), object, 0600)
|
|
|
|
}
|