fix: correct WebDAV URL and auto-create weegschaal directory on sync

This commit is contained in:
Eddy de Vink 2026-06-27 23:13:54 +02:00
commit 129ac2c241
2 changed files with 21 additions and 2 deletions

View file

@ -42,13 +42,32 @@ async function fetchRemote(config: { url: string; username: string; password: st
return response.json()
}
async function createDir(config: { url: string; username: string; password: string }): Promise<void> {
const base = config.url.replace(/\/+$/, '')
const dirUrl = `${base}/weegschaal`
const response = await fetch(dirUrl, {
method: 'MKCOL',
headers: {
Authorization: 'Basic ' + btoa(`${config.username}:${config.password}`),
},
})
if (response.status !== 201 && response.status !== 405) {
throw new Error(`WebDAV directory create failed: ${response.status}`)
}
}
async function uploadRemote(
config: { url: string; username: string; password: string },
measurements: Measurement[]
): Promise<void> {
const url = `${config.url.replace(/\/+$/, '')}/${WEBDAV_PATH}`
const base = config.url.replace(/\/+$/, '')
const url = `${base}/${WEBDAV_PATH}`
const body = JSON.stringify(measurements)
await createDir(config)
const response = await fetch(url, {
method: 'PUT',
headers: {