2022-10-12 16:09:02 +02:00
|
|
|
import { HttpStatusCode, LiveVideo, VideoDetails, VideoToken } from '../../../../../shared/models'
|
2022-07-15 15:30:14 +02:00
|
|
|
import { logger } from '../../../root-helpers'
|
2022-05-31 08:59:30 +02:00
|
|
|
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) {
|
2022-07-15 15:30:14 +02:00
|
|
|
logger.error(err)
|
2022-05-31 08:59:30 +02:00
|
|
|
|
|
|
|
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 }
|
|
|
|
}
|
|
|
|
|
2022-10-12 16:09:02 +02:00
|
|
|
loadLive (video: VideoDetails) {
|
2022-05-31 08:59:30 +02:00
|
|
|
return this.http.fetch(this.getLiveUrl(video.uuid), { optionalAuth: true })
|
2022-10-12 16:09:02 +02:00
|
|
|
.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)
|
2022-05-31 08:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-10-12 16:09:02 +02:00
|
|
|
|
|
|
|
private getVideoTokenUrl (id: string) {
|
|
|
|
return this.getVideoUrl(id) + '/token'
|
|
|
|
}
|
2022-05-31 08:59:30 +02:00
|
|
|
}
|