diff --git a/README.md b/README.md index c58b9e90..caa495be 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,9 @@ export interface UploadProgress { data?: { percentage: number; // percentage of upload already completed speed: number; // current upload speed per second in bytes - speedHuman: string; // current upload speed per second in human readable form + speedHuman: string; // current upload speed per second in human readable form, + eta: number; // estimated time remaining in seconds + etaHuman: string; // estimated time remaining in human readable format }; } diff --git a/package.json b/package.json index 398ecd97..2e516019 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ngx-uploader", - "version": "3.3.5", + "version": "3.3.6", "main": "bundles/ngx-uploader.umd.js", "module": "index.js", "esnext": "index.js", diff --git a/src/ngx-uploader/classes/ngx-uploader.class.ts b/src/ngx-uploader/classes/ngx-uploader.class.ts index ff1228b7..c77c4af0 100644 --- a/src/ngx-uploader/classes/ngx-uploader.class.ts +++ b/src/ngx-uploader/classes/ngx-uploader.class.ts @@ -20,6 +20,8 @@ export interface UploadProgress { speedHuman: string; startTime: number | null; endTime: number | null; + eta: number | null; + etaHuman: string | null; }; } @@ -96,7 +98,9 @@ export class NgUploaderService { speed: 0, speedHuman: `${humanizeBytes(0)}/s`, startTime: null, - endTime: null + endTime: null, + eta: null, + etaHuman: null } }, lastModifiedDate: file.lastModifiedDate, @@ -193,6 +197,7 @@ export class NgUploaderService { let time: number = new Date().getTime(); let progressStartTime: number = (file.progress.data && file.progress.data.startTime) || time; let speed = 0; + let eta: number | null = null; xhr.upload.addEventListener('progress', (e: ProgressEvent) => { if (e.lengthComputable) { @@ -200,6 +205,7 @@ export class NgUploaderService { const diff = new Date().getTime() - time; speed = Math.round(e.loaded / diff * 1000); progressStartTime = (file.progress.data && file.progress.data.startTime) || new Date().getTime(); + eta = Math.ceil((e.total - e.loaded) / speed); file.progress = { status: UploadStatus.Uploading, @@ -208,7 +214,9 @@ export class NgUploaderService { speed: speed, speedHuman: `${humanizeBytes(speed)}/s`, startTime: progressStartTime, - endTime: null + endTime: null, + eta: eta, + etaHuman: this.secondsToHuman(eta) } }; @@ -231,7 +239,9 @@ export class NgUploaderService { speed: speedAverage, speedHuman: `${humanizeBytes(speedAverage)}/s`, startTime: progressStartTime, - endTime: new Date().getTime() + endTime: new Date().getTime(), + eta: eta, + etaHuman: this.secondsToHuman(eta || 0) } }; @@ -276,6 +286,10 @@ export class NgUploaderService { }); } + secondsToHuman(sec: number): string { + return new Date(sec * 1000).toISOString().substr(11, 8); + } + generateId(): string { return Math.random().toString(36).substring(7); }