]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/server-commands/videos/streaming-playlists-command.ts
26ab2735fd33338ef19733e5c8bf6493506f35c9
[github/Chocobozzz/PeerTube.git] / shared / server-commands / videos / streaming-playlists-command.ts
1 import { wait } from '@shared/core-utils'
2 import { HttpStatusCode } from '@shared/models'
3 import { unwrapBody, unwrapBodyOrDecodeToJSON, unwrapTextOrDecode } from '../requests'
4 import { AbstractCommand, OverrideCommandOptions } from '../shared'
5
6 export class StreamingPlaylistsCommand extends AbstractCommand {
7
8 async get (options: OverrideCommandOptions & {
9 url: string
10
11 videoFileToken?: string
12 reinjectVideoFileToken?: boolean
13
14 withRetry?: boolean // default false
15 currentRetry?: number
16 }) {
17 const { videoFileToken, reinjectVideoFileToken, withRetry, currentRetry = 1 } = options
18
19 try {
20 const result = await unwrapTextOrDecode(this.getRawRequest({
21 ...options,
22
23 url: options.url,
24 query: {
25 videoFileToken,
26 reinjectVideoFileToken
27 },
28 implicitToken: false,
29 defaultExpectedStatus: HttpStatusCode.OK_200
30 }))
31
32 return result
33 } catch (err) {
34 if (!withRetry || currentRetry > 5) throw err
35
36 await wait(100)
37
38 return this.get({
39 ...options,
40
41 withRetry,
42 currentRetry: currentRetry + 1
43 })
44 }
45 }
46
47 getFragmentedSegment (options: OverrideCommandOptions & {
48 url: string
49 range?: string
50 }) {
51 return unwrapBody<Buffer>(this.getRawRequest({
52 ...options,
53
54 url: options.url,
55 range: options.range,
56 implicitToken: false,
57 defaultExpectedStatus: HttpStatusCode.OK_200
58 }))
59 }
60
61 getSegmentSha256 (options: OverrideCommandOptions & {
62 url: string
63 }) {
64 return unwrapBodyOrDecodeToJSON<{ [ id: string ]: string }>(this.getRawRequest({
65 ...options,
66
67 url: options.url,
68 implicitToken: false,
69 defaultExpectedStatus: HttpStatusCode.OK_200
70 }))
71 }
72 }