diff options
| author | Pablo M. Bermudo Garay <pablombg@gmail.com> | 2022-02-09 21:37:44 +0100 |
|---|---|---|
| committer | Pablo M. Bermudo Garay <pablombg@gmail.com> | 2022-02-10 13:14:19 +0100 |
| commit | 3aa4d62526537ec0fe624899a2f05566ea80a198 (patch) | |
| tree | 6ab3bfe1de3f8b598563b0fc71a84cd300ffafa9 /objectstore | |
| parent | 53667583c094252818f203b9233a7726d0a4e8a0 (diff) | |
Add and use file backend
Diffstat (limited to 'objectstore')
| -rw-r--r-- | objectstore/file_backend.go | 110 | ||||
| -rw-r--r-- | objectstore/objecstore.go | 2 |
2 files changed, 111 insertions, 1 deletions
diff --git a/objectstore/file_backend.go b/objectstore/file_backend.go new file mode 100644 index 0000000..df2d6bd --- /dev/null +++ b/objectstore/file_backend.go @@ -0,0 +1,110 @@ +// Basic in-memory object store +// +// Licensed under CC0 1.0 Universal: +// https://creativecommons.org/publicdomain/zero/1.0/legalcode +package objectstore + +import ( + "errors" + "fmt" + "io/ioutil" + "os" + "path" + "sync" +) + +type FileObjectStore struct { + sync.Mutex + dataPath string +} + +func NewFileBackend(dataPath string) *FileObjectStore { + err := os.MkdirAll(dataPath, 0755) + if err != nil { + panic(err) + } + fos := &FileObjectStore{dataPath: dataPath} + return fos +} + +func (fos *FileObjectStore) CreateObject(bucketId string, objectId string, payload []byte) error { + fos.Lock() + defer fos.Unlock() + + // Check if the bucket exists + buckets, err := ioutil.ReadDir(fos.dataPath) + if err != nil { + return fmt.Errorf("Error reading data directory") + } + + bucketFound := false + for _, f := range buckets { + if f.Name() == bucketId && f.IsDir() { + bucketFound = true + break + } + } + + // Create bucket if it doesn't exist + if !bucketFound { + err := os.Mkdir(path.Join(fos.dataPath, bucketId), 0755) + if err != nil { + return fmt.Errorf("Error creating bucket directory: %v", bucketId) + } + } + + // Store the object + err = os.WriteFile(path.Join(fos.dataPath, bucketId, objectId), payload, 0644) + if err != nil { + return fmt.Errorf("Error creating the object file %v", bucketId) + } + + return nil +} + +func (fos *FileObjectStore) GetObject(bucketId string, objectId string) (string, error) { + fos.Lock() + defer fos.Unlock() + + objectPath := path.Join(fos.dataPath, bucketId, objectId) + object, err := os.ReadFile(objectPath) + if err != nil { + return "", os.ErrNotExist + } + + return string(object), nil +} + +func (fos *FileObjectStore) DeleteObject(bucketId string, objectId string) error { + fos.Lock() + defer fos.Unlock() + + objectPath := path.Join(fos.dataPath, bucketId, objectId) + + // Check whether the file exists + _, err := os.Stat(objectPath) + if errors.Is(err, os.ErrNotExist) { + return err + } + + // Delete file + err = os.Remove(objectPath) + if err != nil { + return err + } + + // Delete the bucket directory if it's empty + bucketPath := path.Join(fos.dataPath, bucketId) + entries, err := ioutil.ReadDir(bucketPath) + if err != nil { + fmt.Println(err) + } + if len(entries) == 0 { + err := os.Remove(bucketPath) + if err != nil { + fmt.Println(err) + } + } + + return nil +} diff --git a/objectstore/objecstore.go b/objectstore/objecstore.go index f4985c9..7cbb481 100644 --- a/objectstore/objecstore.go +++ b/objectstore/objecstore.go @@ -9,7 +9,7 @@ import ( ) type ObjectStore interface { - CreateObject(bucketId string, objectId string, content string) error + CreateObject(bucketId string, objectId string, content []byte) error GetObject(bucketId string, objectId string) (string, error) DeleteObject(bucketId string, objectId string) error } |
