2015-03-22 18:14:06 -04:00
|
|
|
package donut
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"encoding/json"
|
2015-03-24 22:58:03 -04:00
|
|
|
"io/ioutil"
|
2015-03-22 18:14:06 -04:00
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
type localDirectoryNode struct {
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:06:15 -04:00
|
|
|
func (d localDirectoryNode) CreateBucket(bucket string) error {
|
|
|
|
objectPath := path.Join(d.root, bucket)
|
|
|
|
return os.MkdirAll(objectPath, 0700)
|
|
|
|
}
|
|
|
|
|
2015-03-22 18:14:06 -04:00
|
|
|
func (d localDirectoryNode) GetBuckets() ([]string, error) {
|
2015-03-24 22:58:03 -04:00
|
|
|
files, err := ioutil.ReadDir(d.root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var results []string
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() {
|
|
|
|
results = append(results, file.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results, nil
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d localDirectoryNode) GetWriter(bucket, object string) (Writer, error) {
|
|
|
|
objectPath := path.Join(d.root, bucket, object)
|
|
|
|
err := os.MkdirAll(objectPath, 0700)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-23 23:40:21 -04:00
|
|
|
return newDonutObjectWriter(objectPath)
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d localDirectoryNode) GetReader(bucket, object string) (io.ReadCloser, error) {
|
|
|
|
return os.Open(path.Join(d.root, bucket, object, "data"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d localDirectoryNode) GetMetadata(bucket, object string) (map[string]string, error) {
|
|
|
|
return d.getMetadata(bucket, object, "metadata.json")
|
|
|
|
}
|
|
|
|
func (d localDirectoryNode) GetDonutMetadata(bucket, object string) (map[string]string, error) {
|
|
|
|
return d.getMetadata(bucket, object, "donutMetadata.json")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d localDirectoryNode) getMetadata(bucket, object, fileName string) (map[string]string, error) {
|
|
|
|
file, err := os.Open(path.Join(d.root, bucket, object, fileName))
|
|
|
|
defer file.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
decoder := json.NewDecoder(file)
|
|
|
|
if err := decoder.Decode(&metadata); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return metadata, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d localDirectoryNode) ListObjects(bucketName string) ([]string, error) {
|
|
|
|
prefix := path.Join(d.root, bucketName)
|
|
|
|
var objects []string
|
|
|
|
if err := filepath.Walk(prefix, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !info.IsDir() && strings.HasSuffix(path, "data") {
|
|
|
|
object := strings.TrimPrefix(path, prefix+"/")
|
|
|
|
object = strings.TrimSuffix(object, "/data")
|
|
|
|
objects = append(objects, object)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sort.Strings(objects)
|
|
|
|
return objects, nil
|
|
|
|
}
|