[8423429] | 1 | import {Component, OnInit} from '@angular/core';
|
---|
| 2 | import {ActiveUserInterface} from "../ActiveUserInterface";
|
---|
| 3 | import {LoginService} from "../login.service";
|
---|
| 4 | import {MaterialsService} from "../materials.service";
|
---|
| 5 | import {HttpErrorResponse, HttpEvent, HttpEventType} from "@angular/common/http";
|
---|
| 6 | import {saveAs} from 'file-saver';
|
---|
| 7 | import {Router} from "@angular/router";
|
---|
| 8 |
|
---|
| 9 | @Component({
|
---|
| 10 | selector: 'app-add-material',
|
---|
| 11 | templateUrl: './add-material.component.html',
|
---|
| 12 | styleUrls: ['./add-material.component.css']
|
---|
| 13 | })
|
---|
| 14 | export class AddMaterialComponent implements OnInit {
|
---|
| 15 | activeUser: ActiveUserInterface | undefined;
|
---|
| 16 | filenames: string[] = [];
|
---|
| 17 | fileStatus = {status: '', requestType: '', percent: 0};
|
---|
| 18 | courseId: number;
|
---|
| 19 |
|
---|
| 20 | constructor(private loginService: LoginService,
|
---|
| 21 | private materialService: MaterialsService,
|
---|
| 22 | private router: Router) {
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | ngOnInit(): void {
|
---|
| 26 | this.activeUser = this.loginService.activeUser;
|
---|
| 27 | if (!this.activeUser) {
|
---|
| 28 | this.router.navigate(['/'])
|
---|
| 29 | }
|
---|
| 30 | this.courseId = this.loginService.currentCourse;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | // define a function to upload files
|
---|
| 34 | onUploadFiles(e): void {
|
---|
| 35 | let files = e.target.files;
|
---|
| 36 | console.log("files", files)
|
---|
| 37 | const formData = new FormData();
|
---|
| 38 | for (const file of files) {
|
---|
| 39 | formData.append('files', file, file.name);
|
---|
| 40 | }
|
---|
| 41 | this.materialService.upload(formData, this.activeUser.username, this.loginService.currentCourse).subscribe(
|
---|
| 42 | event => {
|
---|
| 43 | console.log(event);
|
---|
| 44 | this.reportProgress(event);
|
---|
| 45 | },
|
---|
| 46 | (error: HttpErrorResponse) => {
|
---|
| 47 | console.log(error);
|
---|
| 48 | }
|
---|
| 49 | );
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | // define a function to download files
|
---|
| 53 | onDownloadFile(filename: string): void {
|
---|
| 54 | this.materialService.download(filename).subscribe(
|
---|
| 55 | event => {
|
---|
| 56 | console.log(event);
|
---|
| 57 | this.reportProgress(event);
|
---|
| 58 | },
|
---|
| 59 | (error: HttpErrorResponse) => {
|
---|
| 60 | console.log(error);
|
---|
| 61 | }
|
---|
| 62 | );
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | private reportProgress(httpEvent: HttpEvent<string[] | Blob>): void {
|
---|
| 66 | switch (httpEvent.type) {
|
---|
| 67 | case HttpEventType.UploadProgress:
|
---|
| 68 | this.updateStatus(httpEvent.loaded, httpEvent.total!, 'Uploading... ');
|
---|
| 69 | break;
|
---|
| 70 | case HttpEventType.DownloadProgress:
|
---|
| 71 | this.updateStatus(httpEvent.loaded, httpEvent.total!, 'Downloading... ');
|
---|
| 72 | break;
|
---|
| 73 | case HttpEventType.ResponseHeader:
|
---|
| 74 | console.log('Header returned', httpEvent);
|
---|
| 75 | break;
|
---|
| 76 | case HttpEventType.Response:
|
---|
| 77 | if (httpEvent.body instanceof Array) {
|
---|
| 78 | this.fileStatus.status = 'done';
|
---|
| 79 | for (const filename of httpEvent.body) {
|
---|
| 80 | this.filenames.unshift(filename);
|
---|
| 81 | }
|
---|
| 82 | } else {
|
---|
| 83 | saveAs(new Blob([httpEvent.body!],
|
---|
| 84 | {type: `${httpEvent.headers.get('Content-Type')};charset=utf-8`}),
|
---|
| 85 | httpEvent.headers.get('File-Name'));
|
---|
| 86 | }
|
---|
| 87 | this.fileStatus.status = 'done';
|
---|
| 88 | break;
|
---|
| 89 | default:
|
---|
| 90 | console.log(httpEvent);
|
---|
| 91 | break;
|
---|
| 92 |
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | private updateStatus(loaded: number, total: number, requestType: string): void {
|
---|
| 97 | this.fileStatus.status = 'progress';
|
---|
| 98 | this.fileStatus.requestType = requestType;
|
---|
| 99 | this.fileStatus.percent = Math.round(100 * loaded / total);
|
---|
| 100 | }
|
---|
| 101 | }
|
---|