-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathon-push.component.ts
56 lines (49 loc) · 1.58 KB
/
on-push.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
import { ChangeDetectionStrategy, Component, OnDestroy } from '@angular/core';
import { Uploader, UploadState, UploadxOptions, UploadxService } from 'ngx-uploadx';
import { Observable } from 'rxjs';
import { AuthService } from '../auth.service';
import { serverUrl } from '../config';
import { injectDigestHeader } from '../digest';
@Component({
selector: 'app-on-push',
templateUrl: './on-push.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class OnPushComponent implements OnDestroy {
state$: Observable<UploadState>;
uploads$: Observable<Uploader[]>;
options: UploadxOptions = {
endpoint: `${serverUrl}/files?uploadType=uploadx`,
chunkSize: 1024 * 1024 * 64,
prerequest: injectDigestHeader,
authorize: (req, token) => {
token && (req.headers['Authorization'] = `Token ${token}`);
return req;
}
};
constructor(
private uploadService: UploadxService,
private authService: AuthService
) {
this.uploads$ = this.uploadService.connect(this.options);
this.state$ = this.uploadService.events;
this.authService.getToken().subscribe(token => {
this.uploadService.control({ token });
});
}
ngOnDestroy(): void {
this.uploadService.disconnect();
}
cancelAll(): void {
this.uploadService.control({ action: 'cancel' });
}
pauseAll(): void {
this.uploadService.control({ action: 'pause' });
}
uploadAll(): void {
this.uploadService.control({ action: 'upload' });
}
updateAll(): void {
this.uploadService.control({ action: 'update', metadata: { updated: Date.now() } });
}
}