Fix browser freezing when downloading large files (#1304)
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {BehaviorSubject, Subject} from 'rxjs';
|
||||
|
||||
export interface DownloadProgress {
|
||||
visible: boolean;
|
||||
filename: string;
|
||||
progress: number;
|
||||
loaded: number;
|
||||
total: number;
|
||||
cancelSubject?: Subject<void>;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class DownloadProgressService {
|
||||
private downloadProgressSubject = new BehaviorSubject<DownloadProgress>({
|
||||
visible: false,
|
||||
filename: '',
|
||||
progress: 0,
|
||||
loaded: 0,
|
||||
total: 0
|
||||
});
|
||||
|
||||
private lastUpdateTime = 0;
|
||||
private updateThrottleMs = 100;
|
||||
private pendingUpdate: DownloadProgress | null = null;
|
||||
private throttleTimer: any = null;
|
||||
|
||||
downloadProgress$ = this.downloadProgressSubject.asObservable();
|
||||
|
||||
isDownloadInProgress(): boolean {
|
||||
return this.downloadProgressSubject.value.visible;
|
||||
}
|
||||
|
||||
startDownload(filename: string, cancelSubject: Subject<void>): void {
|
||||
this.lastUpdateTime = 0;
|
||||
this.pendingUpdate = null;
|
||||
if (this.throttleTimer) {
|
||||
clearTimeout(this.throttleTimer);
|
||||
this.throttleTimer = null;
|
||||
}
|
||||
|
||||
this.downloadProgressSubject.next({
|
||||
visible: true,
|
||||
filename,
|
||||
progress: 0,
|
||||
loaded: 0,
|
||||
total: 0,
|
||||
cancelSubject
|
||||
});
|
||||
}
|
||||
|
||||
updateProgress(loaded: number, total: number): void {
|
||||
const current = this.downloadProgressSubject.value;
|
||||
const progress = total > 0 ? Math.min(100, Math.round((loaded / total) * 100)) : 0;
|
||||
const now = Date.now();
|
||||
|
||||
const newProgress: DownloadProgress = {
|
||||
visible: current.visible,
|
||||
filename: current.filename,
|
||||
progress,
|
||||
loaded,
|
||||
total,
|
||||
cancelSubject: current.cancelSubject
|
||||
};
|
||||
|
||||
if (progress === 100) {
|
||||
if (this.throttleTimer) {
|
||||
clearTimeout(this.throttleTimer);
|
||||
this.throttleTimer = null;
|
||||
}
|
||||
this.downloadProgressSubject.next(newProgress);
|
||||
this.lastUpdateTime = now;
|
||||
this.pendingUpdate = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (now - this.lastUpdateTime >= this.updateThrottleMs) {
|
||||
this.downloadProgressSubject.next(newProgress);
|
||||
this.lastUpdateTime = now;
|
||||
this.pendingUpdate = null;
|
||||
|
||||
if (this.throttleTimer) {
|
||||
clearTimeout(this.throttleTimer);
|
||||
this.throttleTimer = null;
|
||||
}
|
||||
} else {
|
||||
this.pendingUpdate = newProgress;
|
||||
|
||||
if (!this.throttleTimer) {
|
||||
const remainingTime = this.updateThrottleMs - (now - this.lastUpdateTime);
|
||||
this.throttleTimer = setTimeout(() => {
|
||||
if (this.pendingUpdate) {
|
||||
this.downloadProgressSubject.next(this.pendingUpdate);
|
||||
this.lastUpdateTime = Date.now();
|
||||
this.pendingUpdate = null;
|
||||
}
|
||||
this.throttleTimer = null;
|
||||
}, remainingTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
completeDownload(): void {
|
||||
if (this.throttleTimer) {
|
||||
clearTimeout(this.throttleTimer);
|
||||
this.throttleTimer = null;
|
||||
}
|
||||
this.pendingUpdate = null;
|
||||
|
||||
this.downloadProgressSubject.next({
|
||||
visible: false,
|
||||
filename: '',
|
||||
progress: 0,
|
||||
loaded: 0,
|
||||
total: 0
|
||||
});
|
||||
}
|
||||
|
||||
cancelDownload(): void {
|
||||
const current = this.downloadProgressSubject.value;
|
||||
current.cancelSubject?.next();
|
||||
current.cancelSubject?.complete();
|
||||
this.completeDownload();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
import {Injectable, inject} from '@angular/core';
|
||||
import {HttpClient, HttpEvent, HttpEventType, HttpResponse} from '@angular/common/http';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {DownloadProgressService} from './download-progress.service';
|
||||
import {Observable, Subject, throwError} from 'rxjs';
|
||||
import {catchError, finalize, takeUntil, tap} from 'rxjs/operators';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class FileDownloadService {
|
||||
private http = inject(HttpClient);
|
||||
private downloadProgressService = inject(DownloadProgressService);
|
||||
private messageService = inject(MessageService);
|
||||
|
||||
downloadFile(url: string, defaultFilename: string): void {
|
||||
const cancelSubject = new Subject<void>();
|
||||
|
||||
this.initiateDownload(url)
|
||||
.pipe(
|
||||
takeUntil(cancelSubject),
|
||||
tap(event => this.handleDownloadProgress(event, defaultFilename, cancelSubject)),
|
||||
finalize(() => this.downloadProgressService.completeDownload()),
|
||||
catchError(error => {
|
||||
this.handleDownloadError(error);
|
||||
return throwError(() => error);
|
||||
})
|
||||
)
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
private initiateDownload(url: string): Observable<HttpEvent<Blob>> {
|
||||
return this.http.get(url, {
|
||||
responseType: 'blob',
|
||||
observe: 'events',
|
||||
reportProgress: true
|
||||
});
|
||||
}
|
||||
|
||||
private handleDownloadProgress(
|
||||
event: HttpEvent<Blob>,
|
||||
defaultFilename: string,
|
||||
cancelSubject: Subject<void>
|
||||
): void {
|
||||
if (event.type === HttpEventType.Response) {
|
||||
this.handleDownloadComplete(event, defaultFilename);
|
||||
} else if (event.type === HttpEventType.DownloadProgress) {
|
||||
this.updateProgress(event, defaultFilename, cancelSubject);
|
||||
}
|
||||
}
|
||||
|
||||
private updateProgress(
|
||||
event: any,
|
||||
defaultFilename: string,
|
||||
cancelSubject: Subject<void>
|
||||
): void {
|
||||
if (event.total) {
|
||||
if (!this.downloadProgressService.isDownloadInProgress()) {
|
||||
this.downloadProgressService.startDownload(defaultFilename, cancelSubject);
|
||||
}
|
||||
this.downloadProgressService.updateProgress(event.loaded, event.total);
|
||||
}
|
||||
}
|
||||
|
||||
private handleDownloadComplete(response: HttpResponse<Blob>, defaultFilename: string): void {
|
||||
const filename = this.extractFilenameFromResponse(response, defaultFilename);
|
||||
const blob = response.body;
|
||||
|
||||
if (!blob) {
|
||||
throw new Error('No file content received');
|
||||
}
|
||||
|
||||
this.triggerBrowserDownload(blob, filename);
|
||||
this.showSuccessMessage(filename);
|
||||
}
|
||||
|
||||
private extractFilenameFromResponse(response: HttpResponse<Blob>, defaultFilename: string): string {
|
||||
const contentDisposition = response.headers.get('Content-Disposition');
|
||||
return contentDisposition
|
||||
? contentDisposition.match(/filename="(.+?)"/)?.[1] || defaultFilename
|
||||
: defaultFilename;
|
||||
}
|
||||
|
||||
private triggerBrowserDownload(blob: Blob, filename: string): void {
|
||||
const objectUrl = window.URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
link.href = objectUrl;
|
||||
link.download = filename;
|
||||
link.style.display = 'none';
|
||||
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(link);
|
||||
window.URL.revokeObjectURL(objectUrl);
|
||||
}, 100);
|
||||
}
|
||||
|
||||
private handleDownloadError(error: any): void {
|
||||
if (error?.name !== 'AbortError') {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Download Failed',
|
||||
detail: 'An error occurred while downloading the file. Please try again.',
|
||||
life: 5000
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private showSuccessMessage(filename: string): void {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Download Complete',
|
||||
detail: `${filename} has been downloaded successfully.`,
|
||||
life: 3000
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user