]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/standalone/videos/shared/video-fetcher.ts
cf6d128316272d0d246883db3c816e39f1c28ab7
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / shared / video-fetcher.ts
1 import { HttpStatusCode, LiveVideo, VideoDetails, VideoToken } from '../../../../../shared/models'
2 import { logger } from '../../../root-helpers'
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) {
21 logger.error(err)
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
39 loadLive (video: VideoDetails) {
40 return this.http.fetch(this.getLiveUrl(video.uuid), { optionalAuth: true })
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)
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 }
69
70 private getVideoTokenUrl (id: string) {
71 return this.getVideoUrl(id) + '/token'
72 }
73 }