aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/standalone/videos/shared/video-fetcher.ts
blob: cf6d128316272d0d246883db3c816e39f1c28ab7 (plain) (blame)
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
import { HttpStatusCode, LiveVideo, VideoDetails, VideoToken } from '../../../../../shared/models'
import { logger } from '../../../root-helpers'
import { AuthHTTP } from './auth-http'

export class VideoFetcher {

  constructor (private readonly http: AuthHTTP) {

  }

  async loadVideo (videoId: string) {
    const videoPromise = this.loadVideoInfo(videoId)

    let videoResponse: Response
    let isResponseOk: boolean

    try {
      videoResponse = await videoPromise
      isResponseOk = videoResponse.status === HttpStatusCode.OK_200
    } catch (err) {
      logger.error(err)

      isResponseOk = false
    }

    if (!isResponseOk) {
      if (videoResponse?.status === HttpStatusCode.NOT_FOUND_404) {
        throw new Error('This video does not exist.')
      }

      throw new Error('We cannot fetch the video. Please try again later.')
    }

    const captionsPromise = this.loadVideoCaptions(videoId)

    return { captionsPromise, videoResponse }
  }

  loadLive (video: VideoDetails) {
    return this.http.fetch(this.getLiveUrl(video.uuid), { optionalAuth: true })
      .then(res => res.json() as Promise<LiveVideo>)
  }

  loadVideoToken (video: VideoDetails) {
    return this.http.fetch(this.getVideoTokenUrl(video.uuid), { optionalAuth: true, method: 'POST' })
      .then(res => res.json() as Promise<VideoToken>)
      .then(token => token.files.token)
  }

  getVideoViewsUrl (videoUUID: string) {
    return this.getVideoUrl(videoUUID) + '/views'
  }

  private loadVideoInfo (videoId: string): Promise<Response> {
    return this.http.fetch(this.getVideoUrl(videoId), { optionalAuth: true })
  }

  private loadVideoCaptions (videoId: string): Promise<Response> {
    return this.http.fetch(this.getVideoUrl(videoId) + '/captions', { optionalAuth: true })
  }

  private getVideoUrl (id: string) {
    return window.location.origin + '/api/v1/videos/' + id
  }

  private getLiveUrl (videoId: string) {
    return window.location.origin + '/api/v1/videos/live/' + videoId
  }

  private getVideoTokenUrl (id: string) {
    return this.getVideoUrl(id) + '/token'
  }
}