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

@ -27,7 +27,7 @@ export async function getWebDAVConfig(): Promise<WebDAVConfig | null> {
} }
export default function SettingsScreen() { export default function SettingsScreen() {
const [url, setUrl] = useState('https://nextcloud.eddydevink.nl') const [url, setUrl] = useState('https://nextcloud.eddydevink.nl/remote.php/dav/files/eddy')
const [username, setUsername] = useState('') const [username, setUsername] = useState('')
const [password, setPassword] = useState('') const [password, setPassword] = useState('')
const [showPassword, setShowPassword] = useState(false) const [showPassword, setShowPassword] = useState(false)

View file

@ -42,13 +42,32 @@ async function fetchRemote(config: { url: string; username: string; password: st
return response.json() 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( async function uploadRemote(
config: { url: string; username: string; password: string }, config: { url: string; username: string; password: string },
measurements: Measurement[] measurements: Measurement[]
): Promise<void> { ): Promise<void> {
const url = `${config.url.replace(/\/+$/, '')}/${WEBDAV_PATH}` const base = config.url.replace(/\/+$/, '')
const url = `${base}/${WEBDAV_PATH}`
const body = JSON.stringify(measurements) const body = JSON.stringify(measurements)
await createDir(config)
const response = await fetch(url, { const response = await fetch(url, {
method: 'PUT', method: 'PUT',
headers: { headers: {