mirror of
https://git.sr.ht/~sircmpwn/tokidoki
synced 2025-12-12 06:07:22 +01:00
Add CalDAV support, refactor
The filesystem storage backend now implements the required functions to act as a basic CalDAV server. Some refactoring was done based on the go-webdav development: introduce a UserPrincipalBackend, a new function to serve the user principal URL, and more. See this PR for lots of details: https://github.com/emersion/go-webdav/pull/62 Also adds a simple facility for debug output.
This commit is contained in:
parent
5728f1ee27
commit
001917295d
7 changed files with 487 additions and 85 deletions
|
|
@ -5,74 +5,129 @@ import (
|
|||
"crypto/sha1"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
"github.com/emersion/go-ical"
|
||||
"github.com/emersion/go-vcard"
|
||||
"github.com/emersion/go-webdav"
|
||||
"github.com/emersion/go-webdav/caldav"
|
||||
"github.com/emersion/go-webdav/carddav"
|
||||
|
||||
"git.sr.ht/~sircmpwn/tokidoki/auth"
|
||||
"git.sr.ht/~sircmpwn/tokidoki/debug"
|
||||
)
|
||||
|
||||
type filesystemBackend struct {
|
||||
path string
|
||||
path string
|
||||
caldavPrefix string
|
||||
carddavPrefix string
|
||||
userPrincipalBackend webdav.UserPrincipalBackend
|
||||
}
|
||||
|
||||
var (
|
||||
nilBackend carddav.Backend = (*filesystemBackend)(nil)
|
||||
validFilenameRegex = regexp.MustCompile(`^/[A-Za-z0-9][A-Za-z0-9_-]+(.[a-zA-Z]+)?$`)
|
||||
validFilenameRegex = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9_-]+(.[a-zA-Z]+)?$`)
|
||||
)
|
||||
|
||||
func NewFilesystem(path string) (carddav.Backend, error) {
|
||||
info, err := os.Stat(path)
|
||||
func NewFilesystem(fsPath, caldavPrefix, carddavPrefix string, userPrincipalBackend webdav.UserPrincipalBackend) (caldav.Backend, carddav.Backend, error) {
|
||||
info, err := os.Stat(fsPath)
|
||||
if err != nil {
|
||||
return nilBackend, fmt.Errorf("failed to create filesystem backend: %s", err.Error())
|
||||
return nil, nil, 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 nil, nil, fmt.Errorf("base path for filesystem backend must be a directory")
|
||||
}
|
||||
return &filesystemBackend{
|
||||
path: path,
|
||||
}, nil
|
||||
backend := &filesystemBackend{
|
||||
path: fsPath,
|
||||
caldavPrefix: caldavPrefix,
|
||||
carddavPrefix: carddavPrefix,
|
||||
userPrincipalBackend: userPrincipalBackend,
|
||||
}
|
||||
return backend, backend, nil
|
||||
}
|
||||
|
||||
func (b *filesystemBackend) pathForContext(ctx context.Context) (string, error) {
|
||||
authCtx, ok := auth.FromContext(ctx)
|
||||
if !ok {
|
||||
panic("Invalid data in auth context!")
|
||||
}
|
||||
if authCtx == nil {
|
||||
return "", fmt.Errorf("unauthenticated requests are not supported")
|
||||
}
|
||||
|
||||
userDir := base64.RawStdEncoding.EncodeToString([]byte(authCtx.UserName))
|
||||
path := filepath.Join(b.path, userDir)
|
||||
|
||||
_, 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 (b *filesystemBackend) CurrentUserPrincipal(ctx context.Context) (string, error) {
|
||||
return b.userPrincipalBackend.CurrentUserPrincipal(ctx)
|
||||
}
|
||||
|
||||
func (b *filesystemBackend) safePath(ctx context.Context, path string) (string, error) {
|
||||
basePath, err := b.pathForContext(ctx)
|
||||
func (b *filesystemBackend) AddressbookHomeSetPath(ctx context.Context) (string, error) {
|
||||
upPath, err := b.userPrincipalBackend.CurrentUserPrincipal(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return path.Join(upPath, b.caldavPrefix) + "/", nil
|
||||
}
|
||||
|
||||
func ensureLocalDir(path string) error {
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
err = os.MkdirAll(path, 0755)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating '%s': %s", path, err.Error())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if urlPath == "" {
|
||||
return localPath, nil
|
||||
}
|
||||
|
||||
// We are mapping to local filesystem path, so be conservative about what to accept
|
||||
// TODO this changes once multiple addess books are supported
|
||||
if !validFilenameRegex.MatchString(path) {
|
||||
return "", fmt.Errorf("invalid request path")
|
||||
dir, file := path.Split(urlPath)
|
||||
// only accept resources in prefix, no subdirs for now
|
||||
if dir != homeSetPath {
|
||||
return "", fmt.Errorf("invalid request path %s", urlPath)
|
||||
}
|
||||
return filepath.Join(basePath, path), nil
|
||||
// only accept simple file names for now
|
||||
if !validFilenameRegex.MatchString(file) {
|
||||
fmt.Printf("%s does not match regex!\n", file)
|
||||
return "", fmt.Errorf("invalid file name")
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func etagForFile(path string) (string, error) {
|
||||
|
|
@ -128,10 +183,27 @@ func vcardFromFile(path string, propFilter []string) (vcard.Card, error) {
|
|||
return vcardPropFilter(card, propFilter), nil
|
||||
}
|
||||
|
||||
func createDefaultAddressBook(path string) error {
|
||||
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
|
||||
// return vcardPropFilter(card, propFilter), nil
|
||||
}
|
||||
|
||||
func createDefaultAddressBook(path, localPath string) error {
|
||||
// TODO what should the default address book look like?
|
||||
defaultAB := carddav.AddressBook{
|
||||
Path: "/default",
|
||||
Path: path,
|
||||
Name: "My contacts",
|
||||
Description: "Default address book",
|
||||
MaxResourceSize: 1024,
|
||||
|
|
@ -141,7 +213,7 @@ func createDefaultAddressBook(path string) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("error creating default address book: %s", err.Error())
|
||||
}
|
||||
err = os.WriteFile(path, blob, 0644)
|
||||
err = os.WriteFile(localPath, blob, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error writing default address book: %s", err.Error())
|
||||
}
|
||||
|
|
@ -149,15 +221,23 @@ func createDefaultAddressBook(path string) error {
|
|||
}
|
||||
|
||||
func (b *filesystemBackend) AddressBook(ctx context.Context) (*carddav.AddressBook, error) {
|
||||
path, err := b.pathForContext(ctx)
|
||||
debug.Printf("filesystem.AddressBook()")
|
||||
path, err := b.localCardDAVPath(ctx, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
path = filepath.Join(path, "_default_ab.json")
|
||||
path = filepath.Join(path, "addressbook.json")
|
||||
|
||||
debug.Printf("loading addressbook from %s", path)
|
||||
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if os.IsNotExist(err) {
|
||||
err = createDefaultAddressBook(path)
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -175,8 +255,9 @@ func (b *filesystemBackend) AddressBook(ctx context.Context) (*carddav.AddressBo
|
|||
return &addressBook, nil
|
||||
}
|
||||
|
||||
func (b *filesystemBackend) GetAddressObject(ctx context.Context, path string, req *carddav.AddressDataRequest) (*carddav.AddressObject, error) {
|
||||
localPath, err := b.safePath(ctx, path)
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -202,7 +283,7 @@ func (b *filesystemBackend) GetAddressObject(ctx context.Context, path string, r
|
|||
}
|
||||
|
||||
obj := carddav.AddressObject{
|
||||
Path: path,
|
||||
Path: objPath,
|
||||
ModTime: info.ModTime(),
|
||||
ETag: etag,
|
||||
Card: card,
|
||||
|
|
@ -210,17 +291,21 @@ func (b *filesystemBackend) GetAddressObject(ctx context.Context, path string, r
|
|||
return &obj, nil
|
||||
}
|
||||
|
||||
func (b *filesystemBackend) loadAll(ctx context.Context, propFilter []string) ([]carddav.AddressObject, error) {
|
||||
func (b *filesystemBackend) loadAllContacts(ctx context.Context, propFilter []string) ([]carddav.AddressObject, error) {
|
||||
var result []carddav.AddressObject
|
||||
|
||||
path, err := b.pathForContext(ctx)
|
||||
localPath, err := b.localCardDAVPath(ctx, "")
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
err = filepath.Walk(path, func(filename string, info os.FileInfo, err error) error {
|
||||
// Skip address book meta data files
|
||||
if !info.Mode().IsRegular() || filepath.Ext(filename) == ".json" {
|
||||
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" {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -235,7 +320,7 @@ func (b *filesystemBackend) loadAll(ctx context.Context, propFilter []string) ([
|
|||
}
|
||||
|
||||
obj := carddav.AddressObject{
|
||||
Path: "/" + filepath.Base(filename),
|
||||
Path: path.Join(homeSetPath, filepath.Base(filename)),
|
||||
ModTime: info.ModTime(),
|
||||
ETag: etag,
|
||||
Card: card,
|
||||
|
|
@ -244,25 +329,72 @@ func (b *filesystemBackend) loadAll(ctx context.Context, propFilter []string) ([
|
|||
return nil
|
||||
})
|
||||
|
||||
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{
|
||||
Path: path.Join(homeSetPath, filepath.Base(filename)),
|
||||
ModTime: info.ModTime(),
|
||||
ETag: etag,
|
||||
Data: cal,
|
||||
}
|
||||
result = append(result, obj)
|
||||
return nil
|
||||
})
|
||||
|
||||
debug.Printf("filesystem.loadAllCalendars() returning %d results from %s", len(result), localPath)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (b *filesystemBackend) ListAddressObjects(ctx context.Context, req *carddav.AddressDataRequest) ([]carddav.AddressObject, error) {
|
||||
debug.Printf("filesystem.ListAddressObjects(%v)", req)
|
||||
var propFilter []string
|
||||
if req != nil && !req.AllProp {
|
||||
propFilter = req.Props
|
||||
}
|
||||
|
||||
return b.loadAll(ctx, propFilter)
|
||||
return b.loadAllContacts(ctx, propFilter)
|
||||
}
|
||||
|
||||
func (b *filesystemBackend) QueryAddressObjects(ctx context.Context, query *carddav.AddressBookQuery) ([]carddav.AddressObject, error) {
|
||||
debug.Printf("filesystem.QueryAddressObjects(%v)", query)
|
||||
var propFilter []string
|
||||
if query != nil && !query.DataRequest.AllProp {
|
||||
propFilter = query.DataRequest.Props
|
||||
}
|
||||
|
||||
result, err := b.loadAll(ctx, propFilter)
|
||||
result, err := b.loadAllContacts(ctx, propFilter)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
|
@ -270,31 +402,19 @@ func (b *filesystemBackend) QueryAddressObjects(ctx context.Context, query *card
|
|||
return carddav.Filter(query, result)
|
||||
}
|
||||
|
||||
func (b *filesystemBackend) hasUIDConflict(ctx context.Context, uid, path string) (bool, error) {
|
||||
all, err := b.loadAll(ctx, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
for _, contact := range all {
|
||||
if contact.Path != path && contact.Card.Value(vcard.FieldUID) == uid {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
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)
|
||||
|
||||
return false, nil
|
||||
}
|
||||
// Object always get saved as <UID>.vcf
|
||||
dirname, _ := path.Split(objPath)
|
||||
objPath = path.Join(dirname, card.Value(vcard.FieldUID)+".vcf")
|
||||
|
||||
func (b *filesystemBackend) PutAddressObject(ctx context.Context, path string, card vcard.Card) (loc string, err error) {
|
||||
localPath, err := b.safePath(ctx, path)
|
||||
localPath, err := b.localCardDAVPath(ctx, objPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
conflict, err := b.hasUIDConflict(ctx, card.Value(vcard.FieldUID), path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if conflict {
|
||||
if _, err := os.Stat(localPath); !os.IsNotExist(err) {
|
||||
return "", carddav.NewPreconditionError(carddav.PreconditionNoUIDConflict)
|
||||
}
|
||||
|
||||
|
|
@ -310,11 +430,13 @@ func (b *filesystemBackend) PutAddressObject(ctx context.Context, path string, c
|
|||
return "", err
|
||||
}
|
||||
|
||||
return path, nil
|
||||
return objPath, nil
|
||||
}
|
||||
|
||||
func (b *filesystemBackend) DeleteAddressObject(ctx context.Context, path string) error {
|
||||
localPath, err := b.safePath(ctx, path)
|
||||
debug.Printf("filesystem.DeleteAddressObject(%s)", path)
|
||||
|
||||
localPath, err := b.localCardDAVPath(ctx, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -324,3 +446,167 @@ func (b *filesystemBackend) DeleteAddressObject(ctx context.Context, path string
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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)
|
||||
return nil, nil
|
||||
}
|
||||
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{
|
||||
Path: objPath,
|
||||
ModTime: info.ModTime(),
|
||||
ETag: etag,
|
||||
Data: calendar,
|
||||
}
|
||||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue