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 {ActivatedRoute} from "@angular/router";
|
---|
8 |
|
---|
9 | @Component({
|
---|
10 | selector: 'app-materials',
|
---|
11 | templateUrl: './materials.component.html',
|
---|
12 | styleUrls: ['./materials.component.css']
|
---|
13 | })
|
---|
14 | export class MaterialsComponent implements OnInit{
|
---|
15 |
|
---|
16 | activeUser: ActiveUserInterface | undefined;
|
---|
17 | courseId : number | undefined;
|
---|
18 | files: string[] | any = []
|
---|
19 | fileStatus = {status: '', requestType: '', percent: 0};
|
---|
20 |
|
---|
21 | constructor(private loginService: LoginService,
|
---|
22 | private materialService: MaterialsService,
|
---|
23 | private _router: ActivatedRoute) {
|
---|
24 | }
|
---|
25 | ngOnInit() {
|
---|
26 | this.activeUser = this.loginService.activeUser;
|
---|
27 | this.courseId = +this._router.snapshot.paramMap.get("id");
|
---|
28 | this.materialService.findAllMaterialsForCourse(this.courseId)
|
---|
29 | .subscribe(response => {
|
---|
30 | console.log("files", this.files)
|
---|
31 | this.files = response
|
---|
32 | })
|
---|
33 | }
|
---|
34 |
|
---|
35 | onDownloadFile(filename: string): void {
|
---|
36 | this.materialService.download(filename).subscribe(
|
---|
37 | event => {
|
---|
38 | console.log(event);
|
---|
39 | this.resportProgress(event);
|
---|
40 | },
|
---|
41 | (error: HttpErrorResponse) => {
|
---|
42 | console.log(error);
|
---|
43 | }
|
---|
44 | );
|
---|
45 | }
|
---|
46 |
|
---|
47 | private resportProgress(httpEvent: HttpEvent<string[] | Blob>): void {
|
---|
48 | switch (httpEvent.type) {
|
---|
49 | case HttpEventType.UploadProgress:
|
---|
50 | this.updateStatus(httpEvent.loaded, httpEvent.total!, 'Uploading... ');
|
---|
51 | break;
|
---|
52 | case HttpEventType.DownloadProgress:
|
---|
53 | this.updateStatus(httpEvent.loaded, httpEvent.total!, 'Downloading... ');
|
---|
54 | break;
|
---|
55 | case HttpEventType.ResponseHeader:
|
---|
56 | console.log('Header returned', httpEvent);
|
---|
57 | break;
|
---|
58 | case HttpEventType.Response:
|
---|
59 | if (httpEvent.body instanceof Array) {
|
---|
60 | this.fileStatus.status = 'done';
|
---|
61 | for (const filename of httpEvent.body) {
|
---|
62 | this.files.unshift(filename);
|
---|
63 | }
|
---|
64 | } else {
|
---|
65 | saveAs(new Blob([httpEvent.body!],
|
---|
66 | { type: `${httpEvent.headers.get('Content-Type')};charset=utf-8`}),
|
---|
67 | httpEvent.headers.get('File-Name'));
|
---|
68 | }
|
---|
69 | this.fileStatus.status = 'done';
|
---|
70 | break;
|
---|
71 | default:
|
---|
72 | console.log(httpEvent);
|
---|
73 | break;
|
---|
74 |
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | private updateStatus(loaded: number, total: number, requestType: string): void {
|
---|
79 | this.fileStatus.status = 'progress';
|
---|
80 | this.fileStatus.requestType = requestType;
|
---|
81 | this.fileStatus.percent = Math.round(100 * loaded / total);
|
---|
82 | }
|
---|
83 |
|
---|
84 | }
|
---|