Finished estimated upload time calculator

This commit is contained in:
Max W. 2024-02-15 11:01:10 +01:00
parent ab16fd1c5d
commit 7ae97b1aa3

View File

@ -29,6 +29,8 @@ export class UploadComponent {
uploadProgress = 0;
uploadFinished = false;
uploadSpeedBps: number = 0;
constructor() {
this.uploadSpeedTest();
}
@ -68,6 +70,8 @@ export class UploadComponent {
console.log(`Uploading file: ${this.fileToUpload.name}, Short storage: ${this.shortStorage}, Single download: ${this.singleDownload}, Password protected: ${this.passwordProtected}`);
this.uploadStarted = true;
this.calculateEstimatedUploadTime();
const interval = setInterval(() => {
if (this.uploadProgress < 100) {
@ -107,28 +111,20 @@ export class UploadComponent {
const end = new Date().getTime(); // End timer
const duration = (end - start) / 1000; // Convert ms to seconds
const bitsUploaded = payload.byteLength * 8;
const speedBps = bitsUploaded / duration;
const speedKbps = speedBps / 1024;
const speedMbps = speedKbps / 1024;
console.log(`Upload speed: ${speedMbps.toFixed(2)} Mbps`);
this.uploadSpeedBps = bitsUploaded / duration;
console.log("Speed test ping finished.");
})
.catch(error => {
console.error('Error during upload test:', error);
});
}
/**
* Calculates the estimated upload time.
* @param {number} fileSize - The size of the file in bytes.
* @param {number} uploadSpeed - The upload speed in bytes per second.
* @returns {number} The estimated upload time in seconds.
*/
private calculateEstimatedUploadTime(fileSize: number, uploadSpeed: number) {
if (uploadSpeed <= 0) {
console.error("Upload speed must be greater than 0.");
return NaN;
private calculateEstimatedUploadTime() : void {
const fileSize = this.fileToUpload?.size;
const uploadSpeed = this.uploadSpeedBps / 8; // Convert bits per second to bytes per second
if (fileSize && uploadSpeed) {
const estimatedTimeInSeconds = fileSize / uploadSpeed;
console.log(`Estimated upload time: ${estimatedTimeInSeconds} seconds`);
}
const estimatedTimeInSeconds = fileSize / uploadSpeed;
return estimatedTimeInSeconds;
}
}