Skip to content

A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.

fix: resolves server crash on missing snapshot download

towfiqidd2d69cb23b0
diff --git a/backend/__tests__/controllers/BackupController.test.ts b/backend/__tests__/controllers/BackupController.test.ts
index 7a7d362..341cf6f 100644
--- a/backend/__tests__/controllers/BackupController.test.ts
+++ b/backend/__tests__/controllers/BackupController.test.ts
@@ -59,7 +59,7 @@ describe('BackupController', () => {
});
it('should successfully stream backup download', async () => {
- const mockFileStream = { pipe: mockPipe };
+ const mockFileStream = { pipe: mockPipe, on: jest.fn() };
mockRequest.params = { id: 'backup-123' };
mockBackupService.getBackupDownload.mockResolvedValue({
fileName: 'backup.tar',
diff --git a/backend/src/controllers/BackupController.ts b/backend/src/controllers/BackupController.ts
index f48a63e..d89222e 100644
--- a/backend/src/controllers/BackupController.ts
+++ b/backend/src/controllers/BackupController.ts
@@ -22,11 +22,24 @@ export class BackupController {
res.setHeader('Content-Disposition', `attachment; filename=${fileName}`);
res.setHeader('Transfer-Encoding', 'chunked');
+ fileStream.on('error', (error: Error) => {
+ if (res.headersSent) {
+ res.destroy(error);
+ return;
+ }
+ res.removeHeader('Content-Disposition');
+ res.removeHeader('Transfer-Encoding');
+ res.status(500).type('json').json({
+ success: false,
+ error: 'Failed to get Downloaded file. ' + (error.message || ''),
+ });
+ });
+
// Pipe the stream directly to response
fileStream.pipe(res);
return;
} catch (error: any) {
- res.status(500).json({
+ res.status(error.statusCode || 500).json({
success: false,
error: 'Failed to get Downloaded file. ' + (error.message || ''),
});
diff --git a/backend/src/managers/handlers/DownloadHandler.ts b/backend/src/managers/handlers/DownloadHandler.ts
index 3271817..a5f8b17 100644
--- a/backend/src/managers/handlers/DownloadHandler.ts
+++ b/backend/src/managers/handlers/DownloadHandler.ts
@@ -10,6 +10,9 @@ import { runCommand } from '../../utils/runCommand';
import { appPaths } from '../../utils/AppPaths';
import { configService } from '../../services/ConfigService';
+export const DOWNLOAD_NOT_FOUND_ERROR =
+ 'Download file not found. Please generate the download again.';
+
export class DownloadHandler {
constructor(private emitter: EventEmitter) {}
@@ -132,6 +135,10 @@ export class DownloadHandler {
const fileName = `backup-${backupId}.tar`;
const zipPath = path.join(tempDir, fileName);
+ if (!existsSync(zipPath)) {
+ throw new Error(DOWNLOAD_NOT_FOUND_ERROR);
+ }
+
const fileStream = createReadStream(zipPath);
return { fileName, fileStream };
} catch (error: any) {
diff --git a/backend/src/services/BackupServices.ts b/backend/src/services/BackupServices.ts
index 7e17e56..89104f6 100644
--- a/backend/src/services/BackupServices.ts
+++ b/backend/src/services/BackupServices.ts
@@ -1,5 +1,6 @@
import { sql } from 'drizzle-orm';
import { BaseSnapshotManager } from '../managers/BaseSnapshotManager';
+import { DOWNLOAD_NOT_FOUND_ERROR } from '../managers/handlers/DownloadHandler';
import { BackupStore } from '../stores/BackupStore';
import { StorageStore } from '../stores/StorageStore';
import { RestoreStore } from '../stores/RestoreStore';
@@ -103,6 +104,13 @@ export class BackupService {
const strategy = this.getSnapshotStrategy(backupDevice, backup.method);
const downloadResult = await strategy.getSnapshotDownload(backup.planId as string, backupId);
if (!downloadResult.success) {
+ if (
+ typeof downloadResult.result === 'string' &&
+ downloadResult.result.includes(DOWNLOAD_NOT_FOUND_ERROR)
+ ) {
+ await this.backupStore.update(backupId, { download: null });
+ throw new NotFoundError(DOWNLOAD_NOT_FOUND_ERROR);
+ }
throw new Error(
typeof downloadResult.result === 'string'
? downloadResult.result
diff --git a/frontend/src/services/backups.ts b/frontend/src/services/backups.ts
index 7728272..95bef67 100644
--- a/frontend/src/services/backups.ts
+++ b/frontend/src/services/backups.ts
@@ -73,13 +73,16 @@ export async function getBackupDownload(id: string) {
}
export function useGetBackupDownload() {
+ const queryClient = useQueryClient();
return useMutation({
mutationFn: getBackupDownload,
onSuccess: (res) => {
console.log('# Backup Downloaded! :', res);
},
- onError: (res) => {
- console.log('# Download Failed! :', res);
+ onError: (error) => {
+ console.log('# Download Failed! :', error);
+ toast.error(error.message || 'Download Failed!');
+ queryClient.invalidateQueries({ queryKey: ['plan'] });
},
});
}