mirror of
https://github.com/trevor1969/teatracker.git
synced 2026-08-09 10:41:59 +00:00
feat: Store data in data/ subdirectory for better organization
- Add DATA_DIRECTORY constant ('data/')
- Update all file paths to use data/teas.json and data/entries.json
- Add ensureSubDirectory('data') call to create data directory
- Add migrateToDataDirectory() method to automatically migrate old files
- Migration runs on first load if old files exist in root directory
Co-authored-by: trevor1969 <trevor1969@users.noreply.github.com>
This commit is contained in:
51
app.js
51
app.js
@ -8,6 +8,9 @@ const STORAGE_KEY_TEAS = 'teatracker_teas';
|
|||||||
const STORAGE_KEY_ENTRIES = 'teatracker_entries';
|
const STORAGE_KEY_ENTRIES = 'teatracker_entries';
|
||||||
const STORAGE_KEY_NC_CONFIG = 'teatracker_nextcloud_config';
|
const STORAGE_KEY_NC_CONFIG = 'teatracker_nextcloud_config';
|
||||||
|
|
||||||
|
// Data directory for Nextcloud storage
|
||||||
|
const DATA_DIRECTORY = 'data/';
|
||||||
|
|
||||||
// ============================================
|
// ============================================
|
||||||
// Nextcloud Storage Integration
|
// 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() {
|
async ensureDirectory() {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(this.getDirectoryUrl(), {
|
const response = await fetch(this.getDirectoryUrl(), {
|
||||||
method: 'MKCOL',
|
method: 'MKCOL',
|
||||||
headers: { 'Authorization': this.authHeader }
|
headers: { 'Authorization': this.authHeader }
|
||||||
});
|
});
|
||||||
|
await this.ensureSubDirectory('data');
|
||||||
await this.ensureSubDirectory('backup');
|
await this.ensureSubDirectory('backup');
|
||||||
return response.ok || response.status === 405;
|
return response.ok || response.status === 405;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -144,6 +179,7 @@ class NextcloudStorage {
|
|||||||
|
|
||||||
async createBackup(backupName, teas, entries) {
|
async createBackup(backupName, teas, entries) {
|
||||||
try {
|
try {
|
||||||
|
await this.ensureSubDirectory('data');
|
||||||
await this.ensureSubDirectory('backup');
|
await this.ensureSubDirectory('backup');
|
||||||
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
||||||
const backupData = { timestamp, teas, entries };
|
const backupData = { timestamp, teas, entries };
|
||||||
@ -237,8 +273,11 @@ class TeeTracker {
|
|||||||
// Try to load from Nextcloud first
|
// Try to load from Nextcloud first
|
||||||
await this.nextcloudStorage.ensureDirectory();
|
await this.nextcloudStorage.ensureDirectory();
|
||||||
|
|
||||||
const teas = await this.nextcloudStorage.loadFile('teas.json');
|
// Check if old files exist in root directory and migrate to data/ directory
|
||||||
const entries = await this.nextcloudStorage.loadFile('entries.json');
|
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) {
|
if (teas) {
|
||||||
this.teas = teas.map(tea => ({
|
this.teas = teas.map(tea => ({
|
||||||
@ -294,8 +333,8 @@ class TeeTracker {
|
|||||||
await this.nextcloudStorage.ensureDirectory();
|
await this.nextcloudStorage.ensureDirectory();
|
||||||
|
|
||||||
// Save to Nextcloud
|
// Save to Nextcloud
|
||||||
const teasSaved = await this.nextcloudStorage.saveFile('teas.json', this.teas);
|
const teasSaved = await this.nextcloudStorage.saveFile(DATA_DIRECTORY + 'teas.json', this.teas);
|
||||||
const entriesSaved = await this.nextcloudStorage.saveFile('entries.json', this.entries);
|
const entriesSaved = await this.nextcloudStorage.saveFile(DATA_DIRECTORY + 'entries.json', this.entries);
|
||||||
|
|
||||||
// Also save locally as backup
|
// Also save locally as backup
|
||||||
this.saveToLocalStorage();
|
this.saveToLocalStorage();
|
||||||
@ -938,8 +977,8 @@ class TeeTracker {
|
|||||||
localStorage.removeItem(STORAGE_KEY_ENTRIES);
|
localStorage.removeItem(STORAGE_KEY_ENTRIES);
|
||||||
|
|
||||||
if (this.useNextcloud && this.nextcloudStorage) {
|
if (this.useNextcloud && this.nextcloudStorage) {
|
||||||
await this.nextcloudStorage.saveFile('teas.json', []);
|
await this.nextcloudStorage.saveFile(DATA_DIRECTORY + 'teas.json', []);
|
||||||
await this.nextcloudStorage.saveFile('entries.json', []);
|
await this.nextcloudStorage.saveFile(DATA_DIRECTORY + 'entries.json', []);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.closeConfirmModal();
|
this.closeConfirmModal();
|
||||||
|
|||||||
Reference in New Issue
Block a user