2022-02-22 11:14:19 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2022-02-22 17:24:17 +00:00
|
|
|
"context"
|
2022-03-16 13:47:47 +00:00
|
|
|
"crypto/sha1"
|
2022-02-24 11:54:30 +00:00
|
|
|
"encoding/base64"
|
2022-02-22 17:24:17 +00:00
|
|
|
"encoding/json"
|
2022-03-23 09:38:14 +00:00
|
|
|
"errors"
|
2022-02-22 17:24:17 +00:00
|
|
|
"fmt"
|
2022-03-16 13:47:47 +00:00
|
|
|
"io"
|
2022-03-23 09:38:14 +00:00
|
|
|
"io/fs"
|
2022-02-22 17:24:17 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2022-03-23 09:38:14 +00:00
|
|
|
"path"
|
2022-02-22 17:24:17 +00:00
|
|
|
"path/filepath"
|
2022-03-10 15:46:56 +00:00
|
|
|
"regexp"
|
2022-05-03 16:00:07 +01:00
|
|
|
"strings"
|
2022-02-22 17:24:17 +00:00
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
"github.com/emersion/go-ical"
|
2022-02-22 11:14:19 +00:00
|
|
|
"github.com/emersion/go-vcard"
|
2022-03-23 09:38:14 +00:00
|
|
|
"github.com/emersion/go-webdav"
|
|
|
|
"github.com/emersion/go-webdav/caldav"
|
2022-02-22 11:14:19 +00:00
|
|
|
"github.com/emersion/go-webdav/carddav"
|
2022-02-22 17:24:17 +00:00
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
"git.sr.ht/~sircmpwn/tokidoki/debug"
|
2022-02-22 11:14:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type filesystemBackend struct {
|
2022-03-23 09:38:14 +00:00
|
|
|
path string
|
|
|
|
caldavPrefix string
|
|
|
|
carddavPrefix string
|
|
|
|
userPrincipalBackend webdav.UserPrincipalBackend
|
2022-02-22 11:14:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 13:37:21 +00:00
|
|
|
var (
|
2022-03-23 09:38:14 +00:00
|
|
|
validFilenameRegex = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_-]+(.[a-zA-Z]+)?$`)
|
2022-03-16 13:37:21 +00:00
|
|
|
)
|
2022-02-22 11:14:19 +00:00
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
func NewFilesystem(fsPath, caldavPrefix, carddavPrefix string, userPrincipalBackend webdav.UserPrincipalBackend) (caldav.Backend, carddav.Backend, error) {
|
|
|
|
info, err := os.Stat(fsPath)
|
2022-02-22 17:24:17 +00:00
|
|
|
if err != nil {
|
2022-03-23 09:38:14 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to create filesystem backend: %s", err.Error())
|
2022-02-22 17:24:17 +00:00
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
2022-03-23 09:38:14 +00:00
|
|
|
return nil, nil, fmt.Errorf("base path for filesystem backend must be a directory")
|
2022-02-22 17:24:17 +00:00
|
|
|
}
|
2022-03-23 09:38:14 +00:00
|
|
|
backend := &filesystemBackend{
|
|
|
|
path: fsPath,
|
|
|
|
caldavPrefix: caldavPrefix,
|
|
|
|
carddavPrefix: carddavPrefix,
|
|
|
|
userPrincipalBackend: userPrincipalBackend,
|
|
|
|
}
|
|
|
|
return backend, backend, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *filesystemBackend) CurrentUserPrincipal(ctx context.Context) (string, error) {
|
|
|
|
return b.userPrincipalBackend.CurrentUserPrincipal(ctx)
|
2022-02-22 17:24:17 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
func (b *filesystemBackend) AddressbookHomeSetPath(ctx context.Context) (string, error) {
|
|
|
|
upPath, err := b.userPrincipalBackend.CurrentUserPrincipal(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2022-02-22 17:24:17 +00:00
|
|
|
}
|
2022-03-23 09:38:14 +00:00
|
|
|
|
|
|
|
return path.Join(upPath, b.carddavPrefix) + "/", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *filesystemBackend) CalendarHomeSetPath(ctx context.Context) (string, error) {
|
|
|
|
upPath, err := b.userPrincipalBackend.CurrentUserPrincipal(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2022-03-16 13:33:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
return path.Join(upPath, b.caldavPrefix) + "/", nil
|
|
|
|
}
|
2022-02-22 17:24:17 +00:00
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
func ensureLocalDir(path string) error {
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(path, 0755)
|
2022-02-22 17:24:17 +00:00
|
|
|
if err != nil {
|
2022-03-23 09:38:14 +00:00
|
|
|
return fmt.Errorf("error creating '%s': %s", path, err.Error())
|
2022-02-22 17:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-23 09:38:14 +00:00
|
|
|
return nil
|
2022-02-22 17:24:17 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
// don't use this directly, use localCalDAVPath or localCardDAVPath instead.
|
|
|
|
func (b *filesystemBackend) safeLocalPath(homeSetPath string, urlPath string) (string, error) {
|
|
|
|
localPath := filepath.Join(b.path, homeSetPath)
|
|
|
|
if err := ensureLocalDir(localPath); err != nil {
|
2022-03-10 15:46:56 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2022-03-23 09:38:14 +00:00
|
|
|
|
|
|
|
if urlPath == "" {
|
|
|
|
return localPath, nil
|
|
|
|
}
|
|
|
|
|
2022-03-10 15:46:56 +00:00
|
|
|
// We are mapping to local filesystem path, so be conservative about what to accept
|
|
|
|
// TODO this changes once multiple addess books are supported
|
2022-03-23 09:38:14 +00:00
|
|
|
dir, file := path.Split(urlPath)
|
|
|
|
// only accept resources in prefix, no subdirs for now
|
|
|
|
if dir != homeSetPath {
|
2022-05-03 16:00:07 +01:00
|
|
|
if strings.HasPrefix(dir, homeSetPath+"/") {
|
|
|
|
err := fmt.Errorf("invalid request path: %s", urlPath)
|
|
|
|
return "", webdav.NewHTTPError(400, err)
|
|
|
|
} else {
|
|
|
|
err := fmt.Errorf("Access to resource outside of home set: %s", urlPath)
|
|
|
|
return "", webdav.NewHTTPError(403, err)
|
|
|
|
}
|
2022-03-23 09:38:14 +00:00
|
|
|
}
|
|
|
|
// only accept simple file names for now
|
|
|
|
if !validFilenameRegex.MatchString(file) {
|
2022-05-03 16:00:07 +01:00
|
|
|
debug.Printf("%s does not match regex!\n", file)
|
|
|
|
err := fmt.Errorf("invalid file name: %s", file)
|
|
|
|
return "", webdav.NewHTTPError(400, err)
|
2022-03-23 09:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// dir (= homeSetPath) is already included in path, so only file here
|
|
|
|
return filepath.Join(localPath, file), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *filesystemBackend) localCalDAVPath(ctx context.Context, urlPath string) (string, error) {
|
|
|
|
homeSetPath, err := b.CalendarHomeSetPath(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2022-03-10 15:46:56 +00:00
|
|
|
}
|
2022-03-23 09:38:14 +00:00
|
|
|
|
|
|
|
return b.safeLocalPath(homeSetPath, urlPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *filesystemBackend) localCardDAVPath(ctx context.Context, urlPath string) (string, error) {
|
|
|
|
homeSetPath, err := b.AddressbookHomeSetPath(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.safeLocalPath(homeSetPath, urlPath)
|
2022-03-10 15:46:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 11:54:30 +00:00
|
|
|
func etagForFile(path string) (string, error) {
|
2022-03-16 13:47:47 +00:00
|
|
|
f, err := os.Open(path)
|
2022-02-24 11:54:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-03-16 13:47:47 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
h := sha1.New()
|
|
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
csum := h.Sum(nil)
|
|
|
|
|
2022-02-24 11:54:30 +00:00
|
|
|
return base64.StdEncoding.EncodeToString(csum[:]), nil
|
|
|
|
}
|
|
|
|
|
2022-03-16 13:51:26 +00:00
|
|
|
func vcardPropFilter(card vcard.Card, props []string) vcard.Card {
|
2022-02-28 18:50:36 +00:00
|
|
|
if card == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(props) == 0 {
|
|
|
|
return card
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make(vcard.Card)
|
2022-03-16 13:51:26 +00:00
|
|
|
result["VERSION"] = card["VERSION"]
|
2022-02-28 18:50:36 +00:00
|
|
|
for _, prop := range props {
|
2022-03-16 13:51:26 +00:00
|
|
|
value, ok := card[prop]
|
2022-02-28 18:50:36 +00:00
|
|
|
if ok {
|
|
|
|
result[prop] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 13:51:26 +00:00
|
|
|
return result
|
2022-02-28 18:50:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 13:51:26 +00:00
|
|
|
func vcardFromFile(path string, propFilter []string) (vcard.Card, error) {
|
2022-02-24 11:54:30 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-16 13:51:26 +00:00
|
|
|
return vcardPropFilter(card, propFilter), nil
|
2022-02-24 11:54:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
func calendarFromFile(path string, propFilter []string) (*ical.Calendar, error) {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
dec := ical.NewDecoder(f)
|
|
|
|
cal, err := dec.Decode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cal, nil
|
2022-05-17 14:24:43 +01:00
|
|
|
// TODO implement
|
|
|
|
//return icalPropFilter(cal, propFilter), nil
|
2022-03-23 09:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createDefaultAddressBook(path, localPath string) error {
|
2022-02-22 17:24:17 +00:00
|
|
|
// TODO what should the default address book look like?
|
|
|
|
defaultAB := carddav.AddressBook{
|
2022-03-23 09:38:14 +00:00
|
|
|
Path: path,
|
2022-02-22 17:24:17 +00:00
|
|
|
Name: "My contacts",
|
|
|
|
Description: "Default address book",
|
|
|
|
MaxResourceSize: 1024,
|
2022-03-16 14:08:42 +00:00
|
|
|
SupportedAddressData: nil,
|
2022-02-22 17:24:17 +00:00
|
|
|
}
|
|
|
|
blob, err := json.MarshalIndent(defaultAB, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating default address book: %s", err.Error())
|
2022-02-22 11:14:19 +00:00
|
|
|
}
|
2022-03-23 09:38:14 +00:00
|
|
|
err = os.WriteFile(localPath, blob, 0644)
|
2022-02-22 17:24:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error writing default address book: %s", err.Error())
|
|
|
|
}
|
|
|
|
return nil
|
2022-02-22 11:14:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 17:24:17 +00:00
|
|
|
func (b *filesystemBackend) AddressBook(ctx context.Context) (*carddav.AddressBook, error) {
|
2022-03-23 09:38:14 +00:00
|
|
|
debug.Printf("filesystem.AddressBook()")
|
|
|
|
path, err := b.localCardDAVPath(ctx, "")
|
2022-02-22 17:24:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-23 09:38:14 +00:00
|
|
|
path = filepath.Join(path, "addressbook.json")
|
|
|
|
|
|
|
|
debug.Printf("loading addressbook from %s", path)
|
2022-03-16 14:02:18 +00:00
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(path)
|
2022-02-22 17:24:17 +00:00
|
|
|
if os.IsNotExist(err) {
|
2022-03-23 09:38:14 +00:00
|
|
|
urlPath, err := b.AddressbookHomeSetPath(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
debug.Printf("creating default addressbook (URL:path): %s:%s", urlPath, path)
|
|
|
|
err = createDefaultAddressBook(urlPath, path)
|
2022-02-22 17:24:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-16 14:02:18 +00:00
|
|
|
data, err = ioutil.ReadFile(path)
|
2022-02-22 17:24:17 +00:00
|
|
|
}
|
|
|
|
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
|
2022-02-22 11:14:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
func (b *filesystemBackend) GetAddressObject(ctx context.Context, objPath string, req *carddav.AddressDataRequest) (*carddav.AddressObject, error) {
|
|
|
|
debug.Printf("filesystem.GetAddressObject(%s, %v)", objPath, req)
|
|
|
|
localPath, err := b.localCardDAVPath(ctx, objPath)
|
2022-02-24 11:54:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-10 15:46:56 +00:00
|
|
|
info, err := os.Stat(localPath)
|
2022-02-24 11:54:30 +00:00
|
|
|
if err != nil {
|
2022-05-03 16:00:07 +01:00
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return nil, webdav.NewHTTPError(404, err)
|
|
|
|
}
|
2022-02-24 11:54:30 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-10 16:22:21 +00:00
|
|
|
var propFilter []string
|
2022-02-28 18:50:36 +00:00
|
|
|
if req != nil && !req.AllProp {
|
|
|
|
propFilter = req.Props
|
|
|
|
}
|
|
|
|
|
2022-03-10 15:46:56 +00:00
|
|
|
card, err := vcardFromFile(localPath, propFilter)
|
2022-02-24 11:54:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-10 15:46:56 +00:00
|
|
|
etag, err := etagForFile(localPath)
|
2022-02-24 11:54:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := carddav.AddressObject{
|
2022-05-17 14:28:09 +01:00
|
|
|
Path: objPath,
|
|
|
|
ModTime: info.ModTime(),
|
|
|
|
ContentLength: info.Size(),
|
|
|
|
ETag: etag,
|
|
|
|
Card: card,
|
2022-02-24 11:54:30 +00:00
|
|
|
}
|
|
|
|
return &obj, nil
|
2022-02-22 11:14:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
func (b *filesystemBackend) loadAllContacts(ctx context.Context, propFilter []string) ([]carddav.AddressObject, error) {
|
2022-03-16 14:08:42 +00:00
|
|
|
var result []carddav.AddressObject
|
2022-02-24 11:54:30 +00:00
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
localPath, err := b.localCardDAVPath(ctx, "")
|
2022-02-24 11:54:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
homeSetPath, err := b.AddressbookHomeSetPath(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = filepath.Walk(localPath, func(filename string, info os.FileInfo, err error) error {
|
|
|
|
if !info.Mode().IsRegular() || filepath.Ext(filename) != ".vcf" {
|
2022-02-24 11:54:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-28 18:50:36 +00:00
|
|
|
card, err := vcardFromFile(filename, propFilter)
|
2022-02-24 11:54:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
etag, err := etagForFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := carddav.AddressObject{
|
2022-05-17 14:28:09 +01:00
|
|
|
Path: path.Join(homeSetPath, filepath.Base(filename)),
|
|
|
|
ModTime: info.ModTime(),
|
|
|
|
ContentLength: info.Size(),
|
|
|
|
ETag: etag,
|
|
|
|
Card: card,
|
2022-02-24 11:54:30 +00:00
|
|
|
}
|
|
|
|
result = append(result, obj)
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-28 18:50:36 +00:00
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
debug.Printf("filesystem.loadAllContacts() returning %d results from %s", len(result), localPath)
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *filesystemBackend) loadAllCalendars(ctx context.Context, propFilter []string) ([]caldav.CalendarObject, error) {
|
|
|
|
var result []caldav.CalendarObject
|
|
|
|
|
|
|
|
localPath, err := b.localCalDAVPath(ctx, "")
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
homeSetPath, err := b.CalendarHomeSetPath(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = filepath.Walk(localPath, func(filename string, info os.FileInfo, err error) error {
|
|
|
|
// Skip address book meta data files
|
|
|
|
if !info.Mode().IsRegular() || filepath.Ext(filename) != ".ics" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cal, err := calendarFromFile(filename, propFilter)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("load calendar error for %s: %v\n", filename, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
etag, err := etagForFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := caldav.CalendarObject{
|
2022-05-17 14:28:09 +01:00
|
|
|
Path: path.Join(homeSetPath, filepath.Base(filename)),
|
|
|
|
ModTime: info.ModTime(),
|
|
|
|
ContentLength: info.Size(),
|
|
|
|
ETag: etag,
|
|
|
|
Data: cal,
|
2022-03-23 09:38:14 +00:00
|
|
|
}
|
|
|
|
result = append(result, obj)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
debug.Printf("filesystem.loadAllCalendars() returning %d results from %s", len(result), localPath)
|
2022-02-28 18:50:36 +00:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *filesystemBackend) ListAddressObjects(ctx context.Context, req *carddav.AddressDataRequest) ([]carddav.AddressObject, error) {
|
2022-03-23 09:38:14 +00:00
|
|
|
debug.Printf("filesystem.ListAddressObjects(%v)", req)
|
2022-03-10 16:22:21 +00:00
|
|
|
var propFilter []string
|
2022-02-28 18:50:36 +00:00
|
|
|
if req != nil && !req.AllProp {
|
|
|
|
propFilter = req.Props
|
2022-02-24 11:54:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
return b.loadAllContacts(ctx, propFilter)
|
2022-02-22 11:14:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 18:50:36 +00:00
|
|
|
func (b *filesystemBackend) QueryAddressObjects(ctx context.Context, query *carddav.AddressBookQuery) ([]carddav.AddressObject, error) {
|
2022-03-23 09:38:14 +00:00
|
|
|
debug.Printf("filesystem.QueryAddressObjects(%v)", query)
|
2022-03-10 16:22:21 +00:00
|
|
|
var propFilter []string
|
2022-02-28 18:50:36 +00:00
|
|
|
if query != nil && !query.DataRequest.AllProp {
|
|
|
|
propFilter = query.DataRequest.Props
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
result, err := b.loadAllContacts(ctx, propFilter)
|
2022-02-28 18:50:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return carddav.Filter(query, result)
|
2022-02-22 11:14:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
func (b *filesystemBackend) PutAddressObject(ctx context.Context, objPath string, card vcard.Card, opts *carddav.PutAddressObjectOptions) (loc string, err error) {
|
|
|
|
debug.Printf("filesystem.PutAddressObject(%v, %v, %v)", objPath, card, opts)
|
2022-03-10 16:22:21 +00:00
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
// Object always get saved as <UID>.vcf
|
|
|
|
dirname, _ := path.Split(objPath)
|
|
|
|
objPath = path.Join(dirname, card.Value(vcard.FieldUID)+".vcf")
|
2022-03-10 16:22:21 +00:00
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
localPath, err := b.localCardDAVPath(ctx, objPath)
|
2022-02-24 11:54:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-03-10 16:22:21 +00:00
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
if _, err := os.Stat(localPath); !os.IsNotExist(err) {
|
2022-03-10 16:22:21 +00:00
|
|
|
return "", carddav.NewPreconditionError(carddav.PreconditionNoUIDConflict)
|
|
|
|
}
|
|
|
|
|
2022-03-16 14:11:06 +00:00
|
|
|
f, err := os.Create(localPath)
|
2022-02-24 11:54:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
enc := vcard.NewEncoder(f)
|
|
|
|
err = enc.Encode(card)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:38:14 +00:00
|
|
|
return objPath, nil
|
2022-02-22 11:14:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 11:54:30 +00:00
|
|
|
func (b *filesystemBackend) DeleteAddressObject(ctx context.Context, path string) error {
|
2022-03-23 09:38:14 +00:00
|
|
|
debug.Printf("filesystem.DeleteAddressObject(%s)", path)
|
|
|
|
|
|
|
|
localPath, err := b.localCardDAVPath(ctx, path)
|
2022-02-24 11:54:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-10 15:46:56 +00:00
|
|
|
err = os.Remove(localPath)
|
2022-02-24 11:54:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-22 17:24:17 +00:00
|
|
|
return nil
|
2022-02-22 11:14:19 +00:00
|
|
|
}
|
2022-03-23 09:38:14 +00:00
|
|
|
|
|
|
|
func createDefaultCalendar(path, localPath string) error {
|
|
|
|
// TODO what should the default calendar look like?
|
|
|
|
defaultC := caldav.Calendar{
|
|
|
|
Path: path,
|
|
|
|
Name: "My calendar",
|
|
|
|
Description: "Default calendar",
|
|
|
|
MaxResourceSize: 4096,
|
|
|
|
}
|
|
|
|
blob, err := json.MarshalIndent(defaultC, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating default calendar: %s", err.Error())
|
|
|
|
}
|
|
|
|
err = os.WriteFile(localPath, blob, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error writing default calendar: %s", err.Error())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *filesystemBackend) Calendar(ctx context.Context) (*caldav.Calendar, error) {
|
|
|
|
debug.Printf("filesystem.Calendar()")
|
|
|
|
|
|
|
|
path, err := b.localCalDAVPath(ctx, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
path = filepath.Join(path, "calendar.json")
|
|
|
|
|
|
|
|
debug.Printf("loading calendar from %s", path)
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
homeSetPath, err := b.CalendarHomeSetPath(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
debug.Printf("creating default calendar (URL:path): %s:%s", homeSetPath, path)
|
|
|
|
err = createDefaultCalendar(homeSetPath, path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data, err = ioutil.ReadFile(path)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error opening calendar: %s", err.Error())
|
|
|
|
}
|
|
|
|
var calendar caldav.Calendar
|
|
|
|
err = json.Unmarshal(data, &calendar)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading calendar: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &calendar, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *filesystemBackend) GetCalendarObject(ctx context.Context, objPath string, req *caldav.CalendarCompRequest) (*caldav.CalendarObject, error) {
|
|
|
|
debug.Printf("filesystem.GetCalendarObject(%s, %v)", objPath, req)
|
|
|
|
|
|
|
|
localPath, err := b.localCalDAVPath(ctx, objPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := os.Stat(localPath)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
debug.Printf("not found: %s", localPath)
|
2022-05-03 16:00:07 +01:00
|
|
|
return nil, webdav.NewHTTPError(404, err)
|
2022-03-23 09:38:14 +00:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var propFilter []string
|
|
|
|
if req != nil && !req.AllProps {
|
|
|
|
propFilter = req.Props
|
|
|
|
}
|
|
|
|
|
|
|
|
calendar, err := calendarFromFile(localPath, propFilter)
|
|
|
|
if err != nil {
|
|
|
|
debug.Printf("error reading calendar: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
etag, err := etagForFile(localPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := caldav.CalendarObject{
|
2022-05-17 14:28:09 +01:00
|
|
|
Path: objPath,
|
|
|
|
ModTime: info.ModTime(),
|
|
|
|
ContentLength: info.Size(),
|
|
|
|
ETag: etag,
|
|
|
|
Data: calendar,
|
2022-03-23 09:38:14 +00:00
|
|
|
}
|
|
|
|
return &obj, nil
|
|
|
|
return nil, fmt.Errorf("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *filesystemBackend) ListCalendarObjects(ctx context.Context, req *caldav.CalendarCompRequest) ([]caldav.CalendarObject, error) {
|
|
|
|
debug.Printf("filesystem.ListCalendarObjects(%v)", req)
|
|
|
|
|
|
|
|
var propFilter []string
|
|
|
|
if req != nil && !req.AllProps {
|
|
|
|
propFilter = req.Props
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.loadAllCalendars(ctx, propFilter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *filesystemBackend) QueryCalendarObjects(ctx context.Context, query *caldav.CalendarQuery) ([]caldav.CalendarObject, error) {
|
|
|
|
debug.Printf("filesystem.QueryCalendarObjects(%v)", query)
|
|
|
|
|
|
|
|
var propFilter []string
|
|
|
|
if query != nil && !query.CompRequest.AllProps {
|
|
|
|
propFilter = query.CompRequest.Props
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := b.loadAllCalendars(ctx, propFilter)
|
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
// TODO implement:
|
|
|
|
//return caldav.Filter(query, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *filesystemBackend) PutCalendarObject(ctx context.Context, objPath string, calendar *ical.Calendar, opts *caldav.PutCalendarObjectOptions) (loc string, err error) {
|
|
|
|
debug.Printf("filesystem.PutCalendarObject(%s, %v, %v)", objPath, calendar, opts)
|
|
|
|
|
|
|
|
_, uid, err := caldav.ValidateCalendarObject(calendar)
|
|
|
|
if err != nil {
|
|
|
|
return "", caldav.NewPreconditionError(caldav.PreconditionValidCalendarObjectResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object always get saved as <UID>.ics
|
|
|
|
dirname, _ := path.Split(objPath)
|
|
|
|
objPath = path.Join(dirname, uid+".ics")
|
|
|
|
|
|
|
|
localPath, err := b.localCalDAVPath(ctx, objPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(localPath); !os.IsNotExist(err) {
|
|
|
|
return "", caldav.NewPreconditionError(caldav.PreconditionNoUIDConflict)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(localPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
enc := ical.NewEncoder(f)
|
|
|
|
err = enc.Encode(calendar)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return objPath, nil
|
|
|
|
return "", nil
|
|
|
|
}
|