tokidoki/storage/filesystem.go
Conrad Hoffmann 8c8d96c2bc Fix conflation of URL and storage path
The path returned must of course be the external URL path, not the
internal storage path.
2022-02-28 19:48:49 +01:00

246 lines
5.5 KiB
Go

package storage
import (
"context"
"crypto/md5"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/emersion/go-vcard"
"github.com/emersion/go-webdav/carddav"
"git.sr.ht/~sircmpwn/tokidoki/auth"
)
type filesystemBackend struct {
path string
}
var nilBackend carddav.Backend = (*filesystemBackend)(nil)
func NewFilesystem(path string) (carddav.Backend, error) {
info, err := os.Stat(path)
if err != nil {
return nilBackend, fmt.Errorf("failed to create filesystem backend: %s", err.Error())
}
if !info.IsDir() {
return nilBackend, fmt.Errorf("base path for filesystem backend must be a directory")
}
return &filesystemBackend{
path: path,
}, nil
}
func (b *filesystemBackend) pathForContext(ctx context.Context) (string, error) {
raw := ctx.Value(auth.AuthCtxKey)
if raw == nil {
return b.path, nil
}
authCtx, ok := raw.(*auth.AuthContext)
if !ok {
panic("Invalid data in auth context!")
}
//TODO sanitize user name or at least check if valid dir name?
path := filepath.Join(b.path, authCtx.UserName)
_, err := os.Stat(path)
if os.IsNotExist(err) {
err = os.Mkdir(path, 0755)
if err != nil {
return "", fmt.Errorf("error creating '%s': %s", path, err.Error())
}
}
return path, nil
}
func etagForFile(path string) (string, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
csum := md5.Sum(data)
return base64.StdEncoding.EncodeToString(csum[:]), nil
}
func vcardFromFile(path string) (*vcard.Card, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
dec := vcard.NewDecoder(f)
card, err := dec.Decode()
if err != nil {
return nil, err
}
return &card, nil
}
func createDefaultAddressBook(path string) error {
// TODO what should the default address book look like?
defaultAB := carddav.AddressBook{
Path: "/default",
Name: "My contacts",
Description: "Default address book",
MaxResourceSize: 1024,
SupportedAddressData: []carddav.AddressDataType{},
}
blob, err := json.MarshalIndent(defaultAB, "", " ")
if err != nil {
return fmt.Errorf("error creating default address book: %s", err.Error())
}
err = os.WriteFile(path, blob, 0644)
if err != nil {
return fmt.Errorf("error writing default address book: %s", err.Error())
}
return nil
}
func (b *filesystemBackend) AddressBook(ctx context.Context) (*carddav.AddressBook, error) {
path, err := b.pathForContext(ctx)
if err != nil {
return nil, err
}
path = filepath.Join(path, "default.json")
_, err = os.Stat(path)
if os.IsNotExist(err) {
err = createDefaultAddressBook(path)
if err != nil {
return nil, err
}
} else if err != nil {
return nil, fmt.Errorf("error opening address book: %s", err.Error())
}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error opening address book: %s", err.Error())
}
var addressBook carddav.AddressBook
err = json.Unmarshal(data, &addressBook)
if err != nil {
return nil, fmt.Errorf("error reading address book: %s", err.Error())
}
return &addressBook, nil
}
func (b *filesystemBackend) GetAddressObject(ctx context.Context, path string, req *carddav.AddressDataRequest) (*carddav.AddressObject, error) {
basePath, err := b.pathForContext(ctx)
if err != nil {
return nil, err
}
path = filepath.Join(basePath, path)
info, err := os.Stat(path)
if err != nil {
return nil, err
}
card, err := vcardFromFile(path)
if err != nil {
return nil, err
}
etag, err := etagForFile(path)
if err != nil {
return nil, err
}
obj := carddav.AddressObject{
Path: "/" + filepath.Base(path),
ModTime: info.ModTime(),
ETag: etag,
Card: *card,
}
return &obj, nil
}
func (b *filesystemBackend) ListAddressObjects(ctx context.Context, req *carddav.AddressDataRequest) ([]carddav.AddressObject, error) {
result := []carddav.AddressObject{}
path, err := b.pathForContext(ctx)
if err != nil {
return result, err
}
err = filepath.Walk(path, func(filename string, info os.FileInfo, err error) error {
// TODO this heuristic will not work for all clients
if filepath.Ext(filename) != ".vcf" {
return nil
}
card, err := vcardFromFile(filename)
if err != nil {
return err
}
etag, err := etagForFile(filename)
if err != nil {
return err
}
obj := carddav.AddressObject{
Path: "/" + filepath.Base(filename),
ModTime: info.ModTime(),
ETag: etag,
Card: *card,
}
result = append(result, obj)
return nil
})
if err != nil {
panic(err)
}
return result, nil
}
func (*filesystemBackend) QueryAddressObjects(ctx context.Context, query *carddav.AddressBookQuery) ([]carddav.AddressObject, error) {
// TODO
log.Println("QueryAddressObjects called, not implemented")
return []carddav.AddressObject{}, nil
}
func (b *filesystemBackend) PutAddressObject(ctx context.Context, path string, card vcard.Card) (loc string, err error) {
basePath, err := b.pathForContext(ctx)
if err != nil {
return "", err
}
internalPath := filepath.Join(basePath, path)
f, err := os.OpenFile(internalPath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return "", err
}
defer f.Close()
enc := vcard.NewEncoder(f)
err = enc.Encode(card)
if err != nil {
return "", err
}
return path, nil
}
func (b *filesystemBackend) DeleteAddressObject(ctx context.Context, path string) error {
basePath, err := b.pathForContext(ctx)
if err != nil {
return err
}
path = filepath.Join(basePath, path)
//TODO does this need more security/sanity checks?
err = os.Remove(path)
if err != nil {
return err
}
return nil
}