1 import { catchError, map, of, tap } from 'rxjs'
2 import { HttpClient } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { RestExtractor } from '@app/core'
5 import { VideoToken } from '@shared/models'
6 import { VideoService } from './video.service'
9 export class VideoFileTokenService {
11 private readonly store = new Map<string, { token: string, expires: Date }>()
14 private authHttp: HttpClient,
15 private restExtractor: RestExtractor
18 getVideoFileToken (videoUUID: string) {
19 const existing = this.store.get(videoUUID)
20 if (existing) return of(existing)
22 return this.createVideoFileToken(videoUUID)
23 .pipe(tap(result => this.store.set(videoUUID, { token: result.token, expires: new Date(result.expires) })))
26 private createVideoFileToken (videoUUID: string) {
27 return this.authHttp.post<VideoToken>(`${VideoService.BASE_VIDEO_URL}/${videoUUID}/token`, {})
29 map(({ files }) => files),
30 catchError(err => this.restExtractor.handleError(err))