PeerTube/shared/server-commands/videos/streaming-playlists-command.ts

115 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-10-10 04:31:01 -05:00
import { wait } from '@shared/core-utils'
2021-07-16 03:42:24 -05:00
import { HttpStatusCode } from '@shared/models'
2022-10-10 04:31:01 -05:00
import { unwrapBody, unwrapBodyOrDecodeToJSON, unwrapTextOrDecode } from '../requests'
2021-07-09 03:21:10 -05:00
import { AbstractCommand, OverrideCommandOptions } from '../shared'
export class StreamingPlaylistsCommand extends AbstractCommand {
2022-10-10 04:31:01 -05:00
async get (options: OverrideCommandOptions & {
2021-07-09 03:21:10 -05:00
url: string
videoFileToken?: string
reinjectVideoFileToken?: boolean
2022-10-10 04:31:01 -05:00
withRetry?: boolean // default false
currentRetry?: number
2023-04-21 08:00:01 -05:00
}): Promise<string> {
const { videoFileToken, reinjectVideoFileToken, withRetry, currentRetry = 1 } = options
2021-07-09 03:21:10 -05:00
2022-10-10 04:31:01 -05:00
try {
const result = await unwrapTextOrDecode(this.getRawRequest({
...options,
url: options.url,
query: {
videoFileToken,
reinjectVideoFileToken
},
2022-10-10 04:31:01 -05:00
implicitToken: false,
defaultExpectedStatus: HttpStatusCode.OK_200
}))
return result
} catch (err) {
if (!withRetry || currentRetry > 5) throw err
await wait(100)
return this.get({
...options,
withRetry,
currentRetry: currentRetry + 1
})
}
2021-07-09 03:21:10 -05:00
}
2023-05-10 02:28:42 -05:00
async getFragmentedSegment (options: OverrideCommandOptions & {
2021-07-09 03:21:10 -05:00
url: string
range?: string
2023-05-10 02:28:42 -05:00
withRetry?: boolean // default false
currentRetry?: number
2021-07-09 03:21:10 -05:00
}) {
2023-05-10 02:28:42 -05:00
const { withRetry, currentRetry = 1 } = options
try {
const result = await unwrapBody<Buffer>(this.getRawRequest({
...options,
url: options.url,
range: options.range,
implicitToken: false,
responseType: 'application/octet-stream',
defaultExpectedStatus: HttpStatusCode.OK_200
}))
return result
} catch (err) {
if (!withRetry || currentRetry > 5) throw err
await wait(100)
return this.getFragmentedSegment({
...options,
withRetry,
currentRetry: currentRetry + 1
})
}
2021-07-09 03:21:10 -05:00
}
2023-05-10 02:28:42 -05:00
async getSegmentSha256 (options: OverrideCommandOptions & {
2021-07-09 03:21:10 -05:00
url: string
2023-05-10 02:28:42 -05:00
withRetry?: boolean // default false
currentRetry?: number
2021-07-09 03:21:10 -05:00
}) {
2023-05-10 02:28:42 -05:00
const { withRetry, currentRetry = 1 } = options
try {
const result = await unwrapBodyOrDecodeToJSON<{ [ id: string ]: string }>(this.getRawRequest({
...options,
url: options.url,
contentType: 'application/json',
implicitToken: false,
defaultExpectedStatus: HttpStatusCode.OK_200
}))
return result
} catch (err) {
if (!withRetry || currentRetry > 5) throw err
await wait(100)
return this.getSegmentSha256({
...options,
withRetry,
currentRetry: currentRetry + 1
})
}
2021-07-09 03:21:10 -05:00
}
}