Added FileUpload logic
- Added smoother for progress indicator
This commit is contained in:
parent
1bce2e1d2e
commit
a4b6069789
@ -46,7 +46,7 @@
|
|||||||
<button class="btn btn-primary w-full max-w-xs" (click)="startUpload()">Upload File</button>
|
<button class="btn btn-primary w-full max-w-xs" (click)="startUpload()">Upload File</button>
|
||||||
</div>
|
</div>
|
||||||
<div *ngIf="uploadStarted && fileToUpload && !uploadFinished" class="flex flex-col items-center justify-center animate-in fade-in duration-500">
|
<div *ngIf="uploadStarted && fileToUpload && !uploadFinished" class="flex flex-col items-center justify-center animate-in fade-in duration-500">
|
||||||
<div class="radial-progress" [style.--value]="uploadProgress" style="--size:12rem; --thickness: 2rem;" role="progressbar">{{uploadProgress}}%</div>
|
<div class="radial-progress" [style.--value]="uploadProgress" style="--size:12rem; --thickness: 2rem;" role="progressbar">{{ uploadProgress | number:'1.0-0' }}%</div>
|
||||||
<p class="text-gray-700 mt-4">Uploading {{ fileToUpload.name }}...</p>
|
<p class="text-gray-700 mt-4">Uploading {{ fileToUpload.name }}...</p>
|
||||||
</div>
|
</div>
|
||||||
<div *ngIf="uploadFinished" class="flex flex-col items-center justify-center animate-in fade-in duration-1000">
|
<div *ngIf="uploadFinished" class="flex flex-col items-center justify-center animate-in fade-in duration-1000">
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import {Component, ElementRef, ViewChild} from '@angular/core';
|
import {Component, ElementRef, ViewChild} from '@angular/core';
|
||||||
import {NgIf} from "@angular/common";
|
import {DecimalPipe, NgIf} from "@angular/common";
|
||||||
import {FormatFileSizePipePipe} from "../format-file-size-pipe.pipe";
|
import {FormatFileSizePipePipe} from "../format-file-size-pipe.pipe";
|
||||||
import {FormsModule} from "@angular/forms";
|
import {FormsModule} from "@angular/forms";
|
||||||
import axios from "axios";
|
import axios, {AxiosProgressEvent} from "axios";
|
||||||
|
import { DevelopmentStore } from '../../store/DevelopmentStore';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-upload',
|
selector: 'app-upload',
|
||||||
@ -10,7 +11,8 @@ import axios from "axios";
|
|||||||
imports: [
|
imports: [
|
||||||
NgIf,
|
NgIf,
|
||||||
FormatFileSizePipePipe,
|
FormatFileSizePipePipe,
|
||||||
FormsModule
|
FormsModule,
|
||||||
|
DecimalPipe
|
||||||
],
|
],
|
||||||
templateUrl: './upload.component.html',
|
templateUrl: './upload.component.html',
|
||||||
styleUrl: './upload.component.scss'
|
styleUrl: './upload.component.scss'
|
||||||
@ -22,16 +24,21 @@ export class UploadComponent {
|
|||||||
|
|
||||||
shortStorage: boolean = false;
|
shortStorage: boolean = false;
|
||||||
singleDownload: boolean = false;
|
singleDownload: boolean = false;
|
||||||
|
fileDescription: string = '';
|
||||||
passwordProtected: boolean = false;
|
passwordProtected: boolean = false;
|
||||||
password: string = '';
|
password: string = 'nigga123';
|
||||||
|
|
||||||
uploadStarted: boolean = false;
|
uploadStarted: boolean = false;
|
||||||
uploadProgress = 0;
|
uploadProgress = 0; // Real progress
|
||||||
|
targetUploadProgress = 0; // New target progress to reach
|
||||||
|
|
||||||
uploadFinished = false;
|
uploadFinished = false;
|
||||||
|
|
||||||
uploadSpeedBps: number = 0;
|
uploadSpeedBps: number = 0;
|
||||||
|
|
||||||
constructor() {
|
|
||||||
|
|
||||||
|
constructor(private developmentStore: DevelopmentStore) {
|
||||||
this.uploadSpeedTest();
|
this.uploadSpeedTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,16 +70,37 @@ export class UploadComponent {
|
|||||||
const file = files[i];
|
const file = files[i];
|
||||||
this.fileToUpload = file;
|
this.fileToUpload = file;
|
||||||
}
|
}
|
||||||
|
this.calculateEstimatedUploadTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
startUpload(): void {
|
startUpload(): void {
|
||||||
if (this.fileToUpload) {
|
if (this.fileToUpload) {
|
||||||
console.log(`Uploading file: ${this.fileToUpload.name}, Short storage: ${this.shortStorage}, Single download: ${this.singleDownload}, Password protected: ${this.passwordProtected}`);
|
console.log(`Uploading file: ${this.fileToUpload.name}, Short storage: ${this.shortStorage}, Single download: ${this.singleDownload}, Password protected: ${this.passwordProtected}`);
|
||||||
this.uploadStarted = true;
|
this.uploadStarted = true;
|
||||||
|
const formData = this.buildFormDataObject();
|
||||||
|
|
||||||
this.calculateEstimatedUploadTime();
|
const config = {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'multipart/form-data',
|
||||||
|
'Access-Control-Allow-Origin': '*', // Allow CORS
|
||||||
|
},
|
||||||
|
onUploadProgress: (progressEvent: AxiosProgressEvent) => {
|
||||||
|
// @ts-ignore
|
||||||
|
this.targetUploadProgress = Math.round((progressEvent.loaded / progressEvent.total) * 100);
|
||||||
|
this.smoothProgressUpdate();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
axios.post(this.developmentStore.getBaseUrl() + 'api/v1/upload', formData, config)
|
||||||
|
.then(response => {
|
||||||
|
console.log('Upload completed successfully:', response.data);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error('Upload failed:', error.response ? error.response.data : error.message);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
/*// Old simulation of upload progress
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
if (this.uploadProgress < 100) {
|
if (this.uploadProgress < 100) {
|
||||||
this.uploadProgress++;
|
this.uploadProgress++;
|
||||||
@ -80,11 +108,21 @@ export class UploadComponent {
|
|||||||
clearInterval(interval); // Stop the interval when progress reaches 100%
|
clearInterval(interval); // Stop the interval when progress reaches 100%
|
||||||
this.uploadFinished = true;
|
this.uploadFinished = true;
|
||||||
}
|
}
|
||||||
}, 10);
|
}, 10);*/
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buildFormDataObject(): FormData {
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', this.fileToUpload as Blob);
|
||||||
|
formData.append('password', this.passwordProtected ? this.password : '');
|
||||||
|
formData.append('singleDownload', this.singleDownload.toString());
|
||||||
|
formData.append('shortStorage', this.shortStorage.toString());
|
||||||
|
formData.append('fileDescription', this.fileDescription);
|
||||||
|
return formData;
|
||||||
|
}
|
||||||
|
|
||||||
private uploadSpeedTest() {
|
private uploadSpeedTest() {
|
||||||
const start = new Date().getTime(); // Start timer
|
const start = new Date().getTime(); // Start timer
|
||||||
|
|
||||||
@ -127,4 +165,14 @@ export class UploadComponent {
|
|||||||
console.log(`Estimated upload time: ${estimatedTimeInSeconds} seconds`);
|
console.log(`Estimated upload time: ${estimatedTimeInSeconds} seconds`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
smoothProgressUpdate() {
|
||||||
|
if (this.uploadProgress < this.targetUploadProgress) {
|
||||||
|
this.uploadProgress += 0.01 * (this.targetUploadProgress - this.uploadProgress);
|
||||||
|
requestAnimationFrame(this.smoothProgressUpdate.bind(this));
|
||||||
|
} else if (this.uploadProgress > this.targetUploadProgress) {
|
||||||
|
// Handle overshoot
|
||||||
|
this.uploadProgress = this.targetUploadProgress;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import { Injectable } from '@angular/core';
|
|||||||
* This store automatically identifies if the env is production or development.
|
* This store automatically identifies if the env is production or development.
|
||||||
* If this app is running on development mode, it will add port 80 to the base url.
|
* If this app is running on development mode, it will add port 80 to the base url.
|
||||||
*/
|
*/
|
||||||
export class EnvironmentService {
|
export class DevelopmentStore {
|
||||||
private isDevelopment: boolean;
|
private isDevelopment: boolean;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user