You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.2 KiB
92 lines
2.2 KiB
10 years ago
|
package donut
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"os"
|
||
|
"path"
|
||
|
"sort"
|
||
|
"strings"
|
||
|
|
||
|
"encoding/json"
|
||
10 years ago
|
"io/ioutil"
|
||
10 years ago
|
"path/filepath"
|
||
|
)
|
||
|
|
||
|
type localDirectoryNode struct {
|
||
|
root string
|
||
|
}
|
||
|
|
||
10 years ago
|
func (d localDirectoryNode) CreateBucket(bucket string) error {
|
||
|
objectPath := path.Join(d.root, bucket)
|
||
|
return os.MkdirAll(objectPath, 0700)
|
||
|
}
|
||
|
|
||
10 years ago
|
func (d localDirectoryNode) GetBuckets() ([]string, error) {
|
||
10 years ago
|
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
|
||
10 years ago
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
10 years ago
|
return newDonutObjectWriter(objectPath)
|
||
10 years ago
|
}
|
||
|
|
||
|
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
|
||
|
}
|