diff options
Diffstat (limited to 'shared')
-rw-r--r-- | shared/extra-utils/server/servers.ts | 5 | ||||
-rw-r--r-- | shared/extra-utils/shared/abstract-command.ts | 45 | ||||
-rw-r--r-- | shared/extra-utils/videos/index.ts | 3 | ||||
-rw-r--r-- | shared/extra-utils/videos/streaming-playlists-command.ts | 45 | ||||
-rw-r--r-- | shared/extra-utils/videos/streaming-playlists.ts | 76 | ||||
-rw-r--r-- | shared/extra-utils/videos/video-streaming-playlists.ts | 82 |
6 files changed, 165 insertions, 91 deletions
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 95c876110..6a1dadbcc 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts | |||
@@ -26,7 +26,8 @@ import { | |||
26 | ImportsCommand, | 26 | ImportsCommand, |
27 | LiveCommand, | 27 | LiveCommand, |
28 | PlaylistsCommand, | 28 | PlaylistsCommand, |
29 | ServicesCommand | 29 | ServicesCommand, |
30 | StreamingPlaylistsCommand | ||
30 | } from '../videos' | 31 | } from '../videos' |
31 | import { ConfigCommand } from './config-command' | 32 | import { ConfigCommand } from './config-command' |
32 | import { ContactFormCommand } from './contact-form-command' | 33 | import { ContactFormCommand } from './contact-form-command' |
@@ -117,6 +118,7 @@ interface ServerInfo { | |||
117 | playlistsCommand?: PlaylistsCommand | 118 | playlistsCommand?: PlaylistsCommand |
118 | historyCommand?: HistoryCommand | 119 | historyCommand?: HistoryCommand |
119 | importsCommand?: ImportsCommand | 120 | importsCommand?: ImportsCommand |
121 | streamingPlaylistsCommand?: StreamingPlaylistsCommand | ||
120 | } | 122 | } |
121 | 123 | ||
122 | function parallelTests () { | 124 | function parallelTests () { |
@@ -350,6 +352,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] | |||
350 | server.playlistsCommand = new PlaylistsCommand(server) | 352 | server.playlistsCommand = new PlaylistsCommand(server) |
351 | server.historyCommand = new HistoryCommand(server) | 353 | server.historyCommand = new HistoryCommand(server) |
352 | server.importsCommand = new ImportsCommand(server) | 354 | server.importsCommand = new ImportsCommand(server) |
355 | server.streamingPlaylistsCommand = new StreamingPlaylistsCommand(server) | ||
353 | 356 | ||
354 | res(server) | 357 | res(server) |
355 | }) | 358 | }) |
diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts index 1e07e9469..be368376f 100644 --- a/shared/extra-utils/shared/abstract-command.ts +++ b/shared/extra-utils/shared/abstract-command.ts | |||
@@ -16,6 +16,9 @@ export interface OverrideCommandOptions { | |||
16 | } | 16 | } |
17 | 17 | ||
18 | interface InternalCommonCommandOptions extends OverrideCommandOptions { | 18 | interface InternalCommonCommandOptions extends OverrideCommandOptions { |
19 | // Default to server.url | ||
20 | url?: string | ||
21 | |||
19 | path: string | 22 | path: string |
20 | // If we automatically send the server token if the token is not provided | 23 | // If we automatically send the server token if the token is not provided |
21 | implicitToken: boolean | 24 | implicitToken: boolean |
@@ -27,6 +30,7 @@ interface InternalGetCommandOptions extends InternalCommonCommandOptions { | |||
27 | contentType?: string | 30 | contentType?: string |
28 | accept?: string | 31 | accept?: string |
29 | redirects?: number | 32 | redirects?: number |
33 | range?: string | ||
30 | } | 34 | } |
31 | 35 | ||
32 | abstract class AbstractCommand { | 36 | abstract class AbstractCommand { |
@@ -55,6 +59,22 @@ abstract class AbstractCommand { | |||
55 | return unwrapText(this.getRequest(options)) | 59 | return unwrapText(this.getRequest(options)) |
56 | } | 60 | } |
57 | 61 | ||
62 | protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) { | ||
63 | const { url, range } = options | ||
64 | const { host, protocol, pathname } = new URL(url) | ||
65 | |||
66 | return this.getRequest({ | ||
67 | ...options, | ||
68 | |||
69 | token: this.buildCommonRequestToken(options), | ||
70 | defaultExpectedStatus: this.buildStatusCodeExpected(options), | ||
71 | |||
72 | url: `${protocol}//${host}`, | ||
73 | path: pathname, | ||
74 | range | ||
75 | }) | ||
76 | } | ||
77 | |||
58 | protected getRequest (options: InternalGetCommandOptions) { | 78 | protected getRequest (options: InternalGetCommandOptions) { |
59 | const { redirects, query, contentType, accept } = options | 79 | const { redirects, query, contentType, accept } = options |
60 | 80 | ||
@@ -127,20 +147,31 @@ abstract class AbstractCommand { | |||
127 | } | 147 | } |
128 | 148 | ||
129 | private buildCommonRequestOptions (options: InternalCommonCommandOptions) { | 149 | private buildCommonRequestOptions (options: InternalCommonCommandOptions) { |
130 | const { token, expectedStatus, defaultExpectedStatus, path } = options | 150 | const { path } = options |
151 | |||
152 | return { | ||
153 | url: this.server.url, | ||
154 | path, | ||
155 | |||
156 | token: this.buildCommonRequestToken(options), | ||
157 | statusCodeExpected: this.buildStatusCodeExpected(options) | ||
158 | } | ||
159 | } | ||
160 | |||
161 | private buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) { | ||
162 | const { token } = options | ||
131 | 163 | ||
132 | const fallbackToken = options.implicitToken | 164 | const fallbackToken = options.implicitToken |
133 | ? this.server.accessToken | 165 | ? this.server.accessToken |
134 | : undefined | 166 | : undefined |
135 | 167 | ||
136 | return { | 168 | return token !== undefined ? token : fallbackToken |
137 | url: this.server.url, | 169 | } |
138 | path, | ||
139 | 170 | ||
140 | token: token !== undefined ? token : fallbackToken, | 171 | private buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) { |
172 | const { expectedStatus, defaultExpectedStatus } = options | ||
141 | 173 | ||
142 | statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus | 174 | return expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus |
143 | } | ||
144 | } | 175 | } |
145 | } | 176 | } |
146 | 177 | ||
diff --git a/shared/extra-utils/videos/index.ts b/shared/extra-utils/videos/index.ts index 372cf7a90..f87ae8eea 100644 --- a/shared/extra-utils/videos/index.ts +++ b/shared/extra-utils/videos/index.ts | |||
@@ -9,7 +9,8 @@ export * from './live' | |||
9 | export * from './playlists-command' | 9 | export * from './playlists-command' |
10 | export * from './playlists' | 10 | export * from './playlists' |
11 | export * from './services-command' | 11 | export * from './services-command' |
12 | export * from './streaming-playlists-command' | ||
13 | export * from './streaming-playlists' | ||
12 | export * from './video-channels' | 14 | export * from './video-channels' |
13 | export * from './video-comments' | 15 | export * from './video-comments' |
14 | export * from './video-streaming-playlists' | ||
15 | export * from './videos' | 16 | export * from './videos' |
diff --git a/shared/extra-utils/videos/streaming-playlists-command.ts b/shared/extra-utils/videos/streaming-playlists-command.ts new file mode 100644 index 000000000..4caec7137 --- /dev/null +++ b/shared/extra-utils/videos/streaming-playlists-command.ts | |||
@@ -0,0 +1,45 @@ | |||
1 | |||
2 | import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' | ||
3 | import { unwrapBody, unwrapText } from '../requests' | ||
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
5 | |||
6 | export class StreamingPlaylistsCommand extends AbstractCommand { | ||
7 | |||
8 | get (options: OverrideCommandOptions & { | ||
9 | url: string | ||
10 | }) { | ||
11 | return unwrapText(this.getRawRequest({ | ||
12 | ...options, | ||
13 | |||
14 | url: options.url, | ||
15 | implicitToken: false, | ||
16 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
17 | })) | ||
18 | } | ||
19 | |||
20 | getSegment (options: OverrideCommandOptions & { | ||
21 | url: string | ||
22 | range?: string | ||
23 | }) { | ||
24 | return unwrapText(this.getRawRequest({ | ||
25 | ...options, | ||
26 | |||
27 | url: options.url, | ||
28 | range: options.range, | ||
29 | implicitToken: false, | ||
30 | defaultExpectedStatus: HttpStatusCode.OK_200, | ||
31 | })) | ||
32 | } | ||
33 | |||
34 | getSegmentSha256 (options: OverrideCommandOptions & { | ||
35 | url: string | ||
36 | }) { | ||
37 | return unwrapBody<{ [ id: string ]: string }>(this.getRawRequest({ | ||
38 | ...options, | ||
39 | |||
40 | url: options.url, | ||
41 | implicitToken: false, | ||
42 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
43 | })) | ||
44 | } | ||
45 | } | ||
diff --git a/shared/extra-utils/videos/streaming-playlists.ts b/shared/extra-utils/videos/streaming-playlists.ts new file mode 100644 index 000000000..0324c739a --- /dev/null +++ b/shared/extra-utils/videos/streaming-playlists.ts | |||
@@ -0,0 +1,76 @@ | |||
1 | import { expect } from 'chai' | ||
2 | import { sha256 } from '@server/helpers/core-utils' | ||
3 | import { HttpStatusCode } from '@shared/core-utils' | ||
4 | import { VideoStreamingPlaylist } from '@shared/models' | ||
5 | import { ServerInfo } from '../server' | ||
6 | |||
7 | async function checkSegmentHash (options: { | ||
8 | server: ServerInfo | ||
9 | baseUrlPlaylist: string | ||
10 | baseUrlSegment: string | ||
11 | videoUUID: string | ||
12 | resolution: number | ||
13 | hlsPlaylist: VideoStreamingPlaylist | ||
14 | }) { | ||
15 | const { server, baseUrlPlaylist, baseUrlSegment, videoUUID, resolution, hlsPlaylist } = options | ||
16 | const command = server.streamingPlaylistsCommand | ||
17 | |||
18 | const playlist = await command.get({ url: `${baseUrlPlaylist}/${videoUUID}/${resolution}.m3u8` }) | ||
19 | |||
20 | const videoName = `${videoUUID}-${resolution}-fragmented.mp4` | ||
21 | |||
22 | const matches = /#EXT-X-BYTERANGE:(\d+)@(\d+)/.exec(playlist) | ||
23 | |||
24 | const length = parseInt(matches[1], 10) | ||
25 | const offset = parseInt(matches[2], 10) | ||
26 | const range = `${offset}-${offset + length - 1}` | ||
27 | |||
28 | const segmentBody = await command.getSegment({ | ||
29 | url: `${baseUrlSegment}/${videoUUID}/${videoName}`, | ||
30 | expectedStatus: HttpStatusCode.PARTIAL_CONTENT_206, | ||
31 | range: `bytes=${range}` | ||
32 | }) | ||
33 | |||
34 | const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url }) | ||
35 | expect(sha256(segmentBody)).to.equal(shaBody[videoName][range]) | ||
36 | } | ||
37 | |||
38 | async function checkLiveSegmentHash (options: { | ||
39 | server: ServerInfo | ||
40 | baseUrlSegment: string | ||
41 | videoUUID: string | ||
42 | segmentName: string | ||
43 | hlsPlaylist: VideoStreamingPlaylist | ||
44 | }) { | ||
45 | const { server, baseUrlSegment, videoUUID, segmentName, hlsPlaylist } = options | ||
46 | const command = server.streamingPlaylistsCommand | ||
47 | |||
48 | const segmentBody = await command.getSegment({ url: `${baseUrlSegment}/${videoUUID}/${segmentName}` }) | ||
49 | const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url }) | ||
50 | |||
51 | expect(sha256(segmentBody)).to.equal(shaBody[segmentName]) | ||
52 | } | ||
53 | |||
54 | async function checkResolutionsInMasterPlaylist (options: { | ||
55 | server: ServerInfo | ||
56 | playlistUrl: string | ||
57 | resolutions: number[] | ||
58 | }) { | ||
59 | const { server, playlistUrl, resolutions } = options | ||
60 | |||
61 | const masterPlaylist = await server.streamingPlaylistsCommand.get({ url: playlistUrl }) | ||
62 | |||
63 | for (const resolution of resolutions) { | ||
64 | const reg = new RegExp( | ||
65 | '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"' | ||
66 | ) | ||
67 | |||
68 | expect(masterPlaylist).to.match(reg) | ||
69 | } | ||
70 | } | ||
71 | |||
72 | export { | ||
73 | checkSegmentHash, | ||
74 | checkLiveSegmentHash, | ||
75 | checkResolutionsInMasterPlaylist | ||
76 | } | ||
diff --git a/shared/extra-utils/videos/video-streaming-playlists.ts b/shared/extra-utils/videos/video-streaming-playlists.ts deleted file mode 100644 index 99c2e1880..000000000 --- a/shared/extra-utils/videos/video-streaming-playlists.ts +++ /dev/null | |||
@@ -1,82 +0,0 @@ | |||
1 | import { makeRawRequest } from '../requests/requests' | ||
2 | import { sha256 } from '../../../server/helpers/core-utils' | ||
3 | import { VideoStreamingPlaylist } from '../../models/videos/video-streaming-playlist.model' | ||
4 | import { expect } from 'chai' | ||
5 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
6 | |||
7 | function getPlaylist (url: string, statusCodeExpected = HttpStatusCode.OK_200) { | ||
8 | return makeRawRequest(url, statusCodeExpected) | ||
9 | } | ||
10 | |||
11 | function getSegment (url: string, statusCodeExpected = HttpStatusCode.OK_200, range?: string) { | ||
12 | return makeRawRequest(url, statusCodeExpected, range) | ||
13 | } | ||
14 | |||
15 | function getSegmentSha256 (url: string, statusCodeExpected = HttpStatusCode.OK_200) { | ||
16 | return makeRawRequest(url, statusCodeExpected) | ||
17 | } | ||
18 | |||
19 | async function checkSegmentHash ( | ||
20 | baseUrlPlaylist: string, | ||
21 | baseUrlSegment: string, | ||
22 | videoUUID: string, | ||
23 | resolution: number, | ||
24 | hlsPlaylist: VideoStreamingPlaylist | ||
25 | ) { | ||
26 | const res = await getPlaylist(`${baseUrlPlaylist}/${videoUUID}/${resolution}.m3u8`) | ||
27 | const playlist = res.text | ||
28 | |||
29 | const videoName = `${videoUUID}-${resolution}-fragmented.mp4` | ||
30 | |||
31 | const matches = /#EXT-X-BYTERANGE:(\d+)@(\d+)/.exec(playlist) | ||
32 | |||
33 | const length = parseInt(matches[1], 10) | ||
34 | const offset = parseInt(matches[2], 10) | ||
35 | const range = `${offset}-${offset + length - 1}` | ||
36 | |||
37 | const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${videoName}`, HttpStatusCode.PARTIAL_CONTENT_206, `bytes=${range}`) | ||
38 | |||
39 | const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url) | ||
40 | |||
41 | const sha256Server = resSha.body[videoName][range] | ||
42 | expect(sha256(res2.body)).to.equal(sha256Server) | ||
43 | } | ||
44 | |||
45 | async function checkLiveSegmentHash ( | ||
46 | baseUrlSegment: string, | ||
47 | videoUUID: string, | ||
48 | segmentName: string, | ||
49 | hlsPlaylist: VideoStreamingPlaylist | ||
50 | ) { | ||
51 | const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${segmentName}`) | ||
52 | |||
53 | const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url) | ||
54 | |||
55 | const sha256Server = resSha.body[segmentName] | ||
56 | expect(sha256(res2.body)).to.equal(sha256Server) | ||
57 | } | ||
58 | |||
59 | async function checkResolutionsInMasterPlaylist (playlistUrl: string, resolutions: number[]) { | ||
60 | const res = await getPlaylist(playlistUrl) | ||
61 | |||
62 | const masterPlaylist = res.text | ||
63 | |||
64 | for (const resolution of resolutions) { | ||
65 | const reg = new RegExp( | ||
66 | '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"' | ||
67 | ) | ||
68 | |||
69 | expect(masterPlaylist).to.match(reg) | ||
70 | } | ||
71 | } | ||
72 | |||
73 | // --------------------------------------------------------------------------- | ||
74 | |||
75 | export { | ||
76 | getPlaylist, | ||
77 | getSegment, | ||
78 | checkResolutionsInMasterPlaylist, | ||
79 | getSegmentSha256, | ||
80 | checkLiveSegmentHash, | ||
81 | checkSegmentHash | ||
82 | } | ||