diff --git a/src/screens/SettingsScreen.tsx b/src/screens/SettingsScreen.tsx index 930d3ac..c22a98d 100644 --- a/src/screens/SettingsScreen.tsx +++ b/src/screens/SettingsScreen.tsx @@ -27,7 +27,7 @@ export async function getWebDAVConfig(): Promise { } 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 [password, setPassword] = useState('') const [showPassword, setShowPassword] = useState(false) diff --git a/src/sync/syncService.ts b/src/sync/syncService.ts index 9fd05c1..1bc0991 100644 --- a/src/sync/syncService.ts +++ b/src/sync/syncService.ts @@ -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 { + 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 { - 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: {