100 lines
2.6 KiB
Go
100 lines
2.6 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"linkport/internal/store"
|
|
"linkport/internal/syncjob"
|
|
"net/http"
|
|
)
|
|
|
|
func webdavSettingsView(c store.WebDAVSyncSettings) map[string]any {
|
|
return map[string]any{
|
|
"endpoint": c.Endpoint, "prefix": c.Prefix, "username": c.Username,
|
|
"hasPassword": c.Password != "", "automatic": c.Automatic,
|
|
"intervalMinutes": c.IntervalMinutes,
|
|
}
|
|
}
|
|
|
|
func (s *Server) webdavSync(w http.ResponseWriter, r *http.Request, path string) {
|
|
switch path {
|
|
case "configuration/webdav/settings":
|
|
if r.Method != http.MethodGet && r.Method != http.MethodPut {
|
|
fail(w, 405, "method not allowed")
|
|
return
|
|
}
|
|
s.ops.Lock()
|
|
defer s.ops.Unlock()
|
|
c, err := s.store.WebDAVSyncSettings()
|
|
if err != nil {
|
|
fail(w, 500, "cannot load WebDAV sync settings")
|
|
return
|
|
}
|
|
if r.Method == http.MethodPut {
|
|
var body struct {
|
|
store.WebDAVSyncSettings
|
|
ClearPassword bool `json:"clearPassword"`
|
|
}
|
|
if !decode(w, r, &body) {
|
|
return
|
|
}
|
|
if body.ClearPassword {
|
|
body.Password = ""
|
|
} else if body.Password == "" && body.Endpoint == c.Endpoint && body.Username == c.Username {
|
|
body.Password = c.Password
|
|
}
|
|
if err = body.WebDAVSyncSettings.Validate(); err != nil {
|
|
fail(w, 400, err.Error())
|
|
return
|
|
}
|
|
if err = s.store.SaveWebDAVSyncSettings(body.WebDAVSyncSettings); err != nil {
|
|
fail(w, 500, "cannot save WebDAV sync settings")
|
|
return
|
|
}
|
|
c = body.WebDAVSyncSettings
|
|
s.store.Event("", source(r), "webdav_sync_settings_updated", "")
|
|
}
|
|
respond(w, 200, webdavSettingsView(c))
|
|
case "configuration/webdav/status":
|
|
if r.Method != http.MethodGet {
|
|
fail(w, 405, "method not allowed")
|
|
return
|
|
}
|
|
c, err := s.store.WebDAVSyncSettings()
|
|
if err != nil {
|
|
fail(w, 500, "cannot load WebDAV sync settings")
|
|
return
|
|
}
|
|
state, err := s.store.WebDAVSyncState()
|
|
if err != nil {
|
|
fail(w, 500, "cannot load WebDAV sync status")
|
|
return
|
|
}
|
|
respond(w, 200, map[string]any{"state": state, "nextRun": syncjob.NextRun(c, state)})
|
|
case "configuration/webdav/sync", "configuration/webdav/backup":
|
|
if r.Method != http.MethodPost {
|
|
fail(w, 405, "method not allowed")
|
|
return
|
|
}
|
|
var body syncjob.Request
|
|
if !decode(w, r, &body) {
|
|
return
|
|
}
|
|
body.BackupOnly = path == "configuration/webdav/backup"
|
|
result, err := s.syncer.Run(r.Context(), source(r), body)
|
|
if err != nil {
|
|
status := 502
|
|
if errors.Is(err, syncjob.ErrBusy) || errors.Is(err, syncjob.ErrConflict) || errors.Is(err, syncjob.ErrRunning) {
|
|
status = 409
|
|
}
|
|
if errors.Is(err, syncjob.ErrSettings) {
|
|
status = 400
|
|
}
|
|
fail(w, status, err.Error())
|
|
return
|
|
}
|
|
respond(w, 200, result)
|
|
default:
|
|
fail(w, 404, "not found")
|
|
}
|
|
}
|