Add different data providers and improve docs

This adds support for switching between in-memory, local filesystem, s3
or sqlite.
This commit is contained in:
patwie
2024-03-29 10:02:31 +00:00
parent 2dd1421b6e
commit 31c8029402
10 changed files with 426 additions and 89 deletions
+54
View File
@@ -0,0 +1,54 @@
package filesystem
import (
"bytes"
"context"
"excalidraw-complete/core"
"fmt"
"log"
"os"
"path/filepath"
"github.com/oklog/ulid/v2"
)
type documentStore struct {
basePath string // Directory where documents are stored.
}
func NewDocumentStore(basePath string) core.DocumentStore {
if err := os.MkdirAll(basePath, 0755); err != nil {
log.Fatalf("failed to create base directory: %v", err)
}
return &documentStore{basePath: basePath}
}
func (s *documentStore) FindID(ctx context.Context, id string) (*core.Document, error) {
filePath := filepath.Join(s.basePath, id)
data, err := os.ReadFile(filePath)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("document with id %s not found", id)
}
return nil, err
}
document := core.Document{
Data: *bytes.NewBuffer(data),
}
return &document, nil
}
func (s *documentStore) Create(ctx context.Context, document *core.Document) (string, error) {
id := ulid.Make().String()
filePath := filepath.Join(s.basePath, id)
if err := os.WriteFile(filePath, document.Data.Bytes(), 0644); err != nil {
return "", err
}
return id, nil
}