From 5888abe7334ad0df987d7f73b6398b7c60ad6d0e Mon Sep 17 00:00:00 2001 From: ValentinFutterer Date: Thu, 13 Jun 2024 13:10:40 +0200 Subject: [PATCH] Fix bugs in game upload --- .../game-upload/game-upload.component.ts | 41 +++++++++++-------- .../src/app/services/games.service.ts | 13 ++++-- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/frontend/indiestream/src/app/components/game-upload/game-upload.component.ts b/frontend/indiestream/src/app/components/game-upload/game-upload.component.ts index 969dcdd..4e0a7f4 100644 --- a/frontend/indiestream/src/app/components/game-upload/game-upload.component.ts +++ b/frontend/indiestream/src/app/components/game-upload/game-upload.component.ts @@ -3,11 +3,11 @@ import { MatButtonModule } from '@angular/material/button'; import { MatProgressBar } from "@angular/material/progress-bar"; import { MatIcon } from "@angular/material/icon"; import { HttpClientModule, HttpEventType } from "@angular/common/http"; -import {finalize, Subscription} from "rxjs"; -import {NgIf} from "@angular/common"; -import {GamesService} from "../../services/games.service"; -import {MatFormField, MatHint, MatInput, MatLabel, MatSuffix} from "@angular/material/input"; -import {FormBuilder, ReactiveFormsModule, Validators} from "@angular/forms"; +import { catchError, EMPTY, finalize, Subscription } from "rxjs"; +import { NgIf } from "@angular/common"; +import { GamesService } from "../../services/games.service"; +import { MatFormField, MatHint, MatInput, MatLabel, MatSuffix } from "@angular/material/input"; +import { FormBuilder, ReactiveFormsModule, Validators } from "@angular/forms"; @Component({ selector: 'app-game-upload', @@ -32,10 +32,10 @@ export class GameUploadComponent { uploadProgress: number = 0; uploadSub: Subscription = new Subscription(); - file_store: FileList = new DataTransfer().files; gameForm = this.fb.group({ title: ['', Validators.required], - filename: ['', Validators.required] + filename: ['', Validators.required], + file: [new DataTransfer().files, Validators.required] }); constructor(private gamesService: GamesService, private fb: FormBuilder) {} @@ -43,22 +43,28 @@ export class GameUploadComponent { onFileSelected(event: any) { const file:File = event.target.files[0]; if (file) { - this.file_store = event.target.files; this.gameForm.patchValue({ filename: file.name}); + this.gameForm.patchValue({ file: event.target.files}); } } onUpload() { - const upload$ = this.gamesService.uploadGame(this.file_store[0]) - .pipe( - finalize(() => this.reset()) - ); + if (this.gameForm.valid) { + const upload$ = this.gamesService.uploadGame(this.gameForm) + .pipe( + catchError((error) => { + console.error('An error occured during upload', error); + return EMPTY; + }), + finalize(() => this.reset()) + ); - this.uploadSub = upload$.subscribe(event => { - if (event.type == HttpEventType.UploadProgress && event.total !== undefined) { - this.uploadProgress = Math.round(100 * (event.loaded / event.total)); - } - }) + this.uploadSub = upload$.subscribe(event => { + if (event.type == HttpEventType.UploadProgress && event.total !== undefined) { + this.uploadProgress = Math.round(100 * (event.loaded / event.total)); + } + }) + } } reset() { @@ -66,6 +72,5 @@ export class GameUploadComponent { this.uploadSub.unsubscribe(); this.uploadSub = new Subscription(); this.gameForm.reset(); - this.file_store = new DataTransfer().files; } } diff --git a/frontend/indiestream/src/app/services/games.service.ts b/frontend/indiestream/src/app/services/games.service.ts index 799bd55..d343090 100644 --- a/frontend/indiestream/src/app/services/games.service.ts +++ b/frontend/indiestream/src/app/services/games.service.ts @@ -4,6 +4,7 @@ import { Games, Game } from '../modules/games'; import { Observable } from "rxjs"; import { AppConfigService } from "./app-config.service"; import { AuthService} from "./auth.service"; +import {FormGroup} from "@angular/forms"; @Injectable({ providedIn: 'root' @@ -27,12 +28,18 @@ export class GamesService { return this.http.get(this.apiUrl + "/games/" + id) } - deleteGame(id: string): void{ + deleteGame(id: string): void { this.http.delete(this.apiUrl + "/games/" + id) } - uploadGame(file: File): Observable>{ - return this.http.post(this.apiUrl + "/games", file, { + uploadGame(gameForm: FormGroup): Observable> { + const formData = new FormData(); + formData.append('title', gameForm.get('title')?.value); + const files: FileList = gameForm.get('file')?.value; + if (files && files.length > 0) { + formData.append('file', files[0]); + } + return this.http.post(this.apiUrl + "/games", formData, { reportProgress: true, observe: 'events' });