Added upload speedtest

- Updated CORS to apply for all API endpoints
- Added database docker compose
This commit is contained in:
Max W
2024-02-14 23:14:04 +01:00
parent bfcce1ec3b
commit ab16fd1c5d
4 changed files with 93 additions and 1 deletions

View File

@ -2,6 +2,7 @@ import {Component, ElementRef, ViewChild} from '@angular/core';
import {NgIf} from "@angular/common";
import {FormatFileSizePipePipe} from "../format-file-size-pipe.pipe";
import {FormsModule} from "@angular/forms";
import axios from "axios";
@Component({
selector: 'app-upload',
@ -28,6 +29,10 @@ export class UploadComponent {
uploadProgress = 0;
uploadFinished = false;
constructor() {
this.uploadSpeedTest();
}
/**
* Manages the file input click or drag and drop
*/
@ -75,4 +80,55 @@ export class UploadComponent {
}
}
private uploadSpeedTest() {
const start = new Date().getTime(); // Start timer
// Create a 1MB payload for upload
const payload = new ArrayBuffer(1024 * 1024); // 1MB
const uint8View = new Uint8Array(payload);
for (let i = 0; i < uint8View.length; i++) {
uint8View[i] = 0;
}
axios({
method: 'post',
url: 'http://localhost/api/v1/upload-speed-test',
data: uint8View,
headers: {
'Content-Type': 'application/octet-stream',
'Access-Control-Allow-Origin': '*', // Allow CORS
},
onUploadProgress: function(progressEvent) {
// Optional: handle progress events for feedback
}
})
.then(response => {
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`);
})
.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;
}
const estimatedTimeInSeconds = fileSize / uploadSpeed;
return estimatedTimeInSeconds;
}
}