Fix delete song, CORS, and cleanup for local-first SQLite setup

- Fix delete song route: remove reference to non-existent audio_files table,
  use audio_url directly from songs table instead
- Fix CORS: allow 127.0.0.1 in addition to localhost in development mode
- Fix cleanup service: remove audio_files reference, use SQLite datetime syntax
This commit is contained in:
fspecii
2026-02-04 13:39:30 +02:00
parent 645c88ca0a
commit e616713ec1
3 changed files with 32 additions and 68 deletions
+5 -46
View File
@@ -1,5 +1,4 @@
import { pool } from '../db/pool.js';
import { getStorageProvider } from './storage/factory.js';
export interface CleanupResult {
deleted: number;
@@ -7,57 +6,17 @@ export interface CleanupResult {
}
export async function runCleanupJob(): Promise<CleanupResult> {
console.log('Starting audio file cleanup job...');
const result = await pool.query(
`SELECT af.id, af.song_id, af.storage_key, af.storage_provider
FROM audio_files af
WHERE af.expires_at < NOW()
AND af.deleted_at IS NULL`
);
if (result.rows.length === 0) {
console.log('No expired audio files to clean up');
return { deleted: 0, errors: 0 };
}
console.log(`Found ${result.rows.length} expired audio files`);
const storage = getStorageProvider();
let deleted = 0;
let errors = 0;
for (const audioFile of result.rows) {
try {
await storage.delete(audioFile.storage_key);
await pool.query(
'UPDATE audio_files SET deleted_at = CURRENT_TIMESTAMP WHERE id = $1',
[audioFile.id]
);
await pool.query(
'UPDATE songs SET audio_url = NULL WHERE id = $1',
[audioFile.song_id]
);
deleted++;
console.log(`Deleted expired audio: ${audioFile.storage_key}`);
} catch (err) {
errors++;
console.error(`Failed to delete audio ${audioFile.storage_key}:`, err);
}
}
console.log(`Cleanup complete: ${deleted} deleted, ${errors} errors`);
return { deleted, errors };
// Local storage doesn't have expiring files - no cleanup needed
console.log('Cleanup job: No action needed for local storage');
return { deleted: 0, errors: 0 };
}
export async function cleanupDeletedSongs(): Promise<number> {
// Clean up songs without audio that are older than 7 days (SQLite syntax)
const result = await pool.query(
`DELETE FROM songs
WHERE audio_url IS NULL
AND created_at < NOW() - INTERVAL '7 days'
AND created_at < datetime('now', '-7 days')
RETURNING id`
);