mirror of
https://github.com/Mikescher/kpsync.git
synced 2025-10-24 21:55:08 +02:00
logging, upload, etc
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/cryptext"
|
||||
@@ -11,6 +13,8 @@ import (
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/timeext"
|
||||
)
|
||||
|
||||
var ETagConflictError = errors.New("ETag conflict")
|
||||
|
||||
func (app *Application) downloadDatabase() (string, time.Time, string, int64, error) {
|
||||
|
||||
client := http.Client{Timeout: 90 * time.Second}
|
||||
@@ -40,14 +44,13 @@ func (app *Application) downloadDatabase() (string, time.Time, string, int64, er
|
||||
if etag == "" {
|
||||
return "", time.Time{}, "", 0, exerr.New(exerr.TypeInternal, "ETag header is missing").Build()
|
||||
}
|
||||
etag = strings.Trim(etag, "\"\r\n ")
|
||||
|
||||
lmStr := resp.Header.Get("Last-Modified")
|
||||
|
||||
lm, err := time.Parse("Mon, 02 Jan 2006 15:04:05 MST", lmStr)
|
||||
if err != nil {
|
||||
return "", time.Time{}, "", 0, exerr.Wrap(err, "Failed to parse Last-Modified header").Build()
|
||||
}
|
||||
|
||||
lm = lm.In(timeext.TimezoneBerlin)
|
||||
|
||||
sha := cryptext.BytesSha256(bin)
|
||||
@@ -85,6 +88,7 @@ func (app *Application) getRemoteETag() (string, time.Time, error) {
|
||||
if etag == "" {
|
||||
return "", time.Time{}, exerr.New(exerr.TypeInternal, "ETag header is missing").Build()
|
||||
}
|
||||
etag = strings.Trim(etag, "\"\r\n ")
|
||||
|
||||
lmStr := resp.Header.Get("Last-Modified")
|
||||
|
||||
@@ -97,3 +101,58 @@ func (app *Application) getRemoteETag() (string, time.Time, error) {
|
||||
|
||||
return etag, lm, nil
|
||||
}
|
||||
|
||||
func (app *Application) uploadDatabase(etagIfMatch *string) (string, time.Time, string, int64, error) {
|
||||
|
||||
client := http.Client{Timeout: 90 * time.Second}
|
||||
|
||||
req, err := http.NewRequest("PUT", app.config.WebDAVURL, nil)
|
||||
if err != nil {
|
||||
return "", time.Time{}, "", 0, exerr.Wrap(err, "").Build()
|
||||
}
|
||||
|
||||
if etagIfMatch != nil {
|
||||
req.Header.Set("If-Match", "\""+*etagIfMatch+"\"")
|
||||
}
|
||||
|
||||
bin, err := os.ReadFile(app.dbFile)
|
||||
if err != nil {
|
||||
return "", time.Time{}, "", 0, exerr.Wrap(err, "Failed to read database file").Build()
|
||||
}
|
||||
|
||||
sha := cryptext.BytesSha256(bin)
|
||||
|
||||
sz := int64(len(bin))
|
||||
|
||||
req.ContentLength = sz
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", time.Time{}, "", 0, exerr.Wrap(err, "Failed to upload remote database").Build()
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusCreated || resp.StatusCode == http.StatusNoContent {
|
||||
|
||||
etag := resp.Header.Get("ETag")
|
||||
if etag == "" {
|
||||
return "", time.Time{}, "", 0, exerr.New(exerr.TypeInternal, "ETag header is missing").Build()
|
||||
}
|
||||
etag = strings.Trim(etag, "\"\r\n ")
|
||||
|
||||
lmStr := resp.Header.Get("Last-Modified")
|
||||
lm, err := time.Parse("Mon, 02 Jan 2006 15:04:05 MST", lmStr)
|
||||
if err != nil {
|
||||
return "", time.Time{}, "", 0, exerr.Wrap(err, "Failed to parse Last-Modified header").Build()
|
||||
}
|
||||
lm = lm.In(timeext.TimezoneBerlin)
|
||||
|
||||
return etag, lm, sha, sz, nil
|
||||
}
|
||||
|
||||
if resp.StatusCode == http.StatusPreconditionFailed {
|
||||
return "", time.Time{}, "", 0, ETagConflictError
|
||||
}
|
||||
|
||||
return "", time.Time{}, "", 0, exerr.New(exerr.TypeInternal, "Failed to upload remote database").Int("sc", resp.StatusCode).Build()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user