]>
Commit | Line | Data |
---|---|---|
3545e72c | 1 | import { HttpStatusCode, LiveVideo, VideoDetails, VideoToken } from '../../../../../shared/models' |
42b40636 | 2 | import { logger } from '../../../root-helpers' |
f1a0f3b7 C |
3 | import { AuthHTTP } from './auth-http' |
4 | ||
5 | export class VideoFetcher { | |
6 | ||
7 | constructor (private readonly http: AuthHTTP) { | |
8 | ||
9 | } | |
10 | ||
11 | async loadVideo (videoId: string) { | |
12 | const videoPromise = this.loadVideoInfo(videoId) | |
13 | ||
14 | let videoResponse: Response | |
15 | let isResponseOk: boolean | |
16 | ||
17 | try { | |
18 | videoResponse = await videoPromise | |
19 | isResponseOk = videoResponse.status === HttpStatusCode.OK_200 | |
20 | } catch (err) { | |
42b40636 | 21 | logger.error(err) |
f1a0f3b7 C |
22 | |
23 | isResponseOk = false | |
24 | } | |
25 | ||
26 | if (!isResponseOk) { | |
27 | if (videoResponse?.status === HttpStatusCode.NOT_FOUND_404) { | |
28 | throw new Error('This video does not exist.') | |
29 | } | |
30 | ||
31 | throw new Error('We cannot fetch the video. Please try again later.') | |
32 | } | |
33 | ||
34 | const captionsPromise = this.loadVideoCaptions(videoId) | |
35 | ||
36 | return { captionsPromise, videoResponse } | |
37 | } | |
38 | ||
3545e72c | 39 | loadLive (video: VideoDetails) { |
f1a0f3b7 | 40 | return this.http.fetch(this.getLiveUrl(video.uuid), { optionalAuth: true }) |
3545e72c C |
41 | .then(res => res.json() as Promise<LiveVideo>) |
42 | } | |
43 | ||
44 | loadVideoToken (video: VideoDetails) { | |
45 | return this.http.fetch(this.getVideoTokenUrl(video.uuid), { optionalAuth: true, method: 'POST' }) | |
46 | .then(res => res.json() as Promise<VideoToken>) | |
47 | .then(token => token.files.token) | |
f1a0f3b7 C |
48 | } |
49 | ||
50 | getVideoViewsUrl (videoUUID: string) { | |
51 | return this.getVideoUrl(videoUUID) + '/views' | |
52 | } | |
53 | ||
54 | private loadVideoInfo (videoId: string): Promise<Response> { | |
55 | return this.http.fetch(this.getVideoUrl(videoId), { optionalAuth: true }) | |
56 | } | |
57 | ||
58 | private loadVideoCaptions (videoId: string): Promise<Response> { | |
59 | return this.http.fetch(this.getVideoUrl(videoId) + '/captions', { optionalAuth: true }) | |
60 | } | |
61 | ||
62 | private getVideoUrl (id: string) { | |
63 | return window.location.origin + '/api/v1/videos/' + id | |
64 | } | |
65 | ||
66 | private getLiveUrl (videoId: string) { | |
67 | return window.location.origin + '/api/v1/videos/live/' + videoId | |
68 | } | |
3545e72c C |
69 | |
70 | private getVideoTokenUrl (id: string) { | |
71 | return this.getVideoUrl(id) + '/token' | |
72 | } | |
f1a0f3b7 | 73 | } |