diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /shared/server-commands/videos | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'shared/server-commands/videos')
22 files changed, 0 insertions, 2812 deletions
diff --git a/shared/server-commands/videos/blacklist-command.ts b/shared/server-commands/videos/blacklist-command.ts deleted file mode 100644 index 47e23ebc8..000000000 --- a/shared/server-commands/videos/blacklist-command.ts +++ /dev/null | |||
@@ -1,75 +0,0 @@ | |||
1 | |||
2 | import { HttpStatusCode, ResultList, VideoBlacklist, VideoBlacklistType } from '@shared/models' | ||
3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
4 | |||
5 | export class BlacklistCommand extends AbstractCommand { | ||
6 | |||
7 | add (options: OverrideCommandOptions & { | ||
8 | videoId: number | string | ||
9 | reason?: string | ||
10 | unfederate?: boolean | ||
11 | }) { | ||
12 | const { videoId, reason, unfederate } = options | ||
13 | const path = '/api/v1/videos/' + videoId + '/blacklist' | ||
14 | |||
15 | return this.postBodyRequest({ | ||
16 | ...options, | ||
17 | |||
18 | path, | ||
19 | fields: { reason, unfederate }, | ||
20 | implicitToken: true, | ||
21 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
22 | }) | ||
23 | } | ||
24 | |||
25 | update (options: OverrideCommandOptions & { | ||
26 | videoId: number | string | ||
27 | reason?: string | ||
28 | }) { | ||
29 | const { videoId, reason } = options | ||
30 | const path = '/api/v1/videos/' + videoId + '/blacklist' | ||
31 | |||
32 | return this.putBodyRequest({ | ||
33 | ...options, | ||
34 | |||
35 | path, | ||
36 | fields: { reason }, | ||
37 | implicitToken: true, | ||
38 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
39 | }) | ||
40 | } | ||
41 | |||
42 | remove (options: OverrideCommandOptions & { | ||
43 | videoId: number | string | ||
44 | }) { | ||
45 | const { videoId } = options | ||
46 | const path = '/api/v1/videos/' + videoId + '/blacklist' | ||
47 | |||
48 | return this.deleteRequest({ | ||
49 | ...options, | ||
50 | |||
51 | path, | ||
52 | implicitToken: true, | ||
53 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
54 | }) | ||
55 | } | ||
56 | |||
57 | list (options: OverrideCommandOptions & { | ||
58 | sort?: string | ||
59 | type?: VideoBlacklistType | ||
60 | } = {}) { | ||
61 | const { sort, type } = options | ||
62 | const path = '/api/v1/videos/blacklist/' | ||
63 | |||
64 | const query = { sort, type } | ||
65 | |||
66 | return this.getRequestBody<ResultList<VideoBlacklist>>({ | ||
67 | ...options, | ||
68 | |||
69 | path, | ||
70 | query, | ||
71 | implicitToken: true, | ||
72 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
73 | }) | ||
74 | } | ||
75 | } | ||
diff --git a/shared/server-commands/videos/captions-command.ts b/shared/server-commands/videos/captions-command.ts deleted file mode 100644 index a26fcb57d..000000000 --- a/shared/server-commands/videos/captions-command.ts +++ /dev/null | |||
@@ -1,67 +0,0 @@ | |||
1 | import { buildAbsoluteFixturePath } from '@shared/core-utils' | ||
2 | import { HttpStatusCode, ResultList, VideoCaption } from '@shared/models' | ||
3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
4 | |||
5 | export class CaptionsCommand extends AbstractCommand { | ||
6 | |||
7 | add (options: OverrideCommandOptions & { | ||
8 | videoId: string | number | ||
9 | language: string | ||
10 | fixture: string | ||
11 | mimeType?: string | ||
12 | }) { | ||
13 | const { videoId, language, fixture, mimeType } = options | ||
14 | |||
15 | const path = '/api/v1/videos/' + videoId + '/captions/' + language | ||
16 | |||
17 | const captionfile = buildAbsoluteFixturePath(fixture) | ||
18 | const captionfileAttach = mimeType | ||
19 | ? [ captionfile, { contentType: mimeType } ] | ||
20 | : captionfile | ||
21 | |||
22 | return this.putUploadRequest({ | ||
23 | ...options, | ||
24 | |||
25 | path, | ||
26 | fields: {}, | ||
27 | attaches: { | ||
28 | captionfile: captionfileAttach | ||
29 | }, | ||
30 | implicitToken: true, | ||
31 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
32 | }) | ||
33 | } | ||
34 | |||
35 | list (options: OverrideCommandOptions & { | ||
36 | videoId: string | number | ||
37 | videoPassword?: string | ||
38 | }) { | ||
39 | const { videoId, videoPassword } = options | ||
40 | const path = '/api/v1/videos/' + videoId + '/captions' | ||
41 | |||
42 | return this.getRequestBody<ResultList<VideoCaption>>({ | ||
43 | ...options, | ||
44 | |||
45 | path, | ||
46 | headers: this.buildVideoPasswordHeader(videoPassword), | ||
47 | implicitToken: false, | ||
48 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
49 | }) | ||
50 | } | ||
51 | |||
52 | delete (options: OverrideCommandOptions & { | ||
53 | videoId: string | number | ||
54 | language: string | ||
55 | }) { | ||
56 | const { videoId, language } = options | ||
57 | const path = '/api/v1/videos/' + videoId + '/captions/' + language | ||
58 | |||
59 | return this.deleteRequest({ | ||
60 | ...options, | ||
61 | |||
62 | path, | ||
63 | implicitToken: true, | ||
64 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
65 | }) | ||
66 | } | ||
67 | } | ||
diff --git a/shared/server-commands/videos/change-ownership-command.ts b/shared/server-commands/videos/change-ownership-command.ts deleted file mode 100644 index ad4c726ef..000000000 --- a/shared/server-commands/videos/change-ownership-command.ts +++ /dev/null | |||
@@ -1,68 +0,0 @@ | |||
1 | |||
2 | import { HttpStatusCode, ResultList, VideoChangeOwnership } from '@shared/models' | ||
3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
4 | |||
5 | export class ChangeOwnershipCommand extends AbstractCommand { | ||
6 | |||
7 | create (options: OverrideCommandOptions & { | ||
8 | videoId: number | string | ||
9 | username: string | ||
10 | }) { | ||
11 | const { videoId, username } = options | ||
12 | const path = '/api/v1/videos/' + videoId + '/give-ownership' | ||
13 | |||
14 | return this.postBodyRequest({ | ||
15 | ...options, | ||
16 | |||
17 | path, | ||
18 | fields: { username }, | ||
19 | implicitToken: true, | ||
20 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
21 | }) | ||
22 | } | ||
23 | |||
24 | list (options: OverrideCommandOptions = {}) { | ||
25 | const path = '/api/v1/videos/ownership' | ||
26 | |||
27 | return this.getRequestBody<ResultList<VideoChangeOwnership>>({ | ||
28 | ...options, | ||
29 | |||
30 | path, | ||
31 | query: { sort: '-createdAt' }, | ||
32 | implicitToken: true, | ||
33 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
34 | }) | ||
35 | } | ||
36 | |||
37 | accept (options: OverrideCommandOptions & { | ||
38 | ownershipId: number | ||
39 | channelId: number | ||
40 | }) { | ||
41 | const { ownershipId, channelId } = options | ||
42 | const path = '/api/v1/videos/ownership/' + ownershipId + '/accept' | ||
43 | |||
44 | return this.postBodyRequest({ | ||
45 | ...options, | ||
46 | |||
47 | path, | ||
48 | fields: { channelId }, | ||
49 | implicitToken: true, | ||
50 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
51 | }) | ||
52 | } | ||
53 | |||
54 | refuse (options: OverrideCommandOptions & { | ||
55 | ownershipId: number | ||
56 | }) { | ||
57 | const { ownershipId } = options | ||
58 | const path = '/api/v1/videos/ownership/' + ownershipId + '/refuse' | ||
59 | |||
60 | return this.postBodyRequest({ | ||
61 | ...options, | ||
62 | |||
63 | path, | ||
64 | implicitToken: true, | ||
65 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
66 | }) | ||
67 | } | ||
68 | } | ||
diff --git a/shared/server-commands/videos/channel-syncs-command.ts b/shared/server-commands/videos/channel-syncs-command.ts deleted file mode 100644 index de4a160ec..000000000 --- a/shared/server-commands/videos/channel-syncs-command.ts +++ /dev/null | |||
@@ -1,55 +0,0 @@ | |||
1 | import { HttpStatusCode, ResultList, VideoChannelSync, VideoChannelSyncCreate } from '@shared/models' | ||
2 | import { pick } from '@shared/core-utils' | ||
3 | import { unwrapBody } from '../requests' | ||
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
5 | |||
6 | export class ChannelSyncsCommand extends AbstractCommand { | ||
7 | private static readonly API_PATH = '/api/v1/video-channel-syncs' | ||
8 | |||
9 | listByAccount (options: OverrideCommandOptions & { | ||
10 | accountName: string | ||
11 | start?: number | ||
12 | count?: number | ||
13 | sort?: string | ||
14 | }) { | ||
15 | const { accountName, sort = 'createdAt' } = options | ||
16 | |||
17 | const path = `/api/v1/accounts/${accountName}/video-channel-syncs` | ||
18 | |||
19 | return this.getRequestBody<ResultList<VideoChannelSync>>({ | ||
20 | ...options, | ||
21 | |||
22 | path, | ||
23 | query: { sort, ...pick(options, [ 'start', 'count' ]) }, | ||
24 | implicitToken: true, | ||
25 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
26 | }) | ||
27 | } | ||
28 | |||
29 | async create (options: OverrideCommandOptions & { | ||
30 | attributes: VideoChannelSyncCreate | ||
31 | }) { | ||
32 | return unwrapBody<{ videoChannelSync: VideoChannelSync }>(this.postBodyRequest({ | ||
33 | ...options, | ||
34 | |||
35 | path: ChannelSyncsCommand.API_PATH, | ||
36 | fields: options.attributes, | ||
37 | implicitToken: true, | ||
38 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
39 | })) | ||
40 | } | ||
41 | |||
42 | delete (options: OverrideCommandOptions & { | ||
43 | channelSyncId: number | ||
44 | }) { | ||
45 | const path = `${ChannelSyncsCommand.API_PATH}/${options.channelSyncId}` | ||
46 | |||
47 | return this.deleteRequest({ | ||
48 | ...options, | ||
49 | |||
50 | path, | ||
51 | implicitToken: true, | ||
52 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
53 | }) | ||
54 | } | ||
55 | } | ||
diff --git a/shared/server-commands/videos/channels-command.ts b/shared/server-commands/videos/channels-command.ts deleted file mode 100644 index 385d0fe73..000000000 --- a/shared/server-commands/videos/channels-command.ts +++ /dev/null | |||
@@ -1,202 +0,0 @@ | |||
1 | import { pick } from '@shared/core-utils' | ||
2 | import { | ||
3 | ActorFollow, | ||
4 | HttpStatusCode, | ||
5 | ResultList, | ||
6 | VideoChannel, | ||
7 | VideoChannelCreate, | ||
8 | VideoChannelCreateResult, | ||
9 | VideoChannelUpdate, | ||
10 | VideosImportInChannelCreate | ||
11 | } from '@shared/models' | ||
12 | import { unwrapBody } from '../requests' | ||
13 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
14 | |||
15 | export class ChannelsCommand extends AbstractCommand { | ||
16 | |||
17 | list (options: OverrideCommandOptions & { | ||
18 | start?: number | ||
19 | count?: number | ||
20 | sort?: string | ||
21 | withStats?: boolean | ||
22 | } = {}) { | ||
23 | const path = '/api/v1/video-channels' | ||
24 | |||
25 | return this.getRequestBody<ResultList<VideoChannel>>({ | ||
26 | ...options, | ||
27 | |||
28 | path, | ||
29 | query: pick(options, [ 'start', 'count', 'sort', 'withStats' ]), | ||
30 | implicitToken: false, | ||
31 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
32 | }) | ||
33 | } | ||
34 | |||
35 | listByAccount (options: OverrideCommandOptions & { | ||
36 | accountName: string | ||
37 | start?: number | ||
38 | count?: number | ||
39 | sort?: string | ||
40 | withStats?: boolean | ||
41 | search?: string | ||
42 | }) { | ||
43 | const { accountName, sort = 'createdAt' } = options | ||
44 | const path = '/api/v1/accounts/' + accountName + '/video-channels' | ||
45 | |||
46 | return this.getRequestBody<ResultList<VideoChannel>>({ | ||
47 | ...options, | ||
48 | |||
49 | path, | ||
50 | query: { sort, ...pick(options, [ 'start', 'count', 'withStats', 'search' ]) }, | ||
51 | implicitToken: false, | ||
52 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
53 | }) | ||
54 | } | ||
55 | |||
56 | async create (options: OverrideCommandOptions & { | ||
57 | attributes: Partial<VideoChannelCreate> | ||
58 | }) { | ||
59 | const path = '/api/v1/video-channels/' | ||
60 | |||
61 | // Default attributes | ||
62 | const defaultAttributes = { | ||
63 | displayName: 'my super video channel', | ||
64 | description: 'my super channel description', | ||
65 | support: 'my super channel support' | ||
66 | } | ||
67 | const attributes = { ...defaultAttributes, ...options.attributes } | ||
68 | |||
69 | const body = await unwrapBody<{ videoChannel: VideoChannelCreateResult }>(this.postBodyRequest({ | ||
70 | ...options, | ||
71 | |||
72 | path, | ||
73 | fields: attributes, | ||
74 | implicitToken: true, | ||
75 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
76 | })) | ||
77 | |||
78 | return body.videoChannel | ||
79 | } | ||
80 | |||
81 | update (options: OverrideCommandOptions & { | ||
82 | channelName: string | ||
83 | attributes: VideoChannelUpdate | ||
84 | }) { | ||
85 | const { channelName, attributes } = options | ||
86 | const path = '/api/v1/video-channels/' + channelName | ||
87 | |||
88 | return this.putBodyRequest({ | ||
89 | ...options, | ||
90 | |||
91 | path, | ||
92 | fields: attributes, | ||
93 | implicitToken: true, | ||
94 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
95 | }) | ||
96 | } | ||
97 | |||
98 | delete (options: OverrideCommandOptions & { | ||
99 | channelName: string | ||
100 | }) { | ||
101 | const path = '/api/v1/video-channels/' + options.channelName | ||
102 | |||
103 | return this.deleteRequest({ | ||
104 | ...options, | ||
105 | |||
106 | path, | ||
107 | implicitToken: true, | ||
108 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
109 | }) | ||
110 | } | ||
111 | |||
112 | get (options: OverrideCommandOptions & { | ||
113 | channelName: string | ||
114 | }) { | ||
115 | const path = '/api/v1/video-channels/' + options.channelName | ||
116 | |||
117 | return this.getRequestBody<VideoChannel>({ | ||
118 | ...options, | ||
119 | |||
120 | path, | ||
121 | implicitToken: false, | ||
122 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
123 | }) | ||
124 | } | ||
125 | |||
126 | updateImage (options: OverrideCommandOptions & { | ||
127 | fixture: string | ||
128 | channelName: string | number | ||
129 | type: 'avatar' | 'banner' | ||
130 | }) { | ||
131 | const { channelName, fixture, type } = options | ||
132 | |||
133 | const path = `/api/v1/video-channels/${channelName}/${type}/pick` | ||
134 | |||
135 | return this.updateImageRequest({ | ||
136 | ...options, | ||
137 | |||
138 | path, | ||
139 | fixture, | ||
140 | fieldname: type + 'file', | ||
141 | |||
142 | implicitToken: true, | ||
143 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
144 | }) | ||
145 | } | ||
146 | |||
147 | deleteImage (options: OverrideCommandOptions & { | ||
148 | channelName: string | number | ||
149 | type: 'avatar' | 'banner' | ||
150 | }) { | ||
151 | const { channelName, type } = options | ||
152 | |||
153 | const path = `/api/v1/video-channels/${channelName}/${type}` | ||
154 | |||
155 | return this.deleteRequest({ | ||
156 | ...options, | ||
157 | |||
158 | path, | ||
159 | implicitToken: true, | ||
160 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
161 | }) | ||
162 | } | ||
163 | |||
164 | listFollowers (options: OverrideCommandOptions & { | ||
165 | channelName: string | ||
166 | start?: number | ||
167 | count?: number | ||
168 | sort?: string | ||
169 | search?: string | ||
170 | }) { | ||
171 | const { channelName, start, count, sort, search } = options | ||
172 | const path = '/api/v1/video-channels/' + channelName + '/followers' | ||
173 | |||
174 | const query = { start, count, sort, search } | ||
175 | |||
176 | return this.getRequestBody<ResultList<ActorFollow>>({ | ||
177 | ...options, | ||
178 | |||
179 | path, | ||
180 | query, | ||
181 | implicitToken: true, | ||
182 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
183 | }) | ||
184 | } | ||
185 | |||
186 | importVideos (options: OverrideCommandOptions & VideosImportInChannelCreate & { | ||
187 | channelName: string | ||
188 | }) { | ||
189 | const { channelName, externalChannelUrl, videoChannelSyncId } = options | ||
190 | |||
191 | const path = `/api/v1/video-channels/${channelName}/import-videos` | ||
192 | |||
193 | return this.postBodyRequest({ | ||
194 | ...options, | ||
195 | |||
196 | path, | ||
197 | fields: { externalChannelUrl, videoChannelSyncId }, | ||
198 | implicitToken: true, | ||
199 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
200 | }) | ||
201 | } | ||
202 | } | ||
diff --git a/shared/server-commands/videos/channels.ts b/shared/server-commands/videos/channels.ts deleted file mode 100644 index 3c0d4b723..000000000 --- a/shared/server-commands/videos/channels.ts +++ /dev/null | |||
@@ -1,29 +0,0 @@ | |||
1 | import { PeerTubeServer } from '../server/server' | ||
2 | |||
3 | function setDefaultVideoChannel (servers: PeerTubeServer[]) { | ||
4 | const tasks: Promise<any>[] = [] | ||
5 | |||
6 | for (const server of servers) { | ||
7 | const p = server.users.getMyInfo() | ||
8 | .then(user => { server.store.channel = user.videoChannels[0] }) | ||
9 | |||
10 | tasks.push(p) | ||
11 | } | ||
12 | |||
13 | return Promise.all(tasks) | ||
14 | } | ||
15 | |||
16 | async function setDefaultChannelAvatar (serversArg: PeerTubeServer | PeerTubeServer[], channelName: string = 'root_channel') { | ||
17 | const servers = Array.isArray(serversArg) | ||
18 | ? serversArg | ||
19 | : [ serversArg ] | ||
20 | |||
21 | for (const server of servers) { | ||
22 | await server.channels.updateImage({ channelName, fixture: 'avatar.png', type: 'avatar' }) | ||
23 | } | ||
24 | } | ||
25 | |||
26 | export { | ||
27 | setDefaultVideoChannel, | ||
28 | setDefaultChannelAvatar | ||
29 | } | ||
diff --git a/shared/server-commands/videos/comments-command.ts b/shared/server-commands/videos/comments-command.ts deleted file mode 100644 index 0dab1b66a..000000000 --- a/shared/server-commands/videos/comments-command.ts +++ /dev/null | |||
@@ -1,159 +0,0 @@ | |||
1 | import { pick } from '@shared/core-utils' | ||
2 | import { HttpStatusCode, ResultList, VideoComment, VideoCommentThreads, VideoCommentThreadTree } from '@shared/models' | ||
3 | import { unwrapBody } from '../requests' | ||
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
5 | |||
6 | export class CommentsCommand extends AbstractCommand { | ||
7 | |||
8 | private lastVideoId: number | string | ||
9 | private lastThreadId: number | ||
10 | private lastReplyId: number | ||
11 | |||
12 | listForAdmin (options: OverrideCommandOptions & { | ||
13 | start?: number | ||
14 | count?: number | ||
15 | sort?: string | ||
16 | isLocal?: boolean | ||
17 | onLocalVideo?: boolean | ||
18 | search?: string | ||
19 | searchAccount?: string | ||
20 | searchVideo?: string | ||
21 | } = {}) { | ||
22 | const { sort = '-createdAt' } = options | ||
23 | const path = '/api/v1/videos/comments' | ||
24 | |||
25 | const query = { sort, ...pick(options, [ 'start', 'count', 'isLocal', 'onLocalVideo', 'search', 'searchAccount', 'searchVideo' ]) } | ||
26 | |||
27 | return this.getRequestBody<ResultList<VideoComment>>({ | ||
28 | ...options, | ||
29 | |||
30 | path, | ||
31 | query, | ||
32 | implicitToken: true, | ||
33 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
34 | }) | ||
35 | } | ||
36 | |||
37 | listThreads (options: OverrideCommandOptions & { | ||
38 | videoId: number | string | ||
39 | videoPassword?: string | ||
40 | start?: number | ||
41 | count?: number | ||
42 | sort?: string | ||
43 | }) { | ||
44 | const { start, count, sort, videoId, videoPassword } = options | ||
45 | const path = '/api/v1/videos/' + videoId + '/comment-threads' | ||
46 | |||
47 | return this.getRequestBody<VideoCommentThreads>({ | ||
48 | ...options, | ||
49 | |||
50 | path, | ||
51 | query: { start, count, sort }, | ||
52 | headers: this.buildVideoPasswordHeader(videoPassword), | ||
53 | implicitToken: false, | ||
54 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
55 | }) | ||
56 | } | ||
57 | |||
58 | getThread (options: OverrideCommandOptions & { | ||
59 | videoId: number | string | ||
60 | threadId: number | ||
61 | }) { | ||
62 | const { videoId, threadId } = options | ||
63 | const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId | ||
64 | |||
65 | return this.getRequestBody<VideoCommentThreadTree>({ | ||
66 | ...options, | ||
67 | |||
68 | path, | ||
69 | implicitToken: false, | ||
70 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
71 | }) | ||
72 | } | ||
73 | |||
74 | async createThread (options: OverrideCommandOptions & { | ||
75 | videoId: number | string | ||
76 | text: string | ||
77 | videoPassword?: string | ||
78 | }) { | ||
79 | const { videoId, text, videoPassword } = options | ||
80 | const path = '/api/v1/videos/' + videoId + '/comment-threads' | ||
81 | |||
82 | const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({ | ||
83 | ...options, | ||
84 | |||
85 | path, | ||
86 | fields: { text }, | ||
87 | headers: this.buildVideoPasswordHeader(videoPassword), | ||
88 | implicitToken: true, | ||
89 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
90 | })) | ||
91 | |||
92 | this.lastThreadId = body.comment?.id | ||
93 | this.lastVideoId = videoId | ||
94 | |||
95 | return body.comment | ||
96 | } | ||
97 | |||
98 | async addReply (options: OverrideCommandOptions & { | ||
99 | videoId: number | string | ||
100 | toCommentId: number | ||
101 | text: string | ||
102 | videoPassword?: string | ||
103 | }) { | ||
104 | const { videoId, toCommentId, text, videoPassword } = options | ||
105 | const path = '/api/v1/videos/' + videoId + '/comments/' + toCommentId | ||
106 | |||
107 | const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({ | ||
108 | ...options, | ||
109 | |||
110 | path, | ||
111 | fields: { text }, | ||
112 | headers: this.buildVideoPasswordHeader(videoPassword), | ||
113 | implicitToken: true, | ||
114 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
115 | })) | ||
116 | |||
117 | this.lastReplyId = body.comment?.id | ||
118 | |||
119 | return body.comment | ||
120 | } | ||
121 | |||
122 | async addReplyToLastReply (options: OverrideCommandOptions & { | ||
123 | text: string | ||
124 | }) { | ||
125 | return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastReplyId }) | ||
126 | } | ||
127 | |||
128 | async addReplyToLastThread (options: OverrideCommandOptions & { | ||
129 | text: string | ||
130 | }) { | ||
131 | return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastThreadId }) | ||
132 | } | ||
133 | |||
134 | async findCommentId (options: OverrideCommandOptions & { | ||
135 | videoId: number | string | ||
136 | text: string | ||
137 | }) { | ||
138 | const { videoId, text } = options | ||
139 | const { data } = await this.listThreads({ videoId, count: 25, sort: '-createdAt' }) | ||
140 | |||
141 | return data.find(c => c.text === text).id | ||
142 | } | ||
143 | |||
144 | delete (options: OverrideCommandOptions & { | ||
145 | videoId: number | string | ||
146 | commentId: number | ||
147 | }) { | ||
148 | const { videoId, commentId } = options | ||
149 | const path = '/api/v1/videos/' + videoId + '/comments/' + commentId | ||
150 | |||
151 | return this.deleteRequest({ | ||
152 | ...options, | ||
153 | |||
154 | path, | ||
155 | implicitToken: true, | ||
156 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
157 | }) | ||
158 | } | ||
159 | } | ||
diff --git a/shared/server-commands/videos/history-command.ts b/shared/server-commands/videos/history-command.ts deleted file mode 100644 index d27afcff2..000000000 --- a/shared/server-commands/videos/history-command.ts +++ /dev/null | |||
@@ -1,54 +0,0 @@ | |||
1 | import { HttpStatusCode, ResultList, Video } from '@shared/models' | ||
2 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
3 | |||
4 | export class HistoryCommand extends AbstractCommand { | ||
5 | |||
6 | list (options: OverrideCommandOptions & { | ||
7 | search?: string | ||
8 | } = {}) { | ||
9 | const { search } = options | ||
10 | const path = '/api/v1/users/me/history/videos' | ||
11 | |||
12 | return this.getRequestBody<ResultList<Video>>({ | ||
13 | ...options, | ||
14 | |||
15 | path, | ||
16 | query: { | ||
17 | search | ||
18 | }, | ||
19 | implicitToken: true, | ||
20 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
21 | }) | ||
22 | } | ||
23 | |||
24 | removeElement (options: OverrideCommandOptions & { | ||
25 | videoId: number | ||
26 | }) { | ||
27 | const { videoId } = options | ||
28 | const path = '/api/v1/users/me/history/videos/' + videoId | ||
29 | |||
30 | return this.deleteRequest({ | ||
31 | ...options, | ||
32 | |||
33 | path, | ||
34 | implicitToken: true, | ||
35 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
36 | }) | ||
37 | } | ||
38 | |||
39 | removeAll (options: OverrideCommandOptions & { | ||
40 | beforeDate?: string | ||
41 | } = {}) { | ||
42 | const { beforeDate } = options | ||
43 | const path = '/api/v1/users/me/history/videos/remove' | ||
44 | |||
45 | return this.postBodyRequest({ | ||
46 | ...options, | ||
47 | |||
48 | path, | ||
49 | fields: { beforeDate }, | ||
50 | implicitToken: true, | ||
51 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
52 | }) | ||
53 | } | ||
54 | } | ||
diff --git a/shared/server-commands/videos/imports-command.ts b/shared/server-commands/videos/imports-command.ts deleted file mode 100644 index e307a79be..000000000 --- a/shared/server-commands/videos/imports-command.ts +++ /dev/null | |||
@@ -1,77 +0,0 @@ | |||
1 | |||
2 | import { HttpStatusCode, ResultList } from '@shared/models' | ||
3 | import { VideoImport, VideoImportCreate } from '../../models/videos' | ||
4 | import { unwrapBody } from '../requests' | ||
5 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
6 | |||
7 | export class ImportsCommand extends AbstractCommand { | ||
8 | |||
9 | importVideo (options: OverrideCommandOptions & { | ||
10 | attributes: (VideoImportCreate | { torrentfile?: string, previewfile?: string, thumbnailfile?: string }) | ||
11 | }) { | ||
12 | const { attributes } = options | ||
13 | const path = '/api/v1/videos/imports' | ||
14 | |||
15 | let attaches: any = {} | ||
16 | if (attributes.torrentfile) attaches = { torrentfile: attributes.torrentfile } | ||
17 | if (attributes.thumbnailfile) attaches = { thumbnailfile: attributes.thumbnailfile } | ||
18 | if (attributes.previewfile) attaches = { previewfile: attributes.previewfile } | ||
19 | |||
20 | return unwrapBody<VideoImport>(this.postUploadRequest({ | ||
21 | ...options, | ||
22 | |||
23 | path, | ||
24 | attaches, | ||
25 | fields: options.attributes, | ||
26 | implicitToken: true, | ||
27 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
28 | })) | ||
29 | } | ||
30 | |||
31 | delete (options: OverrideCommandOptions & { | ||
32 | importId: number | ||
33 | }) { | ||
34 | const path = '/api/v1/videos/imports/' + options.importId | ||
35 | |||
36 | return this.deleteRequest({ | ||
37 | ...options, | ||
38 | |||
39 | path, | ||
40 | implicitToken: true, | ||
41 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
42 | }) | ||
43 | } | ||
44 | |||
45 | cancel (options: OverrideCommandOptions & { | ||
46 | importId: number | ||
47 | }) { | ||
48 | const path = '/api/v1/videos/imports/' + options.importId + '/cancel' | ||
49 | |||
50 | return this.postBodyRequest({ | ||
51 | ...options, | ||
52 | |||
53 | path, | ||
54 | implicitToken: true, | ||
55 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
56 | }) | ||
57 | } | ||
58 | |||
59 | getMyVideoImports (options: OverrideCommandOptions & { | ||
60 | sort?: string | ||
61 | targetUrl?: string | ||
62 | videoChannelSyncId?: number | ||
63 | search?: string | ||
64 | } = {}) { | ||
65 | const { sort, targetUrl, videoChannelSyncId, search } = options | ||
66 | const path = '/api/v1/users/me/videos/imports' | ||
67 | |||
68 | return this.getRequestBody<ResultList<VideoImport>>({ | ||
69 | ...options, | ||
70 | |||
71 | path, | ||
72 | query: { sort, targetUrl, videoChannelSyncId, search }, | ||
73 | implicitToken: true, | ||
74 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
75 | }) | ||
76 | } | ||
77 | } | ||
diff --git a/shared/server-commands/videos/index.ts b/shared/server-commands/videos/index.ts deleted file mode 100644 index 106d80af0..000000000 --- a/shared/server-commands/videos/index.ts +++ /dev/null | |||
@@ -1,21 +0,0 @@ | |||
1 | export * from './blacklist-command' | ||
2 | export * from './captions-command' | ||
3 | export * from './change-ownership-command' | ||
4 | export * from './channels' | ||
5 | export * from './channels-command' | ||
6 | export * from './channel-syncs-command' | ||
7 | export * from './comments-command' | ||
8 | export * from './history-command' | ||
9 | export * from './imports-command' | ||
10 | export * from './live-command' | ||
11 | export * from './live' | ||
12 | export * from './playlists-command' | ||
13 | export * from './services-command' | ||
14 | export * from './storyboard-command' | ||
15 | export * from './streaming-playlists-command' | ||
16 | export * from './comments-command' | ||
17 | export * from './video-studio-command' | ||
18 | export * from './video-token-command' | ||
19 | export * from './views-command' | ||
20 | export * from './videos-command' | ||
21 | export * from './video-passwords-command' | ||
diff --git a/shared/server-commands/videos/live-command.ts b/shared/server-commands/videos/live-command.ts deleted file mode 100644 index 6006d9fe9..000000000 --- a/shared/server-commands/videos/live-command.ts +++ /dev/null | |||
@@ -1,337 +0,0 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { readdir } from 'fs-extra' | ||
4 | import { join } from 'path' | ||
5 | import { omit, wait } from '@shared/core-utils' | ||
6 | import { | ||
7 | HttpStatusCode, | ||
8 | LiveVideo, | ||
9 | LiveVideoCreate, | ||
10 | LiveVideoSession, | ||
11 | LiveVideoUpdate, | ||
12 | ResultList, | ||
13 | VideoCreateResult, | ||
14 | VideoDetails, | ||
15 | VideoPrivacy, | ||
16 | VideoState | ||
17 | } from '@shared/models' | ||
18 | import { unwrapBody } from '../requests' | ||
19 | import { ObjectStorageCommand, PeerTubeServer } from '../server' | ||
20 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
21 | import { sendRTMPStream, testFfmpegStreamError } from './live' | ||
22 | |||
23 | export class LiveCommand extends AbstractCommand { | ||
24 | |||
25 | get (options: OverrideCommandOptions & { | ||
26 | videoId: number | string | ||
27 | }) { | ||
28 | const path = '/api/v1/videos/live' | ||
29 | |||
30 | return this.getRequestBody<LiveVideo>({ | ||
31 | ...options, | ||
32 | |||
33 | path: path + '/' + options.videoId, | ||
34 | implicitToken: true, | ||
35 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
36 | }) | ||
37 | } | ||
38 | |||
39 | // --------------------------------------------------------------------------- | ||
40 | |||
41 | listSessions (options: OverrideCommandOptions & { | ||
42 | videoId: number | string | ||
43 | }) { | ||
44 | const path = `/api/v1/videos/live/${options.videoId}/sessions` | ||
45 | |||
46 | return this.getRequestBody<ResultList<LiveVideoSession>>({ | ||
47 | ...options, | ||
48 | |||
49 | path, | ||
50 | implicitToken: true, | ||
51 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
52 | }) | ||
53 | } | ||
54 | |||
55 | async findLatestSession (options: OverrideCommandOptions & { | ||
56 | videoId: number | string | ||
57 | }) { | ||
58 | const { data: sessions } = await this.listSessions(options) | ||
59 | |||
60 | return sessions[sessions.length - 1] | ||
61 | } | ||
62 | |||
63 | getReplaySession (options: OverrideCommandOptions & { | ||
64 | videoId: number | string | ||
65 | }) { | ||
66 | const path = `/api/v1/videos/${options.videoId}/live-session` | ||
67 | |||
68 | return this.getRequestBody<LiveVideoSession>({ | ||
69 | ...options, | ||
70 | |||
71 | path, | ||
72 | implicitToken: true, | ||
73 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
74 | }) | ||
75 | } | ||
76 | |||
77 | // --------------------------------------------------------------------------- | ||
78 | |||
79 | update (options: OverrideCommandOptions & { | ||
80 | videoId: number | string | ||
81 | fields: LiveVideoUpdate | ||
82 | }) { | ||
83 | const { videoId, fields } = options | ||
84 | const path = '/api/v1/videos/live' | ||
85 | |||
86 | return this.putBodyRequest({ | ||
87 | ...options, | ||
88 | |||
89 | path: path + '/' + videoId, | ||
90 | fields, | ||
91 | implicitToken: true, | ||
92 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
93 | }) | ||
94 | } | ||
95 | |||
96 | async create (options: OverrideCommandOptions & { | ||
97 | fields: LiveVideoCreate | ||
98 | }) { | ||
99 | const { fields } = options | ||
100 | const path = '/api/v1/videos/live' | ||
101 | |||
102 | const attaches: any = {} | ||
103 | if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile | ||
104 | if (fields.previewfile) attaches.previewfile = fields.previewfile | ||
105 | |||
106 | const body = await unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({ | ||
107 | ...options, | ||
108 | |||
109 | path, | ||
110 | attaches, | ||
111 | fields: omit(fields, [ 'thumbnailfile', 'previewfile' ]), | ||
112 | implicitToken: true, | ||
113 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
114 | })) | ||
115 | |||
116 | return body.video | ||
117 | } | ||
118 | |||
119 | async quickCreate (options: OverrideCommandOptions & { | ||
120 | saveReplay: boolean | ||
121 | permanentLive: boolean | ||
122 | privacy?: VideoPrivacy | ||
123 | videoPasswords?: string[] | ||
124 | }) { | ||
125 | const { saveReplay, permanentLive, privacy = VideoPrivacy.PUBLIC, videoPasswords } = options | ||
126 | |||
127 | const replaySettings = privacy === VideoPrivacy.PASSWORD_PROTECTED | ||
128 | ? { privacy: VideoPrivacy.PRIVATE } | ||
129 | : { privacy } | ||
130 | |||
131 | const { uuid } = await this.create({ | ||
132 | ...options, | ||
133 | |||
134 | fields: { | ||
135 | name: 'live', | ||
136 | permanentLive, | ||
137 | saveReplay, | ||
138 | replaySettings, | ||
139 | channelId: this.server.store.channel.id, | ||
140 | privacy, | ||
141 | videoPasswords | ||
142 | } | ||
143 | }) | ||
144 | |||
145 | const video = await this.server.videos.getWithToken({ id: uuid }) | ||
146 | const live = await this.get({ videoId: uuid }) | ||
147 | |||
148 | return { video, live } | ||
149 | } | ||
150 | |||
151 | // --------------------------------------------------------------------------- | ||
152 | |||
153 | async sendRTMPStreamInVideo (options: OverrideCommandOptions & { | ||
154 | videoId: number | string | ||
155 | fixtureName?: string | ||
156 | copyCodecs?: boolean | ||
157 | }) { | ||
158 | const { videoId, fixtureName, copyCodecs } = options | ||
159 | const videoLive = await this.get({ videoId }) | ||
160 | |||
161 | return sendRTMPStream({ rtmpBaseUrl: videoLive.rtmpUrl, streamKey: videoLive.streamKey, fixtureName, copyCodecs }) | ||
162 | } | ||
163 | |||
164 | async runAndTestStreamError (options: OverrideCommandOptions & { | ||
165 | videoId: number | string | ||
166 | shouldHaveError: boolean | ||
167 | }) { | ||
168 | const command = await this.sendRTMPStreamInVideo(options) | ||
169 | |||
170 | return testFfmpegStreamError(command, options.shouldHaveError) | ||
171 | } | ||
172 | |||
173 | // --------------------------------------------------------------------------- | ||
174 | |||
175 | waitUntilPublished (options: OverrideCommandOptions & { | ||
176 | videoId: number | string | ||
177 | }) { | ||
178 | const { videoId } = options | ||
179 | return this.waitUntilState({ videoId, state: VideoState.PUBLISHED }) | ||
180 | } | ||
181 | |||
182 | waitUntilWaiting (options: OverrideCommandOptions & { | ||
183 | videoId: number | string | ||
184 | }) { | ||
185 | const { videoId } = options | ||
186 | return this.waitUntilState({ videoId, state: VideoState.WAITING_FOR_LIVE }) | ||
187 | } | ||
188 | |||
189 | waitUntilEnded (options: OverrideCommandOptions & { | ||
190 | videoId: number | string | ||
191 | }) { | ||
192 | const { videoId } = options | ||
193 | return this.waitUntilState({ videoId, state: VideoState.LIVE_ENDED }) | ||
194 | } | ||
195 | |||
196 | async waitUntilSegmentGeneration (options: OverrideCommandOptions & { | ||
197 | server: PeerTubeServer | ||
198 | videoUUID: string | ||
199 | playlistNumber: number | ||
200 | segment: number | ||
201 | objectStorage?: ObjectStorageCommand | ||
202 | objectStorageBaseUrl?: string | ||
203 | }) { | ||
204 | const { | ||
205 | server, | ||
206 | objectStorage, | ||
207 | playlistNumber, | ||
208 | segment, | ||
209 | videoUUID, | ||
210 | objectStorageBaseUrl | ||
211 | } = options | ||
212 | |||
213 | const segmentName = `${playlistNumber}-00000${segment}.ts` | ||
214 | const baseUrl = objectStorage | ||
215 | ? join(objectStorageBaseUrl || objectStorage.getMockPlaylistBaseUrl(), 'hls') | ||
216 | : server.url + '/static/streaming-playlists/hls' | ||
217 | |||
218 | let error = true | ||
219 | |||
220 | while (error) { | ||
221 | try { | ||
222 | // Check fragment exists | ||
223 | await this.getRawRequest({ | ||
224 | ...options, | ||
225 | |||
226 | url: `${baseUrl}/${videoUUID}/${segmentName}`, | ||
227 | implicitToken: false, | ||
228 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
229 | }) | ||
230 | |||
231 | const video = await server.videos.get({ id: videoUUID }) | ||
232 | const hlsPlaylist = video.streamingPlaylists[0] | ||
233 | |||
234 | // Check SHA generation | ||
235 | const shaBody = await server.streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url, withRetry: !!objectStorage }) | ||
236 | if (!shaBody[segmentName]) { | ||
237 | throw new Error('Segment SHA does not exist') | ||
238 | } | ||
239 | |||
240 | // Check fragment is in m3u8 playlist | ||
241 | const subPlaylist = await server.streamingPlaylists.get({ url: `${baseUrl}/${video.uuid}/${playlistNumber}.m3u8` }) | ||
242 | if (!subPlaylist.includes(segmentName)) throw new Error('Fragment does not exist in playlist') | ||
243 | |||
244 | error = false | ||
245 | } catch { | ||
246 | error = true | ||
247 | await wait(100) | ||
248 | } | ||
249 | } | ||
250 | } | ||
251 | |||
252 | async waitUntilReplacedByReplay (options: OverrideCommandOptions & { | ||
253 | videoId: number | string | ||
254 | }) { | ||
255 | let video: VideoDetails | ||
256 | |||
257 | do { | ||
258 | video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId }) | ||
259 | |||
260 | await wait(500) | ||
261 | } while (video.isLive === true || video.state.id !== VideoState.PUBLISHED) | ||
262 | } | ||
263 | |||
264 | // --------------------------------------------------------------------------- | ||
265 | |||
266 | getSegmentFile (options: OverrideCommandOptions & { | ||
267 | videoUUID: string | ||
268 | playlistNumber: number | ||
269 | segment: number | ||
270 | objectStorage?: ObjectStorageCommand | ||
271 | }) { | ||
272 | const { playlistNumber, segment, videoUUID, objectStorage } = options | ||
273 | |||
274 | const segmentName = `${playlistNumber}-00000${segment}.ts` | ||
275 | const baseUrl = objectStorage | ||
276 | ? objectStorage.getMockPlaylistBaseUrl() | ||
277 | : `${this.server.url}/static/streaming-playlists/hls` | ||
278 | |||
279 | const url = `${baseUrl}/${videoUUID}/${segmentName}` | ||
280 | |||
281 | return this.getRawRequest({ | ||
282 | ...options, | ||
283 | |||
284 | url, | ||
285 | implicitToken: false, | ||
286 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
287 | }) | ||
288 | } | ||
289 | |||
290 | getPlaylistFile (options: OverrideCommandOptions & { | ||
291 | videoUUID: string | ||
292 | playlistName: string | ||
293 | objectStorage?: ObjectStorageCommand | ||
294 | }) { | ||
295 | const { playlistName, videoUUID, objectStorage } = options | ||
296 | |||
297 | const baseUrl = objectStorage | ||
298 | ? objectStorage.getMockPlaylistBaseUrl() | ||
299 | : `${this.server.url}/static/streaming-playlists/hls` | ||
300 | |||
301 | const url = `${baseUrl}/${videoUUID}/${playlistName}` | ||
302 | |||
303 | return this.getRawRequest({ | ||
304 | ...options, | ||
305 | |||
306 | url, | ||
307 | implicitToken: false, | ||
308 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
309 | }) | ||
310 | } | ||
311 | |||
312 | // --------------------------------------------------------------------------- | ||
313 | |||
314 | async countPlaylists (options: OverrideCommandOptions & { | ||
315 | videoUUID: string | ||
316 | }) { | ||
317 | const basePath = this.server.servers.buildDirectory('streaming-playlists') | ||
318 | const hlsPath = join(basePath, 'hls', options.videoUUID) | ||
319 | |||
320 | const files = await readdir(hlsPath) | ||
321 | |||
322 | return files.filter(f => f.endsWith('.m3u8')).length | ||
323 | } | ||
324 | |||
325 | private async waitUntilState (options: OverrideCommandOptions & { | ||
326 | videoId: number | string | ||
327 | state: VideoState | ||
328 | }) { | ||
329 | let video: VideoDetails | ||
330 | |||
331 | do { | ||
332 | video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId }) | ||
333 | |||
334 | await wait(500) | ||
335 | } while (video.state.id !== options.state) | ||
336 | } | ||
337 | } | ||
diff --git a/shared/server-commands/videos/live.ts b/shared/server-commands/videos/live.ts deleted file mode 100644 index cebadb1db..000000000 --- a/shared/server-commands/videos/live.ts +++ /dev/null | |||
@@ -1,128 +0,0 @@ | |||
1 | import ffmpeg, { FfmpegCommand } from 'fluent-ffmpeg' | ||
2 | import { truncate } from 'lodash' | ||
3 | import { buildAbsoluteFixturePath, wait } from '@shared/core-utils' | ||
4 | import { VideoDetails, VideoInclude, VideoPrivacy } from '@shared/models' | ||
5 | import { PeerTubeServer } from '../server/server' | ||
6 | |||
7 | function sendRTMPStream (options: { | ||
8 | rtmpBaseUrl: string | ||
9 | streamKey: string | ||
10 | fixtureName?: string // default video_short.mp4 | ||
11 | copyCodecs?: boolean // default false | ||
12 | }) { | ||
13 | const { rtmpBaseUrl, streamKey, fixtureName = 'video_short.mp4', copyCodecs = false } = options | ||
14 | |||
15 | const fixture = buildAbsoluteFixturePath(fixtureName) | ||
16 | |||
17 | const command = ffmpeg(fixture) | ||
18 | command.inputOption('-stream_loop -1') | ||
19 | command.inputOption('-re') | ||
20 | |||
21 | if (copyCodecs) { | ||
22 | command.outputOption('-c copy') | ||
23 | } else { | ||
24 | command.outputOption('-c:v libx264') | ||
25 | command.outputOption('-g 120') | ||
26 | command.outputOption('-x264-params "no-scenecut=1"') | ||
27 | command.outputOption('-r 60') | ||
28 | } | ||
29 | |||
30 | command.outputOption('-f flv') | ||
31 | |||
32 | const rtmpUrl = rtmpBaseUrl + '/' + streamKey | ||
33 | command.output(rtmpUrl) | ||
34 | |||
35 | command.on('error', err => { | ||
36 | if (err?.message?.includes('Exiting normally')) return | ||
37 | |||
38 | if (process.env.DEBUG) console.error(err) | ||
39 | }) | ||
40 | |||
41 | if (process.env.DEBUG) { | ||
42 | command.on('stderr', data => console.log(data)) | ||
43 | command.on('stdout', data => console.log(data)) | ||
44 | } | ||
45 | |||
46 | command.run() | ||
47 | |||
48 | return command | ||
49 | } | ||
50 | |||
51 | function waitFfmpegUntilError (command: FfmpegCommand, successAfterMS = 10000) { | ||
52 | return new Promise<void>((res, rej) => { | ||
53 | command.on('error', err => { | ||
54 | return rej(err) | ||
55 | }) | ||
56 | |||
57 | setTimeout(() => { | ||
58 | res() | ||
59 | }, successAfterMS) | ||
60 | }) | ||
61 | } | ||
62 | |||
63 | async function testFfmpegStreamError (command: FfmpegCommand, shouldHaveError: boolean) { | ||
64 | let error: Error | ||
65 | |||
66 | try { | ||
67 | await waitFfmpegUntilError(command, 45000) | ||
68 | } catch (err) { | ||
69 | error = err | ||
70 | } | ||
71 | |||
72 | await stopFfmpeg(command) | ||
73 | |||
74 | if (shouldHaveError && !error) throw new Error('Ffmpeg did not have an error') | ||
75 | if (!shouldHaveError && error) throw error | ||
76 | } | ||
77 | |||
78 | async function stopFfmpeg (command: FfmpegCommand) { | ||
79 | command.kill('SIGINT') | ||
80 | |||
81 | await wait(500) | ||
82 | } | ||
83 | |||
84 | async function waitUntilLivePublishedOnAllServers (servers: PeerTubeServer[], videoId: string) { | ||
85 | for (const server of servers) { | ||
86 | await server.live.waitUntilPublished({ videoId }) | ||
87 | } | ||
88 | } | ||
89 | |||
90 | async function waitUntilLiveWaitingOnAllServers (servers: PeerTubeServer[], videoId: string) { | ||
91 | for (const server of servers) { | ||
92 | await server.live.waitUntilWaiting({ videoId }) | ||
93 | } | ||
94 | } | ||
95 | |||
96 | async function waitUntilLiveReplacedByReplayOnAllServers (servers: PeerTubeServer[], videoId: string) { | ||
97 | for (const server of servers) { | ||
98 | await server.live.waitUntilReplacedByReplay({ videoId }) | ||
99 | } | ||
100 | } | ||
101 | |||
102 | async function findExternalSavedVideo (server: PeerTubeServer, liveDetails: VideoDetails) { | ||
103 | const include = VideoInclude.BLACKLISTED | ||
104 | const privacyOneOf = [ VideoPrivacy.INTERNAL, VideoPrivacy.PRIVATE, VideoPrivacy.PUBLIC, VideoPrivacy.UNLISTED ] | ||
105 | |||
106 | const { data } = await server.videos.list({ token: server.accessToken, sort: '-publishedAt', include, privacyOneOf }) | ||
107 | |||
108 | const videoNameSuffix = ` - ${new Date(liveDetails.publishedAt).toLocaleString()}` | ||
109 | const truncatedVideoName = truncate(liveDetails.name, { | ||
110 | length: 120 - videoNameSuffix.length | ||
111 | }) | ||
112 | const toFind = truncatedVideoName + videoNameSuffix | ||
113 | |||
114 | return data.find(v => v.name === toFind) | ||
115 | } | ||
116 | |||
117 | export { | ||
118 | sendRTMPStream, | ||
119 | waitFfmpegUntilError, | ||
120 | testFfmpegStreamError, | ||
121 | stopFfmpeg, | ||
122 | |||
123 | waitUntilLivePublishedOnAllServers, | ||
124 | waitUntilLiveReplacedByReplayOnAllServers, | ||
125 | waitUntilLiveWaitingOnAllServers, | ||
126 | |||
127 | findExternalSavedVideo | ||
128 | } | ||
diff --git a/shared/server-commands/videos/playlists-command.ts b/shared/server-commands/videos/playlists-command.ts deleted file mode 100644 index da3bef7b0..000000000 --- a/shared/server-commands/videos/playlists-command.ts +++ /dev/null | |||
@@ -1,281 +0,0 @@ | |||
1 | import { omit, pick } from '@shared/core-utils' | ||
2 | import { | ||
3 | BooleanBothQuery, | ||
4 | HttpStatusCode, | ||
5 | ResultList, | ||
6 | VideoExistInPlaylist, | ||
7 | VideoPlaylist, | ||
8 | VideoPlaylistCreate, | ||
9 | VideoPlaylistCreateResult, | ||
10 | VideoPlaylistElement, | ||
11 | VideoPlaylistElementCreate, | ||
12 | VideoPlaylistElementCreateResult, | ||
13 | VideoPlaylistElementUpdate, | ||
14 | VideoPlaylistReorder, | ||
15 | VideoPlaylistType, | ||
16 | VideoPlaylistUpdate | ||
17 | } from '@shared/models' | ||
18 | import { unwrapBody } from '../requests' | ||
19 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
20 | |||
21 | export class PlaylistsCommand extends AbstractCommand { | ||
22 | |||
23 | list (options: OverrideCommandOptions & { | ||
24 | start?: number | ||
25 | count?: number | ||
26 | sort?: string | ||
27 | playlistType?: VideoPlaylistType | ||
28 | }) { | ||
29 | const path = '/api/v1/video-playlists' | ||
30 | const query = pick(options, [ 'start', 'count', 'sort', 'playlistType' ]) | ||
31 | |||
32 | return this.getRequestBody<ResultList<VideoPlaylist>>({ | ||
33 | ...options, | ||
34 | |||
35 | path, | ||
36 | query, | ||
37 | implicitToken: false, | ||
38 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
39 | }) | ||
40 | } | ||
41 | |||
42 | listByChannel (options: OverrideCommandOptions & { | ||
43 | handle: string | ||
44 | start?: number | ||
45 | count?: number | ||
46 | sort?: string | ||
47 | playlistType?: VideoPlaylistType | ||
48 | }) { | ||
49 | const path = '/api/v1/video-channels/' + options.handle + '/video-playlists' | ||
50 | const query = pick(options, [ 'start', 'count', 'sort', 'playlistType' ]) | ||
51 | |||
52 | return this.getRequestBody<ResultList<VideoPlaylist>>({ | ||
53 | ...options, | ||
54 | |||
55 | path, | ||
56 | query, | ||
57 | implicitToken: false, | ||
58 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
59 | }) | ||
60 | } | ||
61 | |||
62 | listByAccount (options: OverrideCommandOptions & { | ||
63 | handle: string | ||
64 | start?: number | ||
65 | count?: number | ||
66 | sort?: string | ||
67 | search?: string | ||
68 | playlistType?: VideoPlaylistType | ||
69 | }) { | ||
70 | const path = '/api/v1/accounts/' + options.handle + '/video-playlists' | ||
71 | const query = pick(options, [ 'start', 'count', 'sort', 'search', 'playlistType' ]) | ||
72 | |||
73 | return this.getRequestBody<ResultList<VideoPlaylist>>({ | ||
74 | ...options, | ||
75 | |||
76 | path, | ||
77 | query, | ||
78 | implicitToken: false, | ||
79 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
80 | }) | ||
81 | } | ||
82 | |||
83 | get (options: OverrideCommandOptions & { | ||
84 | playlistId: number | string | ||
85 | }) { | ||
86 | const { playlistId } = options | ||
87 | const path = '/api/v1/video-playlists/' + playlistId | ||
88 | |||
89 | return this.getRequestBody<VideoPlaylist>({ | ||
90 | ...options, | ||
91 | |||
92 | path, | ||
93 | implicitToken: false, | ||
94 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
95 | }) | ||
96 | } | ||
97 | |||
98 | listVideos (options: OverrideCommandOptions & { | ||
99 | playlistId: number | string | ||
100 | start?: number | ||
101 | count?: number | ||
102 | query?: { nsfw?: BooleanBothQuery } | ||
103 | }) { | ||
104 | const path = '/api/v1/video-playlists/' + options.playlistId + '/videos' | ||
105 | const query = options.query ?? {} | ||
106 | |||
107 | return this.getRequestBody<ResultList<VideoPlaylistElement>>({ | ||
108 | ...options, | ||
109 | |||
110 | path, | ||
111 | query: { | ||
112 | ...query, | ||
113 | start: options.start, | ||
114 | count: options.count | ||
115 | }, | ||
116 | implicitToken: true, | ||
117 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
118 | }) | ||
119 | } | ||
120 | |||
121 | delete (options: OverrideCommandOptions & { | ||
122 | playlistId: number | string | ||
123 | }) { | ||
124 | const path = '/api/v1/video-playlists/' + options.playlistId | ||
125 | |||
126 | return this.deleteRequest({ | ||
127 | ...options, | ||
128 | |||
129 | path, | ||
130 | implicitToken: true, | ||
131 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
132 | }) | ||
133 | } | ||
134 | |||
135 | async create (options: OverrideCommandOptions & { | ||
136 | attributes: VideoPlaylistCreate | ||
137 | }) { | ||
138 | const path = '/api/v1/video-playlists' | ||
139 | |||
140 | const fields = omit(options.attributes, [ 'thumbnailfile' ]) | ||
141 | |||
142 | const attaches = options.attributes.thumbnailfile | ||
143 | ? { thumbnailfile: options.attributes.thumbnailfile } | ||
144 | : {} | ||
145 | |||
146 | const body = await unwrapBody<{ videoPlaylist: VideoPlaylistCreateResult }>(this.postUploadRequest({ | ||
147 | ...options, | ||
148 | |||
149 | path, | ||
150 | fields, | ||
151 | attaches, | ||
152 | implicitToken: true, | ||
153 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
154 | })) | ||
155 | |||
156 | return body.videoPlaylist | ||
157 | } | ||
158 | |||
159 | update (options: OverrideCommandOptions & { | ||
160 | attributes: VideoPlaylistUpdate | ||
161 | playlistId: number | string | ||
162 | }) { | ||
163 | const path = '/api/v1/video-playlists/' + options.playlistId | ||
164 | |||
165 | const fields = omit(options.attributes, [ 'thumbnailfile' ]) | ||
166 | |||
167 | const attaches = options.attributes.thumbnailfile | ||
168 | ? { thumbnailfile: options.attributes.thumbnailfile } | ||
169 | : {} | ||
170 | |||
171 | return this.putUploadRequest({ | ||
172 | ...options, | ||
173 | |||
174 | path, | ||
175 | fields, | ||
176 | attaches, | ||
177 | implicitToken: true, | ||
178 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
179 | }) | ||
180 | } | ||
181 | |||
182 | async addElement (options: OverrideCommandOptions & { | ||
183 | playlistId: number | string | ||
184 | attributes: VideoPlaylistElementCreate | { videoId: string } | ||
185 | }) { | ||
186 | const attributes = { | ||
187 | ...options.attributes, | ||
188 | |||
189 | videoId: await this.server.videos.getId({ ...options, uuid: options.attributes.videoId }) | ||
190 | } | ||
191 | |||
192 | const path = '/api/v1/video-playlists/' + options.playlistId + '/videos' | ||
193 | |||
194 | const body = await unwrapBody<{ videoPlaylistElement: VideoPlaylistElementCreateResult }>(this.postBodyRequest({ | ||
195 | ...options, | ||
196 | |||
197 | path, | ||
198 | fields: attributes, | ||
199 | implicitToken: true, | ||
200 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
201 | })) | ||
202 | |||
203 | return body.videoPlaylistElement | ||
204 | } | ||
205 | |||
206 | updateElement (options: OverrideCommandOptions & { | ||
207 | playlistId: number | string | ||
208 | elementId: number | string | ||
209 | attributes: VideoPlaylistElementUpdate | ||
210 | }) { | ||
211 | const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.elementId | ||
212 | |||
213 | return this.putBodyRequest({ | ||
214 | ...options, | ||
215 | |||
216 | path, | ||
217 | fields: options.attributes, | ||
218 | implicitToken: true, | ||
219 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
220 | }) | ||
221 | } | ||
222 | |||
223 | removeElement (options: OverrideCommandOptions & { | ||
224 | playlistId: number | string | ||
225 | elementId: number | ||
226 | }) { | ||
227 | const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.elementId | ||
228 | |||
229 | return this.deleteRequest({ | ||
230 | ...options, | ||
231 | |||
232 | path, | ||
233 | implicitToken: true, | ||
234 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
235 | }) | ||
236 | } | ||
237 | |||
238 | reorderElements (options: OverrideCommandOptions & { | ||
239 | playlistId: number | string | ||
240 | attributes: VideoPlaylistReorder | ||
241 | }) { | ||
242 | const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/reorder' | ||
243 | |||
244 | return this.postBodyRequest({ | ||
245 | ...options, | ||
246 | |||
247 | path, | ||
248 | fields: options.attributes, | ||
249 | implicitToken: true, | ||
250 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
251 | }) | ||
252 | } | ||
253 | |||
254 | getPrivacies (options: OverrideCommandOptions = {}) { | ||
255 | const path = '/api/v1/video-playlists/privacies' | ||
256 | |||
257 | return this.getRequestBody<{ [ id: number ]: string }>({ | ||
258 | ...options, | ||
259 | |||
260 | path, | ||
261 | implicitToken: false, | ||
262 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
263 | }) | ||
264 | } | ||
265 | |||
266 | videosExist (options: OverrideCommandOptions & { | ||
267 | videoIds: number[] | ||
268 | }) { | ||
269 | const { videoIds } = options | ||
270 | const path = '/api/v1/users/me/video-playlists/videos-exist' | ||
271 | |||
272 | return this.getRequestBody<VideoExistInPlaylist>({ | ||
273 | ...options, | ||
274 | |||
275 | path, | ||
276 | query: { videoIds }, | ||
277 | implicitToken: true, | ||
278 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
279 | }) | ||
280 | } | ||
281 | } | ||
diff --git a/shared/server-commands/videos/services-command.ts b/shared/server-commands/videos/services-command.ts deleted file mode 100644 index 06760df42..000000000 --- a/shared/server-commands/videos/services-command.ts +++ /dev/null | |||
@@ -1,29 +0,0 @@ | |||
1 | import { HttpStatusCode } from '@shared/models' | ||
2 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
3 | |||
4 | export class ServicesCommand extends AbstractCommand { | ||
5 | |||
6 | getOEmbed (options: OverrideCommandOptions & { | ||
7 | oembedUrl: string | ||
8 | format?: string | ||
9 | maxHeight?: number | ||
10 | maxWidth?: number | ||
11 | }) { | ||
12 | const path = '/services/oembed' | ||
13 | const query = { | ||
14 | url: options.oembedUrl, | ||
15 | format: options.format, | ||
16 | maxheight: options.maxHeight, | ||
17 | maxwidth: options.maxWidth | ||
18 | } | ||
19 | |||
20 | return this.getRequest({ | ||
21 | ...options, | ||
22 | |||
23 | path, | ||
24 | query, | ||
25 | implicitToken: false, | ||
26 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
27 | }) | ||
28 | } | ||
29 | } | ||
diff --git a/shared/server-commands/videos/storyboard-command.ts b/shared/server-commands/videos/storyboard-command.ts deleted file mode 100644 index 06d90fc12..000000000 --- a/shared/server-commands/videos/storyboard-command.ts +++ /dev/null | |||
@@ -1,19 +0,0 @@ | |||
1 | import { HttpStatusCode, Storyboard } from '@shared/models' | ||
2 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
3 | |||
4 | export class StoryboardCommand extends AbstractCommand { | ||
5 | |||
6 | list (options: OverrideCommandOptions & { | ||
7 | id: number | string | ||
8 | }) { | ||
9 | const path = '/api/v1/videos/' + options.id + '/storyboards' | ||
10 | |||
11 | return this.getRequestBody<{ storyboards: Storyboard[] }>({ | ||
12 | ...options, | ||
13 | |||
14 | path, | ||
15 | implicitToken: true, | ||
16 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
17 | }) | ||
18 | } | ||
19 | } | ||
diff --git a/shared/server-commands/videos/streaming-playlists-command.ts b/shared/server-commands/videos/streaming-playlists-command.ts deleted file mode 100644 index b988ac4b2..000000000 --- a/shared/server-commands/videos/streaming-playlists-command.ts +++ /dev/null | |||
@@ -1,119 +0,0 @@ | |||
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 | }): Promise<string> { | ||
17 | const { videoFileToken, reinjectVideoFileToken, expectedStatus, withRetry = false, 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 | // master.m3u8 could be empty | ||
33 | if (!result && (!expectedStatus || expectedStatus === HttpStatusCode.OK_200)) { | ||
34 | throw new Error('Empty result') | ||
35 | } | ||
36 | |||
37 | return result | ||
38 | } catch (err) { | ||
39 | if (!withRetry || currentRetry > 10) throw err | ||
40 | |||
41 | await wait(250) | ||
42 | |||
43 | return this.get({ | ||
44 | ...options, | ||
45 | |||
46 | withRetry, | ||
47 | currentRetry: currentRetry + 1 | ||
48 | }) | ||
49 | } | ||
50 | } | ||
51 | |||
52 | async getFragmentedSegment (options: OverrideCommandOptions & { | ||
53 | url: string | ||
54 | range?: string | ||
55 | |||
56 | withRetry?: boolean // default false | ||
57 | currentRetry?: number | ||
58 | }) { | ||
59 | const { withRetry = false, currentRetry = 1 } = options | ||
60 | |||
61 | try { | ||
62 | const result = await unwrapBody<Buffer>(this.getRawRequest({ | ||
63 | ...options, | ||
64 | |||
65 | url: options.url, | ||
66 | range: options.range, | ||
67 | implicitToken: false, | ||
68 | responseType: 'application/octet-stream', | ||
69 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
70 | })) | ||
71 | |||
72 | return result | ||
73 | } catch (err) { | ||
74 | if (!withRetry || currentRetry > 10) throw err | ||
75 | |||
76 | await wait(250) | ||
77 | |||
78 | return this.getFragmentedSegment({ | ||
79 | ...options, | ||
80 | |||
81 | withRetry, | ||
82 | currentRetry: currentRetry + 1 | ||
83 | }) | ||
84 | } | ||
85 | } | ||
86 | |||
87 | async getSegmentSha256 (options: OverrideCommandOptions & { | ||
88 | url: string | ||
89 | |||
90 | withRetry?: boolean // default false | ||
91 | currentRetry?: number | ||
92 | }) { | ||
93 | const { withRetry = false, currentRetry = 1 } = options | ||
94 | |||
95 | try { | ||
96 | const result = await unwrapBodyOrDecodeToJSON<{ [ id: string ]: string }>(this.getRawRequest({ | ||
97 | ...options, | ||
98 | |||
99 | url: options.url, | ||
100 | contentType: 'application/json', | ||
101 | implicitToken: false, | ||
102 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
103 | })) | ||
104 | |||
105 | return result | ||
106 | } catch (err) { | ||
107 | if (!withRetry || currentRetry > 10) throw err | ||
108 | |||
109 | await wait(250) | ||
110 | |||
111 | return this.getSegmentSha256({ | ||
112 | ...options, | ||
113 | |||
114 | withRetry, | ||
115 | currentRetry: currentRetry + 1 | ||
116 | }) | ||
117 | } | ||
118 | } | ||
119 | } | ||
diff --git a/shared/server-commands/videos/video-passwords-command.ts b/shared/server-commands/videos/video-passwords-command.ts deleted file mode 100644 index bf10335b4..000000000 --- a/shared/server-commands/videos/video-passwords-command.ts +++ /dev/null | |||
@@ -1,55 +0,0 @@ | |||
1 | import { HttpStatusCode, ResultList, VideoPassword } from '@shared/models' | ||
2 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
3 | export class VideoPasswordsCommand extends AbstractCommand { | ||
4 | |||
5 | list (options: OverrideCommandOptions & { | ||
6 | videoId: number | string | ||
7 | start?: number | ||
8 | count?: number | ||
9 | sort?: string | ||
10 | }) { | ||
11 | const { start, count, sort, videoId } = options | ||
12 | const path = '/api/v1/videos/' + videoId + '/passwords' | ||
13 | |||
14 | return this.getRequestBody<ResultList<VideoPassword>>({ | ||
15 | ...options, | ||
16 | |||
17 | path, | ||
18 | query: { start, count, sort }, | ||
19 | implicitToken: true, | ||
20 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
21 | }) | ||
22 | } | ||
23 | |||
24 | updateAll (options: OverrideCommandOptions & { | ||
25 | videoId: number | string | ||
26 | passwords: string[] | ||
27 | }) { | ||
28 | const { videoId, passwords } = options | ||
29 | const path = `/api/v1/videos/${videoId}/passwords` | ||
30 | |||
31 | return this.putBodyRequest({ | ||
32 | ...options, | ||
33 | path, | ||
34 | fields: { passwords }, | ||
35 | implicitToken: true, | ||
36 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
37 | }) | ||
38 | } | ||
39 | |||
40 | remove (options: OverrideCommandOptions & { | ||
41 | id: number | ||
42 | videoId: number | string | ||
43 | }) { | ||
44 | const { id, videoId } = options | ||
45 | const path = `/api/v1/videos/${videoId}/passwords/${id}` | ||
46 | |||
47 | return this.deleteRequest({ | ||
48 | ...options, | ||
49 | |||
50 | path, | ||
51 | implicitToken: true, | ||
52 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
53 | }) | ||
54 | } | ||
55 | } | ||
diff --git a/shared/server-commands/videos/video-stats-command.ts b/shared/server-commands/videos/video-stats-command.ts deleted file mode 100644 index b9b99bfb5..000000000 --- a/shared/server-commands/videos/video-stats-command.ts +++ /dev/null | |||
@@ -1,56 +0,0 @@ | |||
1 | import { pick } from '@shared/core-utils' | ||
2 | import { HttpStatusCode, VideoStatsOverall, VideoStatsRetention, VideoStatsTimeserie, VideoStatsTimeserieMetric } from '@shared/models' | ||
3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
4 | |||
5 | export class VideoStatsCommand extends AbstractCommand { | ||
6 | |||
7 | getOverallStats (options: OverrideCommandOptions & { | ||
8 | videoId: number | string | ||
9 | startDate?: string | ||
10 | endDate?: string | ||
11 | }) { | ||
12 | const path = '/api/v1/videos/' + options.videoId + '/stats/overall' | ||
13 | |||
14 | return this.getRequestBody<VideoStatsOverall>({ | ||
15 | ...options, | ||
16 | path, | ||
17 | |||
18 | query: pick(options, [ 'startDate', 'endDate' ]), | ||
19 | |||
20 | implicitToken: true, | ||
21 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
22 | }) | ||
23 | } | ||
24 | |||
25 | getTimeserieStats (options: OverrideCommandOptions & { | ||
26 | videoId: number | string | ||
27 | metric: VideoStatsTimeserieMetric | ||
28 | startDate?: Date | ||
29 | endDate?: Date | ||
30 | }) { | ||
31 | const path = '/api/v1/videos/' + options.videoId + '/stats/timeseries/' + options.metric | ||
32 | |||
33 | return this.getRequestBody<VideoStatsTimeserie>({ | ||
34 | ...options, | ||
35 | path, | ||
36 | |||
37 | query: pick(options, [ 'startDate', 'endDate' ]), | ||
38 | implicitToken: true, | ||
39 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
40 | }) | ||
41 | } | ||
42 | |||
43 | getRetentionStats (options: OverrideCommandOptions & { | ||
44 | videoId: number | string | ||
45 | }) { | ||
46 | const path = '/api/v1/videos/' + options.videoId + '/stats/retention' | ||
47 | |||
48 | return this.getRequestBody<VideoStatsRetention>({ | ||
49 | ...options, | ||
50 | path, | ||
51 | |||
52 | implicitToken: true, | ||
53 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
54 | }) | ||
55 | } | ||
56 | } | ||
diff --git a/shared/server-commands/videos/video-studio-command.ts b/shared/server-commands/videos/video-studio-command.ts deleted file mode 100644 index 675cd84b7..000000000 --- a/shared/server-commands/videos/video-studio-command.ts +++ /dev/null | |||
@@ -1,67 +0,0 @@ | |||
1 | import { HttpStatusCode, VideoStudioTask } from '@shared/models' | ||
2 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
3 | |||
4 | export class VideoStudioCommand extends AbstractCommand { | ||
5 | |||
6 | static getComplexTask (): VideoStudioTask[] { | ||
7 | return [ | ||
8 | // Total duration: 2 | ||
9 | { | ||
10 | name: 'cut', | ||
11 | options: { | ||
12 | start: 1, | ||
13 | end: 3 | ||
14 | } | ||
15 | }, | ||
16 | |||
17 | // Total duration: 7 | ||
18 | { | ||
19 | name: 'add-outro', | ||
20 | options: { | ||
21 | file: 'video_short.webm' | ||
22 | } | ||
23 | }, | ||
24 | |||
25 | { | ||
26 | name: 'add-watermark', | ||
27 | options: { | ||
28 | file: 'custom-thumbnail.png' | ||
29 | } | ||
30 | }, | ||
31 | |||
32 | // Total duration: 9 | ||
33 | { | ||
34 | name: 'add-intro', | ||
35 | options: { | ||
36 | file: 'video_very_short_240p.mp4' | ||
37 | } | ||
38 | } | ||
39 | ] | ||
40 | } | ||
41 | |||
42 | createEditionTasks (options: OverrideCommandOptions & { | ||
43 | videoId: number | string | ||
44 | tasks: VideoStudioTask[] | ||
45 | }) { | ||
46 | const path = '/api/v1/videos/' + options.videoId + '/studio/edit' | ||
47 | const attaches: { [id: string]: any } = {} | ||
48 | |||
49 | for (let i = 0; i < options.tasks.length; i++) { | ||
50 | const task = options.tasks[i] | ||
51 | |||
52 | if (task.name === 'add-intro' || task.name === 'add-outro' || task.name === 'add-watermark') { | ||
53 | attaches[`tasks[${i}][options][file]`] = task.options.file | ||
54 | } | ||
55 | } | ||
56 | |||
57 | return this.postUploadRequest({ | ||
58 | ...options, | ||
59 | |||
60 | path, | ||
61 | attaches, | ||
62 | fields: { tasks: options.tasks }, | ||
63 | implicitToken: true, | ||
64 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
65 | }) | ||
66 | } | ||
67 | } | ||
diff --git a/shared/server-commands/videos/video-token-command.ts b/shared/server-commands/videos/video-token-command.ts deleted file mode 100644 index c4ed29a8c..000000000 --- a/shared/server-commands/videos/video-token-command.ts +++ /dev/null | |||
@@ -1,34 +0,0 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ | ||
2 | |||
3 | import { HttpStatusCode, VideoToken } from '@shared/models' | ||
4 | import { unwrapBody } from '../requests' | ||
5 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
6 | |||
7 | export class VideoTokenCommand extends AbstractCommand { | ||
8 | |||
9 | create (options: OverrideCommandOptions & { | ||
10 | videoId: number | string | ||
11 | videoPassword?: string | ||
12 | }) { | ||
13 | const { videoId, videoPassword } = options | ||
14 | const path = '/api/v1/videos/' + videoId + '/token' | ||
15 | |||
16 | return unwrapBody<VideoToken>(this.postBodyRequest({ | ||
17 | ...options, | ||
18 | headers: this.buildVideoPasswordHeader(videoPassword), | ||
19 | |||
20 | path, | ||
21 | implicitToken: true, | ||
22 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
23 | })) | ||
24 | } | ||
25 | |||
26 | async getVideoFileToken (options: OverrideCommandOptions & { | ||
27 | videoId: number | string | ||
28 | videoPassword?: string | ||
29 | }) { | ||
30 | const { files } = await this.create(options) | ||
31 | |||
32 | return files.token | ||
33 | } | ||
34 | } | ||
diff --git a/shared/server-commands/videos/videos-command.ts b/shared/server-commands/videos/videos-command.ts deleted file mode 100644 index 4c3513ed4..000000000 --- a/shared/server-commands/videos/videos-command.ts +++ /dev/null | |||
@@ -1,829 +0,0 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ | ||
2 | |||
3 | import { expect } from 'chai' | ||
4 | import { createReadStream, stat } from 'fs-extra' | ||
5 | import got, { Response as GotResponse } from 'got' | ||
6 | import validator from 'validator' | ||
7 | import { buildAbsoluteFixturePath, getAllPrivacies, omit, pick, wait } from '@shared/core-utils' | ||
8 | import { buildUUID } from '@shared/extra-utils' | ||
9 | import { | ||
10 | HttpStatusCode, | ||
11 | ResultList, | ||
12 | UserVideoRateType, | ||
13 | Video, | ||
14 | VideoCreate, | ||
15 | VideoCreateResult, | ||
16 | VideoDetails, | ||
17 | VideoFileMetadata, | ||
18 | VideoInclude, | ||
19 | VideoPrivacy, | ||
20 | VideosCommonQuery, | ||
21 | VideoTranscodingCreate | ||
22 | } from '@shared/models' | ||
23 | import { VideoSource } from '@shared/models/videos/video-source' | ||
24 | import { unwrapBody } from '../requests' | ||
25 | import { waitJobs } from '../server' | ||
26 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
27 | |||
28 | export type VideoEdit = Partial<Omit<VideoCreate, 'thumbnailfile' | 'previewfile'>> & { | ||
29 | fixture?: string | ||
30 | thumbnailfile?: string | ||
31 | previewfile?: string | ||
32 | } | ||
33 | |||
34 | export class VideosCommand extends AbstractCommand { | ||
35 | |||
36 | getCategories (options: OverrideCommandOptions = {}) { | ||
37 | const path = '/api/v1/videos/categories' | ||
38 | |||
39 | return this.getRequestBody<{ [id: number]: string }>({ | ||
40 | ...options, | ||
41 | path, | ||
42 | |||
43 | implicitToken: false, | ||
44 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
45 | }) | ||
46 | } | ||
47 | |||
48 | getLicences (options: OverrideCommandOptions = {}) { | ||
49 | const path = '/api/v1/videos/licences' | ||
50 | |||
51 | return this.getRequestBody<{ [id: number]: string }>({ | ||
52 | ...options, | ||
53 | path, | ||
54 | |||
55 | implicitToken: false, | ||
56 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
57 | }) | ||
58 | } | ||
59 | |||
60 | getLanguages (options: OverrideCommandOptions = {}) { | ||
61 | const path = '/api/v1/videos/languages' | ||
62 | |||
63 | return this.getRequestBody<{ [id: string]: string }>({ | ||
64 | ...options, | ||
65 | path, | ||
66 | |||
67 | implicitToken: false, | ||
68 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
69 | }) | ||
70 | } | ||
71 | |||
72 | getPrivacies (options: OverrideCommandOptions = {}) { | ||
73 | const path = '/api/v1/videos/privacies' | ||
74 | |||
75 | return this.getRequestBody<{ [id in VideoPrivacy]: string }>({ | ||
76 | ...options, | ||
77 | path, | ||
78 | |||
79 | implicitToken: false, | ||
80 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
81 | }) | ||
82 | } | ||
83 | |||
84 | // --------------------------------------------------------------------------- | ||
85 | |||
86 | getDescription (options: OverrideCommandOptions & { | ||
87 | descriptionPath: string | ||
88 | }) { | ||
89 | return this.getRequestBody<{ description: string }>({ | ||
90 | ...options, | ||
91 | path: options.descriptionPath, | ||
92 | |||
93 | implicitToken: false, | ||
94 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
95 | }) | ||
96 | } | ||
97 | |||
98 | getFileMetadata (options: OverrideCommandOptions & { | ||
99 | url: string | ||
100 | }) { | ||
101 | return unwrapBody<VideoFileMetadata>(this.getRawRequest({ | ||
102 | ...options, | ||
103 | |||
104 | url: options.url, | ||
105 | implicitToken: false, | ||
106 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
107 | })) | ||
108 | } | ||
109 | |||
110 | // --------------------------------------------------------------------------- | ||
111 | |||
112 | rate (options: OverrideCommandOptions & { | ||
113 | id: number | string | ||
114 | rating: UserVideoRateType | ||
115 | videoPassword?: string | ||
116 | }) { | ||
117 | const { id, rating, videoPassword } = options | ||
118 | const path = '/api/v1/videos/' + id + '/rate' | ||
119 | |||
120 | return this.putBodyRequest({ | ||
121 | ...options, | ||
122 | |||
123 | path, | ||
124 | fields: { rating }, | ||
125 | headers: this.buildVideoPasswordHeader(videoPassword), | ||
126 | implicitToken: true, | ||
127 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
128 | }) | ||
129 | } | ||
130 | |||
131 | // --------------------------------------------------------------------------- | ||
132 | |||
133 | get (options: OverrideCommandOptions & { | ||
134 | id: number | string | ||
135 | }) { | ||
136 | const path = '/api/v1/videos/' + options.id | ||
137 | |||
138 | return this.getRequestBody<VideoDetails>({ | ||
139 | ...options, | ||
140 | |||
141 | path, | ||
142 | implicitToken: false, | ||
143 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
144 | }) | ||
145 | } | ||
146 | |||
147 | getWithToken (options: OverrideCommandOptions & { | ||
148 | id: number | string | ||
149 | }) { | ||
150 | return this.get({ | ||
151 | ...options, | ||
152 | |||
153 | token: this.buildCommonRequestToken({ ...options, implicitToken: true }) | ||
154 | }) | ||
155 | } | ||
156 | |||
157 | getWithPassword (options: OverrideCommandOptions & { | ||
158 | id: number | string | ||
159 | password?: string | ||
160 | }) { | ||
161 | const path = '/api/v1/videos/' + options.id | ||
162 | |||
163 | return this.getRequestBody<VideoDetails>({ | ||
164 | ...options, | ||
165 | headers:{ | ||
166 | 'x-peertube-video-password': options.password | ||
167 | }, | ||
168 | path, | ||
169 | implicitToken: false, | ||
170 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
171 | }) | ||
172 | } | ||
173 | |||
174 | getSource (options: OverrideCommandOptions & { | ||
175 | id: number | string | ||
176 | }) { | ||
177 | const path = '/api/v1/videos/' + options.id + '/source' | ||
178 | |||
179 | return this.getRequestBody<VideoSource>({ | ||
180 | ...options, | ||
181 | |||
182 | path, | ||
183 | implicitToken: true, | ||
184 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
185 | }) | ||
186 | } | ||
187 | |||
188 | async getId (options: OverrideCommandOptions & { | ||
189 | uuid: number | string | ||
190 | }) { | ||
191 | const { uuid } = options | ||
192 | |||
193 | if (validator.isUUID('' + uuid) === false) return uuid as number | ||
194 | |||
195 | const { id } = await this.get({ ...options, id: uuid }) | ||
196 | |||
197 | return id | ||
198 | } | ||
199 | |||
200 | async listFiles (options: OverrideCommandOptions & { | ||
201 | id: number | string | ||
202 | }) { | ||
203 | const video = await this.get(options) | ||
204 | |||
205 | const files = video.files || [] | ||
206 | const hlsFiles = video.streamingPlaylists[0]?.files || [] | ||
207 | |||
208 | return files.concat(hlsFiles) | ||
209 | } | ||
210 | |||
211 | // --------------------------------------------------------------------------- | ||
212 | |||
213 | listMyVideos (options: OverrideCommandOptions & { | ||
214 | start?: number | ||
215 | count?: number | ||
216 | sort?: string | ||
217 | search?: string | ||
218 | isLive?: boolean | ||
219 | channelId?: number | ||
220 | } = {}) { | ||
221 | const path = '/api/v1/users/me/videos' | ||
222 | |||
223 | return this.getRequestBody<ResultList<Video>>({ | ||
224 | ...options, | ||
225 | |||
226 | path, | ||
227 | query: pick(options, [ 'start', 'count', 'sort', 'search', 'isLive', 'channelId' ]), | ||
228 | implicitToken: true, | ||
229 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
230 | }) | ||
231 | } | ||
232 | |||
233 | listMySubscriptionVideos (options: OverrideCommandOptions & VideosCommonQuery = {}) { | ||
234 | const { sort = '-createdAt' } = options | ||
235 | const path = '/api/v1/users/me/subscriptions/videos' | ||
236 | |||
237 | return this.getRequestBody<ResultList<Video>>({ | ||
238 | ...options, | ||
239 | |||
240 | path, | ||
241 | query: { sort, ...this.buildListQuery(options) }, | ||
242 | implicitToken: true, | ||
243 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
244 | }) | ||
245 | } | ||
246 | |||
247 | // --------------------------------------------------------------------------- | ||
248 | |||
249 | list (options: OverrideCommandOptions & VideosCommonQuery = {}) { | ||
250 | const path = '/api/v1/videos' | ||
251 | |||
252 | const query = this.buildListQuery(options) | ||
253 | |||
254 | return this.getRequestBody<ResultList<Video>>({ | ||
255 | ...options, | ||
256 | |||
257 | path, | ||
258 | query: { sort: 'name', ...query }, | ||
259 | implicitToken: false, | ||
260 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
261 | }) | ||
262 | } | ||
263 | |||
264 | listWithToken (options: OverrideCommandOptions & VideosCommonQuery = {}) { | ||
265 | return this.list({ | ||
266 | ...options, | ||
267 | |||
268 | token: this.buildCommonRequestToken({ ...options, implicitToken: true }) | ||
269 | }) | ||
270 | } | ||
271 | |||
272 | listAllForAdmin (options: OverrideCommandOptions & VideosCommonQuery = {}) { | ||
273 | const include = VideoInclude.NOT_PUBLISHED_STATE | VideoInclude.BLACKLISTED | VideoInclude.BLOCKED_OWNER | ||
274 | const nsfw = 'both' | ||
275 | const privacyOneOf = getAllPrivacies() | ||
276 | |||
277 | return this.list({ | ||
278 | ...options, | ||
279 | |||
280 | include, | ||
281 | nsfw, | ||
282 | privacyOneOf, | ||
283 | |||
284 | token: this.buildCommonRequestToken({ ...options, implicitToken: true }) | ||
285 | }) | ||
286 | } | ||
287 | |||
288 | listByAccount (options: OverrideCommandOptions & VideosCommonQuery & { | ||
289 | handle: string | ||
290 | }) { | ||
291 | const { handle, search } = options | ||
292 | const path = '/api/v1/accounts/' + handle + '/videos' | ||
293 | |||
294 | return this.getRequestBody<ResultList<Video>>({ | ||
295 | ...options, | ||
296 | |||
297 | path, | ||
298 | query: { search, ...this.buildListQuery(options) }, | ||
299 | implicitToken: true, | ||
300 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
301 | }) | ||
302 | } | ||
303 | |||
304 | listByChannel (options: OverrideCommandOptions & VideosCommonQuery & { | ||
305 | handle: string | ||
306 | }) { | ||
307 | const { handle } = options | ||
308 | const path = '/api/v1/video-channels/' + handle + '/videos' | ||
309 | |||
310 | return this.getRequestBody<ResultList<Video>>({ | ||
311 | ...options, | ||
312 | |||
313 | path, | ||
314 | query: this.buildListQuery(options), | ||
315 | implicitToken: true, | ||
316 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
317 | }) | ||
318 | } | ||
319 | |||
320 | // --------------------------------------------------------------------------- | ||
321 | |||
322 | async find (options: OverrideCommandOptions & { | ||
323 | name: string | ||
324 | }) { | ||
325 | const { data } = await this.list(options) | ||
326 | |||
327 | return data.find(v => v.name === options.name) | ||
328 | } | ||
329 | |||
330 | // --------------------------------------------------------------------------- | ||
331 | |||
332 | update (options: OverrideCommandOptions & { | ||
333 | id: number | string | ||
334 | attributes?: VideoEdit | ||
335 | }) { | ||
336 | const { id, attributes = {} } = options | ||
337 | const path = '/api/v1/videos/' + id | ||
338 | |||
339 | // Upload request | ||
340 | if (attributes.thumbnailfile || attributes.previewfile) { | ||
341 | const attaches: any = {} | ||
342 | if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile | ||
343 | if (attributes.previewfile) attaches.previewfile = attributes.previewfile | ||
344 | |||
345 | return this.putUploadRequest({ | ||
346 | ...options, | ||
347 | |||
348 | path, | ||
349 | fields: options.attributes, | ||
350 | attaches: { | ||
351 | thumbnailfile: attributes.thumbnailfile, | ||
352 | previewfile: attributes.previewfile | ||
353 | }, | ||
354 | implicitToken: true, | ||
355 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
356 | }) | ||
357 | } | ||
358 | |||
359 | return this.putBodyRequest({ | ||
360 | ...options, | ||
361 | |||
362 | path, | ||
363 | fields: options.attributes, | ||
364 | implicitToken: true, | ||
365 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
366 | }) | ||
367 | } | ||
368 | |||
369 | remove (options: OverrideCommandOptions & { | ||
370 | id: number | string | ||
371 | }) { | ||
372 | const path = '/api/v1/videos/' + options.id | ||
373 | |||
374 | return unwrapBody(this.deleteRequest({ | ||
375 | ...options, | ||
376 | |||
377 | path, | ||
378 | implicitToken: true, | ||
379 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
380 | })) | ||
381 | } | ||
382 | |||
383 | async removeAll () { | ||
384 | const { data } = await this.list() | ||
385 | |||
386 | for (const v of data) { | ||
387 | await this.remove({ id: v.id }) | ||
388 | } | ||
389 | } | ||
390 | |||
391 | // --------------------------------------------------------------------------- | ||
392 | |||
393 | async upload (options: OverrideCommandOptions & { | ||
394 | attributes?: VideoEdit | ||
395 | mode?: 'legacy' | 'resumable' // default legacy | ||
396 | waitTorrentGeneration?: boolean // default true | ||
397 | completedExpectedStatus?: HttpStatusCode | ||
398 | } = {}) { | ||
399 | const { mode = 'legacy', waitTorrentGeneration = true } = options | ||
400 | let defaultChannelId = 1 | ||
401 | |||
402 | try { | ||
403 | const { videoChannels } = await this.server.users.getMyInfo({ token: options.token }) | ||
404 | defaultChannelId = videoChannels[0].id | ||
405 | } catch (e) { /* empty */ } | ||
406 | |||
407 | // Override default attributes | ||
408 | const attributes = { | ||
409 | name: 'my super video', | ||
410 | category: 5, | ||
411 | licence: 4, | ||
412 | language: 'zh', | ||
413 | channelId: defaultChannelId, | ||
414 | nsfw: true, | ||
415 | waitTranscoding: false, | ||
416 | description: 'my super description', | ||
417 | support: 'my super support text', | ||
418 | tags: [ 'tag' ], | ||
419 | privacy: VideoPrivacy.PUBLIC, | ||
420 | commentsEnabled: true, | ||
421 | downloadEnabled: true, | ||
422 | fixture: 'video_short.webm', | ||
423 | |||
424 | ...options.attributes | ||
425 | } | ||
426 | |||
427 | const created = mode === 'legacy' | ||
428 | ? await this.buildLegacyUpload({ ...options, attributes }) | ||
429 | : await this.buildResumeUpload({ ...options, path: '/api/v1/videos/upload-resumable', attributes }) | ||
430 | |||
431 | // Wait torrent generation | ||
432 | const expectedStatus = this.buildExpectedStatus({ ...options, defaultExpectedStatus: HttpStatusCode.OK_200 }) | ||
433 | if (expectedStatus === HttpStatusCode.OK_200 && waitTorrentGeneration) { | ||
434 | let video: VideoDetails | ||
435 | |||
436 | do { | ||
437 | video = await this.getWithToken({ ...options, id: created.uuid }) | ||
438 | |||
439 | await wait(50) | ||
440 | } while (!video.files[0].torrentUrl) | ||
441 | } | ||
442 | |||
443 | return created | ||
444 | } | ||
445 | |||
446 | async buildLegacyUpload (options: OverrideCommandOptions & { | ||
447 | attributes: VideoEdit | ||
448 | }): Promise<VideoCreateResult> { | ||
449 | const path = '/api/v1/videos/upload' | ||
450 | |||
451 | return unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({ | ||
452 | ...options, | ||
453 | |||
454 | path, | ||
455 | fields: this.buildUploadFields(options.attributes), | ||
456 | attaches: this.buildUploadAttaches(options.attributes), | ||
457 | implicitToken: true, | ||
458 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
459 | })).then(body => body.video || body as any) | ||
460 | } | ||
461 | |||
462 | async buildResumeUpload (options: OverrideCommandOptions & { | ||
463 | path: string | ||
464 | attributes: { fixture?: string } & { [id: string]: any } | ||
465 | completedExpectedStatus?: HttpStatusCode // When the upload is finished | ||
466 | }): Promise<VideoCreateResult> { | ||
467 | const { path, attributes, expectedStatus = HttpStatusCode.OK_200, completedExpectedStatus } = options | ||
468 | |||
469 | let size = 0 | ||
470 | let videoFilePath: string | ||
471 | let mimetype = 'video/mp4' | ||
472 | |||
473 | if (attributes.fixture) { | ||
474 | videoFilePath = buildAbsoluteFixturePath(attributes.fixture) | ||
475 | size = (await stat(videoFilePath)).size | ||
476 | |||
477 | if (videoFilePath.endsWith('.mkv')) { | ||
478 | mimetype = 'video/x-matroska' | ||
479 | } else if (videoFilePath.endsWith('.webm')) { | ||
480 | mimetype = 'video/webm' | ||
481 | } | ||
482 | } | ||
483 | |||
484 | // Do not check status automatically, we'll check it manually | ||
485 | const initializeSessionRes = await this.prepareResumableUpload({ | ||
486 | ...options, | ||
487 | |||
488 | path, | ||
489 | expectedStatus: null, | ||
490 | attributes, | ||
491 | size, | ||
492 | mimetype | ||
493 | }) | ||
494 | const initStatus = initializeSessionRes.status | ||
495 | |||
496 | if (videoFilePath && initStatus === HttpStatusCode.CREATED_201) { | ||
497 | const locationHeader = initializeSessionRes.header['location'] | ||
498 | expect(locationHeader).to.not.be.undefined | ||
499 | |||
500 | const pathUploadId = locationHeader.split('?')[1] | ||
501 | |||
502 | const result = await this.sendResumableChunks({ | ||
503 | ...options, | ||
504 | |||
505 | path, | ||
506 | pathUploadId, | ||
507 | videoFilePath, | ||
508 | size, | ||
509 | expectedStatus: completedExpectedStatus | ||
510 | }) | ||
511 | |||
512 | if (result.statusCode === HttpStatusCode.OK_200) { | ||
513 | await this.endResumableUpload({ | ||
514 | ...options, | ||
515 | |||
516 | expectedStatus: HttpStatusCode.NO_CONTENT_204, | ||
517 | path, | ||
518 | pathUploadId | ||
519 | }) | ||
520 | } | ||
521 | |||
522 | return result.body?.video || result.body as any | ||
523 | } | ||
524 | |||
525 | const expectedInitStatus = expectedStatus === HttpStatusCode.OK_200 | ||
526 | ? HttpStatusCode.CREATED_201 | ||
527 | : expectedStatus | ||
528 | |||
529 | expect(initStatus).to.equal(expectedInitStatus) | ||
530 | |||
531 | return initializeSessionRes.body.video || initializeSessionRes.body | ||
532 | } | ||
533 | |||
534 | async prepareResumableUpload (options: OverrideCommandOptions & { | ||
535 | path: string | ||
536 | attributes: { fixture?: string } & { [id: string]: any } | ||
537 | size: number | ||
538 | mimetype: string | ||
539 | |||
540 | originalName?: string | ||
541 | lastModified?: number | ||
542 | }) { | ||
543 | const { path, attributes, originalName, lastModified, size, mimetype } = options | ||
544 | |||
545 | const attaches = this.buildUploadAttaches(omit(options.attributes, [ 'fixture' ])) | ||
546 | |||
547 | const uploadOptions = { | ||
548 | ...options, | ||
549 | |||
550 | path, | ||
551 | headers: { | ||
552 | 'X-Upload-Content-Type': mimetype, | ||
553 | 'X-Upload-Content-Length': size.toString() | ||
554 | }, | ||
555 | fields: { | ||
556 | filename: attributes.fixture, | ||
557 | originalName, | ||
558 | lastModified, | ||
559 | |||
560 | ...this.buildUploadFields(options.attributes) | ||
561 | }, | ||
562 | |||
563 | // Fixture will be sent later | ||
564 | attaches: this.buildUploadAttaches(omit(options.attributes, [ 'fixture' ])), | ||
565 | implicitToken: true, | ||
566 | |||
567 | defaultExpectedStatus: null | ||
568 | } | ||
569 | |||
570 | if (Object.keys(attaches).length === 0) return this.postBodyRequest(uploadOptions) | ||
571 | |||
572 | return this.postUploadRequest(uploadOptions) | ||
573 | } | ||
574 | |||
575 | sendResumableChunks (options: OverrideCommandOptions & { | ||
576 | pathUploadId: string | ||
577 | path: string | ||
578 | videoFilePath: string | ||
579 | size: number | ||
580 | contentLength?: number | ||
581 | contentRangeBuilder?: (start: number, chunk: any) => string | ||
582 | digestBuilder?: (chunk: any) => string | ||
583 | }) { | ||
584 | const { | ||
585 | path, | ||
586 | pathUploadId, | ||
587 | videoFilePath, | ||
588 | size, | ||
589 | contentLength, | ||
590 | contentRangeBuilder, | ||
591 | digestBuilder, | ||
592 | expectedStatus = HttpStatusCode.OK_200 | ||
593 | } = options | ||
594 | |||
595 | let start = 0 | ||
596 | |||
597 | const token = this.buildCommonRequestToken({ ...options, implicitToken: true }) | ||
598 | const url = this.server.url | ||
599 | |||
600 | const readable = createReadStream(videoFilePath, { highWaterMark: 8 * 1024 }) | ||
601 | return new Promise<GotResponse<{ video: VideoCreateResult }>>((resolve, reject) => { | ||
602 | readable.on('data', async function onData (chunk) { | ||
603 | try { | ||
604 | readable.pause() | ||
605 | |||
606 | const byterangeStart = start + chunk.length - 1 | ||
607 | |||
608 | const headers = { | ||
609 | 'Authorization': 'Bearer ' + token, | ||
610 | 'Content-Type': 'application/octet-stream', | ||
611 | 'Content-Range': contentRangeBuilder | ||
612 | ? contentRangeBuilder(start, chunk) | ||
613 | : `bytes ${start}-${byterangeStart}/${size}`, | ||
614 | 'Content-Length': contentLength ? contentLength + '' : chunk.length + '' | ||
615 | } | ||
616 | |||
617 | if (digestBuilder) { | ||
618 | Object.assign(headers, { digest: digestBuilder(chunk) }) | ||
619 | } | ||
620 | |||
621 | const res = await got<{ video: VideoCreateResult }>({ | ||
622 | url, | ||
623 | method: 'put', | ||
624 | headers, | ||
625 | path: path + '?' + pathUploadId, | ||
626 | body: chunk, | ||
627 | responseType: 'json', | ||
628 | throwHttpErrors: false | ||
629 | }) | ||
630 | |||
631 | start += chunk.length | ||
632 | |||
633 | // Last request, check final status | ||
634 | if (byterangeStart + 1 === size) { | ||
635 | if (res.statusCode === expectedStatus) { | ||
636 | return resolve(res) | ||
637 | } | ||
638 | |||
639 | if (res.statusCode !== HttpStatusCode.PERMANENT_REDIRECT_308) { | ||
640 | readable.off('data', onData) | ||
641 | |||
642 | // eslint-disable-next-line max-len | ||
643 | const message = `Incorrect transient behaviour sending intermediary chunks. Status code is ${res.statusCode} instead of ${expectedStatus}` | ||
644 | return reject(new Error(message)) | ||
645 | } | ||
646 | } | ||
647 | |||
648 | readable.resume() | ||
649 | } catch (err) { | ||
650 | reject(err) | ||
651 | } | ||
652 | }) | ||
653 | }) | ||
654 | } | ||
655 | |||
656 | endResumableUpload (options: OverrideCommandOptions & { | ||
657 | path: string | ||
658 | pathUploadId: string | ||
659 | }) { | ||
660 | return this.deleteRequest({ | ||
661 | ...options, | ||
662 | |||
663 | path: options.path, | ||
664 | rawQuery: options.pathUploadId, | ||
665 | implicitToken: true, | ||
666 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
667 | }) | ||
668 | } | ||
669 | |||
670 | quickUpload (options: OverrideCommandOptions & { | ||
671 | name: string | ||
672 | nsfw?: boolean | ||
673 | privacy?: VideoPrivacy | ||
674 | fixture?: string | ||
675 | videoPasswords?: string[] | ||
676 | }) { | ||
677 | const attributes: VideoEdit = { name: options.name } | ||
678 | if (options.nsfw) attributes.nsfw = options.nsfw | ||
679 | if (options.privacy) attributes.privacy = options.privacy | ||
680 | if (options.fixture) attributes.fixture = options.fixture | ||
681 | if (options.videoPasswords) attributes.videoPasswords = options.videoPasswords | ||
682 | |||
683 | return this.upload({ ...options, attributes }) | ||
684 | } | ||
685 | |||
686 | async randomUpload (options: OverrideCommandOptions & { | ||
687 | wait?: boolean // default true | ||
688 | additionalParams?: VideoEdit & { prefixName?: string } | ||
689 | } = {}) { | ||
690 | const { wait = true, additionalParams } = options | ||
691 | const prefixName = additionalParams?.prefixName || '' | ||
692 | const name = prefixName + buildUUID() | ||
693 | |||
694 | const attributes = { name, ...additionalParams } | ||
695 | |||
696 | const result = await this.upload({ ...options, attributes }) | ||
697 | |||
698 | if (wait) await waitJobs([ this.server ]) | ||
699 | |||
700 | return { ...result, name } | ||
701 | } | ||
702 | |||
703 | // --------------------------------------------------------------------------- | ||
704 | |||
705 | replaceSourceFile (options: OverrideCommandOptions & { | ||
706 | videoId: number | string | ||
707 | fixture: string | ||
708 | completedExpectedStatus?: HttpStatusCode | ||
709 | }) { | ||
710 | return this.buildResumeUpload({ | ||
711 | ...options, | ||
712 | |||
713 | path: '/api/v1/videos/' + options.videoId + '/source/replace-resumable', | ||
714 | attributes: { fixture: options.fixture } | ||
715 | }) | ||
716 | } | ||
717 | |||
718 | // --------------------------------------------------------------------------- | ||
719 | |||
720 | removeHLSPlaylist (options: OverrideCommandOptions & { | ||
721 | videoId: number | string | ||
722 | }) { | ||
723 | const path = '/api/v1/videos/' + options.videoId + '/hls' | ||
724 | |||
725 | return this.deleteRequest({ | ||
726 | ...options, | ||
727 | |||
728 | path, | ||
729 | implicitToken: true, | ||
730 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
731 | }) | ||
732 | } | ||
733 | |||
734 | removeHLSFile (options: OverrideCommandOptions & { | ||
735 | videoId: number | string | ||
736 | fileId: number | ||
737 | }) { | ||
738 | const path = '/api/v1/videos/' + options.videoId + '/hls/' + options.fileId | ||
739 | |||
740 | return this.deleteRequest({ | ||
741 | ...options, | ||
742 | |||
743 | path, | ||
744 | implicitToken: true, | ||
745 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
746 | }) | ||
747 | } | ||
748 | |||
749 | removeAllWebVideoFiles (options: OverrideCommandOptions & { | ||
750 | videoId: number | string | ||
751 | }) { | ||
752 | const path = '/api/v1/videos/' + options.videoId + '/web-videos' | ||
753 | |||
754 | return this.deleteRequest({ | ||
755 | ...options, | ||
756 | |||
757 | path, | ||
758 | implicitToken: true, | ||
759 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
760 | }) | ||
761 | } | ||
762 | |||
763 | removeWebVideoFile (options: OverrideCommandOptions & { | ||
764 | videoId: number | string | ||
765 | fileId: number | ||
766 | }) { | ||
767 | const path = '/api/v1/videos/' + options.videoId + '/web-videos/' + options.fileId | ||
768 | |||
769 | return this.deleteRequest({ | ||
770 | ...options, | ||
771 | |||
772 | path, | ||
773 | implicitToken: true, | ||
774 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
775 | }) | ||
776 | } | ||
777 | |||
778 | runTranscoding (options: OverrideCommandOptions & VideoTranscodingCreate & { | ||
779 | videoId: number | string | ||
780 | }) { | ||
781 | const path = '/api/v1/videos/' + options.videoId + '/transcoding' | ||
782 | |||
783 | return this.postBodyRequest({ | ||
784 | ...options, | ||
785 | |||
786 | path, | ||
787 | fields: pick(options, [ 'transcodingType', 'forceTranscoding' ]), | ||
788 | implicitToken: true, | ||
789 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
790 | }) | ||
791 | } | ||
792 | |||
793 | // --------------------------------------------------------------------------- | ||
794 | |||
795 | private buildListQuery (options: VideosCommonQuery) { | ||
796 | return pick(options, [ | ||
797 | 'start', | ||
798 | 'count', | ||
799 | 'sort', | ||
800 | 'nsfw', | ||
801 | 'isLive', | ||
802 | 'categoryOneOf', | ||
803 | 'licenceOneOf', | ||
804 | 'languageOneOf', | ||
805 | 'privacyOneOf', | ||
806 | 'tagsOneOf', | ||
807 | 'tagsAllOf', | ||
808 | 'isLocal', | ||
809 | 'include', | ||
810 | 'skipCount' | ||
811 | ]) | ||
812 | } | ||
813 | |||
814 | private buildUploadFields (attributes: VideoEdit) { | ||
815 | return omit(attributes, [ 'fixture', 'thumbnailfile', 'previewfile' ]) | ||
816 | } | ||
817 | |||
818 | private buildUploadAttaches (attributes: VideoEdit) { | ||
819 | const attaches: { [ name: string ]: string } = {} | ||
820 | |||
821 | for (const key of [ 'thumbnailfile', 'previewfile' ]) { | ||
822 | if (attributes[key]) attaches[key] = buildAbsoluteFixturePath(attributes[key]) | ||
823 | } | ||
824 | |||
825 | if (attributes.fixture) attaches.videofile = buildAbsoluteFixturePath(attributes.fixture) | ||
826 | |||
827 | return attaches | ||
828 | } | ||
829 | } | ||
diff --git a/shared/server-commands/videos/views-command.ts b/shared/server-commands/videos/views-command.ts deleted file mode 100644 index bdb8daaa4..000000000 --- a/shared/server-commands/videos/views-command.ts +++ /dev/null | |||
@@ -1,51 +0,0 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ | ||
2 | import { HttpStatusCode, VideoViewEvent } from '@shared/models' | ||
3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
4 | |||
5 | export class ViewsCommand extends AbstractCommand { | ||
6 | |||
7 | view (options: OverrideCommandOptions & { | ||
8 | id: number | string | ||
9 | currentTime: number | ||
10 | viewEvent?: VideoViewEvent | ||
11 | xForwardedFor?: string | ||
12 | }) { | ||
13 | const { id, xForwardedFor, viewEvent, currentTime } = options | ||
14 | const path = '/api/v1/videos/' + id + '/views' | ||
15 | |||
16 | return this.postBodyRequest({ | ||
17 | ...options, | ||
18 | |||
19 | path, | ||
20 | xForwardedFor, | ||
21 | fields: { | ||
22 | currentTime, | ||
23 | viewEvent | ||
24 | }, | ||
25 | implicitToken: false, | ||
26 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
27 | }) | ||
28 | } | ||
29 | |||
30 | async simulateView (options: OverrideCommandOptions & { | ||
31 | id: number | string | ||
32 | xForwardedFor?: string | ||
33 | }) { | ||
34 | await this.view({ ...options, currentTime: 0 }) | ||
35 | await this.view({ ...options, currentTime: 5 }) | ||
36 | } | ||
37 | |||
38 | async simulateViewer (options: OverrideCommandOptions & { | ||
39 | id: number | string | ||
40 | currentTimes: number[] | ||
41 | xForwardedFor?: string | ||
42 | }) { | ||
43 | let viewEvent: VideoViewEvent = 'seek' | ||
44 | |||
45 | for (const currentTime of options.currentTimes) { | ||
46 | await this.view({ ...options, currentTime, viewEvent }) | ||
47 | |||
48 | viewEvent = undefined | ||
49 | } | ||
50 | } | ||
51 | } | ||