Add fs filestore
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
Torjus Håkestad 2022-01-19 01:03:24 +01:00
parent 94e1920098
commit 41aeb9abb5
5 changed files with 166 additions and 14 deletions

View File

@ -17,13 +17,20 @@ type ServerConfig struct {
}
type ServerStoreConfig struct {
Type string `toml:"Type"`
Type string `toml:"Type"`
FS *ServerStoreFSStoreConfig `toml:"FS"`
}
type ServerStoreFSStoreConfig struct {
Dir string `toml:"Dir"`
}
func ServerConfigFromReader(r io.Reader) (*ServerConfig, error) {
decoder := toml.NewDecoder(r)
c := ServerConfig{
Store: &ServerStoreConfig{},
Store: &ServerStoreConfig{
FS: &ServerStoreFSStoreConfig{},
},
}
if err := decoder.Decode(&c); err != nil {
return nil, fmt.Errorf("error decoding server config: %w", err)
@ -50,4 +57,8 @@ func (sc *ServerConfig) updateFromEnv() {
if value, ok := os.LookupEnv("GPASTE_STORE_TYPE"); ok {
sc.Store.Type = value
}
if value, ok := os.LookupEnv("GPASTE_STORE_FS_DIR"); ok {
sc.Store.FS.Dir = value
}
}

View File

@ -18,14 +18,19 @@ URL = "http://paste.example.org"
ListenAddr = ":8080"
[Store]
Type = "memory"
Type = "fs"
[Store.FS]
Dir = "/tmp"
`
expected := &gpaste.ServerConfig{
LogLevel: "INFO",
URL: "http://paste.example.org",
ListenAddr: ":8080",
Store: &gpaste.ServerStoreConfig{
Type: "memory",
Type: "fs",
FS: &gpaste.ServerStoreFSStoreConfig{
Dir: "/tmp",
},
},
}
sr := strings.NewReader(simpleConfig)
@ -43,17 +48,21 @@ Type = "memory"
clearEnv()
var envMap map[string]string = map[string]string{
"GPASTE_LOGLEVEL": "DEBUG",
"GPASTE_URL": "http://gpaste.example.org",
"GPASTE_STORE_TYPE": "memory",
"GPASTE_LISTENADDR": ":8000",
"GPASTE_LOGLEVEL": "DEBUG",
"GPASTE_URL": "http://gpaste.example.org",
"GPASTE_STORE_TYPE": "fs",
"GPASTE_LISTENADDR": ":8000",
"GPASTE_STORE_FS_DIR": "/tmp",
}
expected := &gpaste.ServerConfig{
LogLevel: "DEBUG",
URL: "http://gpaste.example.org",
ListenAddr: ":8000",
Store: &gpaste.ServerStoreConfig{
Type: "memory",
Type: "fs",
FS: &gpaste.ServerStoreFSStoreConfig{
Dir: "/tmp",
},
},
}

View File

@ -6,12 +6,12 @@ import (
)
type File struct {
ID string
OriginalFilename string
Body io.ReadCloser
ID string `json:"id"`
OriginalFilename string `json:"original_filename"`
MaxViews uint `json:"max_views"`
ExpiresOn time.Time `json:"expires_on"`
MaxViews uint
ExpiresOn time.Time
Body io.ReadCloser
}
type FileStore interface {

115
filestore_fs.go Normal file
View File

@ -0,0 +1,115 @@
package gpaste
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
)
type FSFileStore struct {
dir string
metadata map[string]*File
}
func NewFSFileStore(dir string) (*FSFileStore, error) {
s := &FSFileStore{
dir: dir,
metadata: make(map[string]*File),
}
err := s.readMetadata()
return s, err
}
func (s *FSFileStore) Store(f *File) error {
defer f.Body.Close()
metadata := &File{
ID: f.ID,
OriginalFilename: f.OriginalFilename,
MaxViews: f.MaxViews,
ExpiresOn: f.ExpiresOn,
}
path := filepath.Join(s.dir, f.ID)
dst, err := os.Create(path)
if err != nil {
return err
}
defer dst.Close()
if _, err := io.Copy(dst, f.Body); err != nil {
return err
}
s.metadata[f.ID] = metadata
if err := s.writeMetadata(); err != nil {
delete(s.metadata, f.ID)
return err
}
return nil
}
func (s *FSFileStore) Get(id string) (*File, error) {
metadata, ok := s.metadata[id]
if !ok {
return nil, fmt.Errorf("no such item")
}
path := filepath.Join(s.dir, id)
f, err := os.Open(path)
if err != nil {
return nil, err
}
metadata.Body = f
return metadata, nil
}
func (s *FSFileStore) Delete(id string) error {
path := filepath.Join(s.dir, id)
if err := os.Remove(path); err != nil {
return err
}
delete(s.metadata, id)
return nil
}
func (s *FSFileStore) List() ([]string, error) {
var results []string
for k := range s.metadata {
results = append(results, k)
}
return results, nil
}
func (s *FSFileStore) writeMetadata() error {
path := filepath.Join(s.dir, "metadata.json")
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
encoder := json.NewEncoder(f)
if err := encoder.Encode(s.metadata); err != nil {
return err
}
return nil
}
func (s *FSFileStore) readMetadata() error {
path := filepath.Join(s.dir, "metadata.json")
f, err := os.Open(path)
if err != nil {
// TODO: Handle errors better
return nil
}
defer f.Close()
decoder := json.NewDecoder(f)
if err := decoder.Decode(&s.metadata); err != nil {
return err
}
return nil
}

17
filestore_fs_test.go Normal file
View File

@ -0,0 +1,17 @@
package gpaste_test
import (
"testing"
"git.t-juice.club/torjus/gpaste"
)
func TestFSFileStore(t *testing.T) {
dir := t.TempDir()
s, err := gpaste.NewFSFileStore(dir)
if err != nil {
t.Fatalf("Error creating store: %s", err)
}
RunFilestoreTest(s, t)
}