diff --git a/app.js b/app.js index 8e770a6..920967b 100644 --- a/app.js +++ b/app.js @@ -8,6 +8,9 @@ const STORAGE_KEY_TEAS = 'teatracker_teas'; const STORAGE_KEY_ENTRIES = 'teatracker_entries'; const STORAGE_KEY_NC_CONFIG = 'teatracker_nextcloud_config'; +// Data directory for Nextcloud storage +const DATA_DIRECTORY = 'data/'; + // ============================================ // Nextcloud Storage Integration // ============================================ @@ -63,12 +66,44 @@ class NextcloudStorage { } } + + // Migrate old files from root to data/ directory + async migrateToDataDirectory() { + try { + // Check if old teas.json exists in root + const oldTeasUrl = this.getFileUrl('teas.json'); + const oldTeasResponse = await fetch(oldTeasUrl, { + headers: { 'Authorization': this.authHeader } + }); + + if (oldTeasResponse.ok) { + const oldTeas = await oldTeasResponse.json(); + await this.saveFile(DATA_DIRECTORY + 'teas.json', oldTeas); + console.log('Migrated teas.json to data/ directory'); + } + + // Check if old entries.json exists in root + const oldEntriesUrl = this.getFileUrl('entries.json'); + const oldEntriesResponse = await fetch(oldEntriesUrl, { + headers: { 'Authorization': this.authHeader } + }); + + if (oldEntriesResponse.ok) { + const oldEntries = await oldEntriesResponse.json(); + await this.saveFile(DATA_DIRECTORY + 'entries.json', oldEntries); + console.log('Migrated entries.json to data/ directory'); + } + } catch (error) { + console.log('No old files to migrate or migration failed:', error.message); + } + } async ensureDirectory() { try { const response = await fetch(this.getDirectoryUrl(), { method: 'MKCOL', headers: { 'Authorization': this.authHeader } }); + await this.ensureSubDirectory('data'); await this.ensureSubDirectory('backup'); return response.ok || response.status === 405; } catch (error) { @@ -144,6 +179,7 @@ class NextcloudStorage { async createBackup(backupName, teas, entries) { try { + await this.ensureSubDirectory('data'); await this.ensureSubDirectory('backup'); const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const backupData = { timestamp, teas, entries }; @@ -237,8 +273,11 @@ class TeeTracker { // Try to load from Nextcloud first await this.nextcloudStorage.ensureDirectory(); - const teas = await this.nextcloudStorage.loadFile('teas.json'); - const entries = await this.nextcloudStorage.loadFile('entries.json'); + // Check if old files exist in root directory and migrate to data/ directory + await this.migrateToDataDirectory(); + + const teas = await this.nextcloudStorage.loadFile(DATA_DIRECTORY + 'teas.json'); + const entries = await this.nextcloudStorage.loadFile(DATA_DIRECTORY + 'entries.json'); if (teas) { this.teas = teas.map(tea => ({ @@ -294,8 +333,8 @@ class TeeTracker { await this.nextcloudStorage.ensureDirectory(); // Save to Nextcloud - const teasSaved = await this.nextcloudStorage.saveFile('teas.json', this.teas); - const entriesSaved = await this.nextcloudStorage.saveFile('entries.json', this.entries); + const teasSaved = await this.nextcloudStorage.saveFile(DATA_DIRECTORY + 'teas.json', this.teas); + const entriesSaved = await this.nextcloudStorage.saveFile(DATA_DIRECTORY + 'entries.json', this.entries); // Also save locally as backup this.saveToLocalStorage(); @@ -938,8 +977,8 @@ class TeeTracker { localStorage.removeItem(STORAGE_KEY_ENTRIES); if (this.useNextcloud && this.nextcloudStorage) { - await this.nextcloudStorage.saveFile('teas.json', []); - await this.nextcloudStorage.saveFile('entries.json', []); + await this.nextcloudStorage.saveFile(DATA_DIRECTORY + 'teas.json', []); + await this.nextcloudStorage.saveFile(DATA_DIRECTORY + 'entries.json', []); } this.closeConfirmModal();