mirror of
https://github.com/9technologygroup/patchmon.net.git
synced 2025-10-30 19:43:49 +00:00
refactor(backend): make /agent/download route more resilient
* Fixes early 404 return * Fixes filename when agent version undefined * Correctly returns 500 when error occurs
This commit is contained in:
@@ -44,19 +44,15 @@ router.get('/agent/download', async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (!agentVersion) {
|
||||
return res.status(404).json({error: 'No agent version available'});
|
||||
}
|
||||
|
||||
// Use script content from database if available, otherwise fallback to file
|
||||
if (agentVersion.script_content) {
|
||||
if (agentVersion && agentVersion.script_content) {
|
||||
// Convert Windows line endings to Unix line endings
|
||||
const scriptContent = agentVersion.script_content.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
||||
res.setHeader('Content-Type', 'application/x-shellscript');
|
||||
res.setHeader('Content-Disposition', `attachment; filename="patchmon-agent-${agentVersion.version}.sh"`);
|
||||
res.send(scriptContent);
|
||||
} else {
|
||||
// Fallback to file system
|
||||
// Fallback to file system when no database version exists or script has no content
|
||||
const agentPath = path.join(__dirname, '../../../agents/patchmon-agent.sh');
|
||||
if (!fs.existsSync(agentPath)) {
|
||||
return res.status(404).json({error: 'Agent script not found'});
|
||||
@@ -64,11 +60,13 @@ router.get('/agent/download', async (req, res) => {
|
||||
// Read file and convert line endings
|
||||
const scriptContent = fs.readFileSync(agentPath, 'utf8').replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
||||
res.setHeader('Content-Type', 'application/x-shellscript');
|
||||
res.setHeader('Content-Disposition', `attachment; filename="patchmon-agent-${agentVersion.version}.sh"`);
|
||||
const version = agentVersion ? `-${agentVersion.version}` : '';
|
||||
res.setHeader('Content-Disposition', `attachment; filename="patchmon-agent${version}.sh"`);
|
||||
res.send(scriptContent);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Agent download error:', error);
|
||||
res.status(500).json({error: 'Failed to download agent script'});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -945,10 +943,10 @@ router.get('/install', async (req, res) => {
|
||||
|
||||
let script = fs.readFileSync(scriptPath, 'utf8');
|
||||
|
||||
// Convert Windows line endings to Unix line endings
|
||||
// Convert Windows line endings to Unix line endings
|
||||
script = script.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
||||
|
||||
// Get the configured server URL from settings
|
||||
// Get the configured server URL from settings
|
||||
try {
|
||||
const settings = await prisma.settings.findFirst();
|
||||
if (settings) {
|
||||
|
||||
Reference in New Issue
Block a user