import {Component, ElementRef, ViewChild} from '@angular/core'; import {DatePipe, DecimalPipe, NgForOf, NgIf} from "@angular/common"; import axios from "axios"; import {firstValueFrom} from "rxjs"; import {DevelopmentStore} from "../../store/DevelopmentStore"; import {AuthStore} from "../../store/authStore"; import {Router} from "@angular/router"; import {FormatFileSizePipePipe} from "../format-file-size-pipe.pipe"; import {DurationPipe} from "../duration.pipe"; import {RelativeTimePipe} from "../relative-time.pipe"; import {FormsModule} from "@angular/forms"; import {EdituserComponent} from "./edituser/edituser.component"; @Component({ selector: 'app-adminui', standalone: true, imports: [ DatePipe, DecimalPipe, NgForOf, FormatFileSizePipePipe, DurationPipe, RelativeTimePipe, FormsModule, EdituserComponent, NgIf ], templateUrl: './adminui.component.html', styleUrl: './adminui.component.scss' }) export class AdminuiComponent { @ViewChild('edit_user_modal') edit_user_modal: ElementRef | undefined; fileUploads: any[] = []; expiredFileUploads: any[] = []; totalFileSizeOnDisk: number = 0; totalFileDownloads = 0; statistics: any = ""; username: string = ""; constructor(private developmentStore: DevelopmentStore, private authStore: AuthStore, private router: Router) { this.init(); } async init() { this.username = await firstValueFrom(this.authStore.username$); await this.verifyToken(); setInterval(() => { this.verifyToken(); }, 5000); } async verifyToken() { if(await firstValueFrom(this.authStore.token$) === "") { console.log("No token present, redirecting to login..."); await this.router.navigate(['/login']); return; } await this.fetchFileUploads(); await this.fetchExpiredFileUploads(); await this.fetchStatistics(); await this.calculateStatistics(); } async calculateStatistics() { this.totalFileSizeOnDisk = 0; this.totalFileDownloads = 0; for(let fileUpload of this.fileUploads) { this.totalFileSizeOnDisk += fileUpload.fileSize; } for(let fileUpload of this.expiredFileUploads) { this.totalFileDownloads += fileUpload.downloadCount; } for(let fileUpload of this.fileUploads) { this.totalFileDownloads += fileUpload.downloadCount; } } openEditUserModal() { this.edit_user_modal?.nativeElement.showModal(); } logout() { this.authStore.setToken(""); this.authStore.setUsername(""); this.router.navigate(['/login']); } async fetchFileUploads() { try { const response = await axios({ method: 'get', url: this.developmentStore.getBaseUrl() + 'api/v1/secure/upload-history', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + await firstValueFrom(this.authStore.token$) } }); this.fileUploads = response.data; } catch (error) { console.error(error); } } async fetchExpiredFileUploads() { try { const response = await axios({ method: 'get', url: this.developmentStore.getBaseUrl() + 'api/v1/secure/expired-upload-history', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + await firstValueFrom(this.authStore.token$) } }); this.expiredFileUploads = response.data; } catch (error) { console.error(error); } } async fetchStatistics() { try { const response = await axios({ method: 'get', url: this.developmentStore.getBaseUrl() + 'api/v1/secure/statistics', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + await firstValueFrom(this.authStore.token$) } }); this.statistics = response.data; console.log(this.statistics) } catch (error) { console.error(error); } } async deleteAllFileUploads() { try { const response = await axios({ method: 'delete', url: this.developmentStore.getBaseUrl() + 'api/v1/secure/files', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + await firstValueFrom(this.authStore.token$) } }); console.log(this.statistics) } catch (error) { console.error(error); } } protected readonly confirm = confirm; }