Add file deleting functionality to the admin panel

This commit is contained in:
2025-06-08 17:51:59 +09:00
parent f34c601ec0
commit 023a7ab926
3 changed files with 240 additions and 4 deletions

View File

@ -1,6 +1,7 @@
import { Database } from "bun:sqlite";
import crypto from "crypto";
import fs from "fs";
import path from "path";
import { config } from "./config.js";
// Database integrity fixes function
@ -1208,6 +1209,41 @@ export async function getFeedCategoryMigrationStatus(): Promise<{
}
}
export async function deleteEpisode(episodeId: string): Promise<boolean> {
try {
// Get episode info first to find the audio file path
const episodeStmt = db.prepare("SELECT audio_path FROM episodes WHERE id = ?");
const episode = episodeStmt.get(episodeId) as any;
if (!episode) {
return false; // Episode not found
}
// Delete from database
const deleteStmt = db.prepare("DELETE FROM episodes WHERE id = ?");
const result = deleteStmt.run(episodeId);
// If database deletion successful, try to delete the audio file
if (result.changes > 0 && episode.audio_path) {
try {
const fullAudioPath = path.join(config.paths.projectRoot, episode.audio_path);
if (fs.existsSync(fullAudioPath)) {
fs.unlinkSync(fullAudioPath);
console.log(`🗑️ Deleted audio file: ${fullAudioPath}`);
}
} catch (fileError) {
console.warn(`⚠️ Failed to delete audio file ${episode.audio_path}:`, fileError);
// Don't fail the operation if file deletion fails
}
}
return result.changes > 0;
} catch (error) {
console.error("Error deleting episode:", error);
throw error;
}
}
export function closeDatabase(): void {
db.close();
}