fix: correct WebDAV URL and auto-create weegschaal directory on sync
This commit is contained in:
parent
e038ba1ad9
commit
129ac2c241
2 changed files with 21 additions and 2 deletions
|
|
@ -27,7 +27,7 @@ export async function getWebDAVConfig(): Promise<WebDAVConfig | null> {
|
|||
}
|
||||
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -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: {
|
||||
|
|
|
|||
Loading…
Reference in a new issue