Fix: Behandle leere Nextcloud-Dateien korrekt

- Aendert loadFile(): Gibt leeres Array zurueck, wenn Datei existiert aber leer ist
- Verhindert, dass leere Dateien als null interpretiert werden und lokale Daten
  oder leere Arrays geladen werden

Fixes: Datenverlust bei Nextcloud-Anmeldung mit leeren Dateien

Co-authored-by: trevor1969 <trevor1969@users.noreply.github.com>
This commit is contained in:
Vibe Nuage Agent
2026-06-05 11:26:26 +00:00
parent 7f059c9b4c
commit 7e1f52a5b2

48
app.js
View File

@ -135,7 +135,8 @@ class NextcloudStorage {
if (response.ok) {
const text = await response.text();
return text ? JSON.parse(text) : null;
// Return empty array if file is empty, otherwise parse JSON
return text.trim() === '' ? [] : (text ? JSON.parse(text) : null);
} else if (response.status === 404) {
return null;
}
@ -280,31 +281,19 @@ class TeeTracker {
const teas = await this.nextcloudStorage.loadFile(DATA_DIRECTORY + 'teas.json');
const entries = await this.nextcloudStorage.loadFile(DATA_DIRECTORY + 'entries.json');
// Load from Nextcloud if available
// Always load from localStorage first as backup
const localTeas = localStorage.getItem(STORAGE_KEY_TEAS);
const localEntries = localStorage.getItem(STORAGE_KEY_ENTRIES);
// Load from Nextcloud if available, otherwise use local data
if (teas) {
this.teas = teas.map(tea => ({
...tea,
organic: tea.organic !== undefined ? tea.organic : false,
rating: tea.rating !== undefined ? tea.rating : 3
}));
}
if (entries) {
this.entries = entries.map(entry => ({
...entry,
teaspoons: entry.teaspoons !== undefined ? entry.teaspoons : 1
}));
}
// If any data is missing from Nextcloud, load from localStorage
// This prevents data loss when switching to Nextcloud
if (!teas && !entries) {
// Both Nextcloud files are missing, load everything from localStorage
this.loadFromLocalStorage();
} else if (!teas) {
// Only teas.json is missing from Nextcloud, load teas from localStorage
const localTeas = localStorage.getItem(STORAGE_KEY_TEAS);
if (localTeas) {
} else if (localTeas) {
// Nextcloud has no teas, use local data
try {
this.teas = JSON.parse(localTeas).map(tea => ({
...tea,
@ -313,12 +302,19 @@ class TeeTracker {
}));
} catch (error) {
console.error('Error loading local teas:', error);
this.teas = [];
}
} else {
this.teas = [];
}
} else if (!entries) {
// Only entries.json is missing from Nextcloud, load entries from localStorage
const localEntries = localStorage.getItem(STORAGE_KEY_ENTRIES);
if (localEntries) {
if (entries) {
this.entries = entries.map(entry => ({
...entry,
teaspoons: entry.teaspoons !== undefined ? entry.teaspoons : 1
}));
} else if (localEntries) {
// Nextcloud has no entries, use local data
try {
this.entries = JSON.parse(localEntries).map(entry => ({
...entry,
@ -326,8 +322,10 @@ class TeeTracker {
}));
} catch (error) {
console.error('Error loading local entries:', error);
this.entries = [];
}
}
} else {
this.entries = [];
}
} else {
// Load from localStorage