Skip to content

Commit

Permalink
fix(speed): fix upload speed calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Jun 25, 2017
1 parent e3bfb5e commit a805955
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/components/app-home/app-home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class AppHomeComponent {
startUpload(): void {
const event: UploadInput = {
type: 'uploadAll',
url: '/upload',
url: 'http://ngx-uploader.com/upload',
method: 'POST',
data: { foo: 'bar' },
concurrency: this.formData.concurrency
Expand Down
23 changes: 15 additions & 8 deletions src/ngx-uploader/classes/ngx-uploader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface UploadProgress {
percentage: number;
speed: number;
speedHuman: string;
startTime: number | null;
endTime: number | null;
};
}

Expand Down Expand Up @@ -92,7 +94,9 @@ export class NgUploaderService {
data: {
percentage: 0,
speed: null,
speedHuman: null
speedHuman: null,
startTime: null,
endTime: null
}
},
lastModifiedDate: file.lastModifiedDate
Expand Down Expand Up @@ -171,22 +175,22 @@ export class NgUploaderService {
const reader = new FileReader();
const xhr = new XMLHttpRequest();
let time: number = new Date().getTime();
let load = 0;
let speed = 0;

xhr.upload.addEventListener('progress', (e: ProgressEvent) => {
if (e.lengthComputable) {
const percentage = Math.round((e.loaded * 100) / e.total);
const diff = new Date().getTime() - time;
time += diff;
load = e.loaded - load;
const speed = parseInt((load / diff * 1000) as any, 10);
speed = Math.round(e.loaded / diff * 1000);

file.progress = {
status: UploadStatus.Uploading,
data: {
percentage: percentage,
speed: speed,
speedHuman: `${humanizeBytes(speed)}/s`
speedHuman: `${humanizeBytes(speed)}/s`,
startTime: file.progress.data.startTime || new Date().getTime(),
endTime: null
}
};

Expand All @@ -201,12 +205,15 @@ export class NgUploaderService {

xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
const speedAverage = Math.round(file.size / (new Date().getTime() - file.progress.data.startTime) * 1000);
file.progress = {
status: UploadStatus.Done,
data: {
percentage: 100,
speed: null,
speedHuman: null
speed: speedAverage,
speedHuman: `${humanizeBytes(speedAverage)}/s`,
startTime: file.progress.data.startTime,
endTime: new Date().getTime()
}
};

Expand Down
7 changes: 3 additions & 4 deletions src/styles/app.sass
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ html, body, .hero
width: 100%
font-family: 'Open Sans', sans-serif
-webkit-font-smoothing: antialiased
background: #00bf8f
background: linear-gradient(to bottom, #001510, #00bf8f)
background: $black

.drop-container
padding: 40px 0
Expand Down Expand Up @@ -64,7 +63,7 @@ html, body, .hero
width: 100%
height: 4px
border-radius: 10px
background: #414E63
background: #495057
margin: 30px 0
position: relative
border: none
Expand Down Expand Up @@ -102,7 +101,7 @@ html, body, .hero

.label
margin: 5px 10px 0 0
color: $black
color: $white
display: block

.input-field
Expand Down

0 comments on commit a805955

Please sign in to comment.