Skip to content

Commit b49ef7d

Browse files
committed
refactor: implement downloadBlob utility for streamlined backup downloads
1 parent 4b1d5f7 commit b49ef7d

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

client/src/app/tables/tables.service.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { environment } from '../../environments/environment';
88
import { Table } from '../../types';
99
import { SelectedDatabaseService } from '../database/selected-database.service';
10+
import { downloadBlob } from '../utils/downloadBlob';
1011
import { fetchJson } from '../utils/fetchJson';
1112

1213
@Injectable({
@@ -77,26 +78,25 @@ export class TablesService {
7778
}
7879
try {
7980
let downloadUrl: string;
81+
let filename: string;
8082

8183
// Use new streaming endpoints for all download types
8284
if (type === 'pgdump') {
8385
downloadUrl = environment.apiContextPath +
8486
`/database/${databaseName}/backup/${selectedBackup}/pgdump`;
87+
filename = selectedBackup.replace('.zip', '.pgdump');
8588
} else if (type === 'plain') {
8689
downloadUrl = environment.apiContextPath +
8790
`/database/${databaseName}/backup/${selectedBackup}/sql`;
91+
filename = selectedBackup.replace('.zip', '.sql');
8892
} else {
8993
downloadUrl = environment.apiContextPath +
9094
`/database/${databaseName}/backup/${selectedBackup}/archive`;
95+
filename = selectedBackup;
9196
}
9297

93-
// Trigger download by creating a temporary link
94-
const link = document.createElement('a');
95-
link.href = downloadUrl;
96-
link.download = ''; // Browser will use filename from Content-Disposition header
97-
document.body.appendChild(link);
98-
link.click();
99-
document.body.removeChild(link);
98+
// Use HttpClient to make authenticated request and download blob
99+
await downloadBlob(this.http, downloadUrl, filename);
100100
} catch (error) {
101101
dispatchEvent(new ErrorNotificationEvent('Could not download backup.'));
102102
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { HttpClient } from '@angular/common/http';
2+
import { firstValueFrom } from 'rxjs';
3+
4+
export async function downloadBlob(
5+
http: HttpClient,
6+
url: string,
7+
filename: string
8+
) {
9+
const blob = await firstValueFrom(
10+
http.get(url, {
11+
responseType: 'blob',
12+
observe: 'body',
13+
})
14+
);
15+
16+
// Create blob URL and trigger download
17+
const blobUrl = URL.createObjectURL(blob);
18+
const link = document.createElement('a');
19+
link.href = blobUrl;
20+
link.download = filename;
21+
document.body.appendChild(link);
22+
link.click();
23+
document.body.removeChild(link);
24+
25+
// Clean up blob URL
26+
URL.revokeObjectURL(blobUrl);
27+
}

0 commit comments

Comments
 (0)