70 lines
1.6 KiB
TypeScript
70 lines
1.6 KiB
TypeScript
import { exec } from "node:child_process"
|
|
import { backupScripts, getBackupScriptCommand } from "../utils/backup-scripts"
|
|
|
|
function runCommand(command: string): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
exec(command, { timeout: 10 * 60 * 1000 }, (error, stdout, stderr) => {
|
|
if (error) {
|
|
reject(stderr || error.message)
|
|
return
|
|
}
|
|
resolve(stdout || stderr)
|
|
})
|
|
})
|
|
}
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const body = await readBody<{ key?: string }>(event)
|
|
const key = typeof body?.key === "string" ? body.key : null
|
|
|
|
if (!key) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: "Clé de script manquante"
|
|
})
|
|
}
|
|
|
|
const script = backupScripts.find((item) => item.key === key)
|
|
if (!script) {
|
|
throw createError({
|
|
statusCode: 404,
|
|
statusMessage: "Script introuvable"
|
|
})
|
|
}
|
|
|
|
try {
|
|
const command = getBackupScriptCommand(script.key)
|
|
if (!command) {
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: "Commande de script manquante"
|
|
})
|
|
}
|
|
|
|
const output = await runCommand(command)
|
|
return {
|
|
ok: true,
|
|
key: script.key,
|
|
label: script.label,
|
|
downloadFolders: script.downloadFolders || [],
|
|
output: output.trim()
|
|
}
|
|
} catch (error) {
|
|
console.error("Erreur execution script:", error)
|
|
|
|
if (
|
|
typeof error === "object" &&
|
|
error !== null &&
|
|
"statusCode" in error &&
|
|
"statusMessage" in error
|
|
) {
|
|
throw error
|
|
}
|
|
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: "Erreur lors de l'opération"
|
|
})
|
|
}
|
|
})
|