-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.component.ts
90 lines (82 loc) · 3 KB
/
app.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { HttpClient, HttpEvent, HttpEventType, HttpUploadProgressEvent } from '@angular/common/http';
import { Component } from '@angular/core';
import { NgxFileDropEntry } from '@bugsplat/ngx-file-drop';
import { BehaviorSubject, bindCallback, filter, finalize, from, map, mergeMap, Observable, scan, switchMap, takeWhile } from 'rxjs';
import { FilesTableEntry } from './files/files-table-entry';
import { FileUploadProgress } from './uploads/file-upload-progress';
import { v4 as uuid } from 'uuid';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
files$?: Observable<FilesTableEntry[]>;
uploads$?: Observable<FileUploadProgress[]>;
private getFilesSubject = new BehaviorSubject<any>(null);
constructor(private httpClient: HttpClient) {
this.files$ = this.getFilesSubject.asObservable()
.pipe(
switchMap(() => this.httpClient.get<FilesTableEntry[]>('http://localhost:8080/files'))
);
}
onFilesDropped(files: NgxFileDropEntry[]): void {
this.uploads$ = from(files)
.pipe(
mergeMap(selectedFile => {
const id = uuid();
const fileEntry = selectedFile.fileEntry as FileSystemFileEntry;
const observableFactory = bindCallback(fileEntry.file) as any;
const file$ = observableFactory.call(fileEntry) as Observable<File>;
return file$
.pipe(
switchMap(file => this.uploadFile(file)
.pipe(
takeWhile(event => event.type !== HttpEventType.Response),
filter(isHttpProgressEvent),
map(event => {
const name = file.name;
const loaded = event.loaded ?? 0;
const total = event.total ?? 1;
const progress = Math.round(loaded / total * 100);
const failed = false;
const done = progress === 100;
return {
id,
name,
progress,
failed,
done
};
}),
)
)
);
}),
scan((acc, next) => {
acc[next.id] = next;
return {
...acc
};
}, {} as Record<string, FileUploadProgress>),
map(progress => Object.values(progress)),
finalize(() => this.getFilesSubject.next(null))
);
}
private uploadFile(file: File): Observable<HttpEvent<unknown>> {
const url = 'http://localhost:8080/upload';
const formData: FormData = new FormData();
formData.append('file', file, file.name);
return this.httpClient.post(
url,
formData,
{
reportProgress: true,
observe: 'events'
}
);
}
}
function isHttpProgressEvent(input: HttpEvent<unknown>): input is HttpUploadProgressEvent {
return input.type === HttpEventType.UploadProgress;
}