* Fix URL for CalDAV if necessary

* Implemented upload and delete of CalDAV items
This commit is contained in:
Andreas Schneider 2018-04-01 20:02:53 +02:00
parent e3288f9848
commit d7cc883cbb
2 changed files with 56 additions and 0 deletions

View File

@ -4,9 +4,11 @@ import (
"bufio"
"encoding/xml"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"text/template"
"time"
@ -174,6 +176,54 @@ func (c *CalDAV) GetEvents() ([]CalDAVItem, error) {
return result, nil
}
func (c CalDAV) UploadItem(item CalDAVItem) error {
itemURL, _ := url.Parse(item.HRef)
base, _ := url.Parse(c.URL)
// Build the absolute URL for the item
itemURL = base.ResolveReference(itemURL)
req, err := http.NewRequest("PUT", itemURL.String(), strings.NewReader(item.ICal.String()))
if err != nil {
return err
}
req.SetBasicAuth(c.Username, c.Password)
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode >= 300 {
return fmt.Errorf("Unexpected result while uploading %s: %d", itemURL.String(), resp.StatusCode)
}
return nil
}
func (c CalDAV) DeleteItem(item CalDAVItem) error {
itemURL, _ := url.Parse(item.HRef)
base, _ := url.Parse(c.URL)
// Build the absolute URL for the item
itemURL = base.ResolveReference(itemURL)
req, err := http.NewRequest("DELETE", itemURL.String(), nil)
if err != nil {
return err
}
req.SetBasicAuth(c.Username, c.Password)
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode >= 300 {
return fmt.Errorf("Unexpected result while deleting %s: %d", itemURL.String(), resp.StatusCode)
}
return nil
}
type MultiStatus struct {
Response []PropFindResponse `xml:"response"`
}

View File

@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"os"
"strings"
)
type ServerSettings struct {
@ -29,5 +30,10 @@ func LoadSettings() Settings {
if err != nil {
panic(err)
}
if settings.CalDAV.URL != "" && strings.HasSuffix(settings.CalDAV.URL, "/") {
settings.CalDAV.URL += "/"
}
return settings
}