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 /packages/server-commands/src/videos/videos-command.ts | |
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 'packages/server-commands/src/videos/videos-command.ts')
-rw-r--r-- | packages/server-commands/src/videos/videos-command.ts | 831 |
1 files changed, 831 insertions, 0 deletions
diff --git a/packages/server-commands/src/videos/videos-command.ts b/packages/server-commands/src/videos/videos-command.ts new file mode 100644 index 000000000..72dc58a4b --- /dev/null +++ b/packages/server-commands/src/videos/videos-command.ts | |||
@@ -0,0 +1,831 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ | ||
2 | |||
3 | import { expect } from 'chai' | ||
4 | import { createReadStream } from 'fs' | ||
5 | import { stat } from 'fs/promises' | ||
6 | import got, { Response as GotResponse } from 'got' | ||
7 | import validator from 'validator' | ||
8 | import { getAllPrivacies, omit, pick, wait } from '@peertube/peertube-core-utils' | ||
9 | import { | ||
10 | HttpStatusCode, | ||
11 | HttpStatusCodeType, | ||
12 | ResultList, | ||
13 | UserVideoRateType, | ||
14 | Video, | ||
15 | VideoCreate, | ||
16 | VideoCreateResult, | ||
17 | VideoDetails, | ||
18 | VideoFileMetadata, | ||
19 | VideoInclude, | ||
20 | VideoPrivacy, | ||
21 | VideoPrivacyType, | ||
22 | VideosCommonQuery, | ||
23 | VideoSource, | ||
24 | VideoTranscodingCreate | ||
25 | } from '@peertube/peertube-models' | ||
26 | import { buildAbsoluteFixturePath, buildUUID } from '@peertube/peertube-node-utils' | ||
27 | import { unwrapBody } from '../requests/index.js' | ||
28 | import { waitJobs } from '../server/jobs.js' | ||
29 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
30 | |||
31 | export type VideoEdit = Partial<Omit<VideoCreate, 'thumbnailfile' | 'previewfile'>> & { | ||
32 | fixture?: string | ||
33 | thumbnailfile?: string | ||
34 | previewfile?: string | ||
35 | } | ||
36 | |||
37 | export class VideosCommand extends AbstractCommand { | ||
38 | |||
39 | getCategories (options: OverrideCommandOptions = {}) { | ||
40 | const path = '/api/v1/videos/categories' | ||
41 | |||
42 | return this.getRequestBody<{ [id: number]: string }>({ | ||
43 | ...options, | ||
44 | path, | ||
45 | |||
46 | implicitToken: false, | ||
47 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
48 | }) | ||
49 | } | ||
50 | |||
51 | getLicences (options: OverrideCommandOptions = {}) { | ||
52 | const path = '/api/v1/videos/licences' | ||
53 | |||
54 | return this.getRequestBody<{ [id: number]: string }>({ | ||
55 | ...options, | ||
56 | path, | ||
57 | |||
58 | implicitToken: false, | ||
59 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
60 | }) | ||
61 | } | ||
62 | |||
63 | getLanguages (options: OverrideCommandOptions = {}) { | ||
64 | const path = '/api/v1/videos/languages' | ||
65 | |||
66 | return this.getRequestBody<{ [id: string]: string }>({ | ||
67 | ...options, | ||
68 | path, | ||
69 | |||
70 | implicitToken: false, | ||
71 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
72 | }) | ||
73 | } | ||
74 | |||
75 | getPrivacies (options: OverrideCommandOptions = {}) { | ||
76 | const path = '/api/v1/videos/privacies' | ||
77 | |||
78 | return this.getRequestBody<{ [id in VideoPrivacyType]: string }>({ | ||
79 | ...options, | ||
80 | path, | ||
81 | |||
82 | implicitToken: false, | ||
83 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
84 | }) | ||
85 | } | ||
86 | |||
87 | // --------------------------------------------------------------------------- | ||
88 | |||
89 | getDescription (options: OverrideCommandOptions & { | ||
90 | descriptionPath: string | ||
91 | }) { | ||
92 | return this.getRequestBody<{ description: string }>({ | ||
93 | ...options, | ||
94 | path: options.descriptionPath, | ||
95 | |||
96 | implicitToken: false, | ||
97 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
98 | }) | ||
99 | } | ||
100 | |||
101 | getFileMetadata (options: OverrideCommandOptions & { | ||
102 | url: string | ||
103 | }) { | ||
104 | return unwrapBody<VideoFileMetadata>(this.getRawRequest({ | ||
105 | ...options, | ||
106 | |||
107 | url: options.url, | ||
108 | implicitToken: false, | ||
109 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
110 | })) | ||
111 | } | ||
112 | |||
113 | // --------------------------------------------------------------------------- | ||
114 | |||
115 | rate (options: OverrideCommandOptions & { | ||
116 | id: number | string | ||
117 | rating: UserVideoRateType | ||
118 | videoPassword?: string | ||
119 | }) { | ||
120 | const { id, rating, videoPassword } = options | ||
121 | const path = '/api/v1/videos/' + id + '/rate' | ||
122 | |||
123 | return this.putBodyRequest({ | ||
124 | ...options, | ||
125 | |||
126 | path, | ||
127 | fields: { rating }, | ||
128 | headers: this.buildVideoPasswordHeader(videoPassword), | ||
129 | implicitToken: true, | ||
130 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
131 | }) | ||
132 | } | ||
133 | |||
134 | // --------------------------------------------------------------------------- | ||
135 | |||
136 | get (options: OverrideCommandOptions & { | ||
137 | id: number | string | ||
138 | }) { | ||
139 | const path = '/api/v1/videos/' + options.id | ||
140 | |||
141 | return this.getRequestBody<VideoDetails>({ | ||
142 | ...options, | ||
143 | |||
144 | path, | ||
145 | implicitToken: false, | ||
146 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
147 | }) | ||
148 | } | ||
149 | |||
150 | getWithToken (options: OverrideCommandOptions & { | ||
151 | id: number | string | ||
152 | }) { | ||
153 | return this.get({ | ||
154 | ...options, | ||
155 | |||
156 | token: this.buildCommonRequestToken({ ...options, implicitToken: true }) | ||
157 | }) | ||
158 | } | ||
159 | |||
160 | getWithPassword (options: OverrideCommandOptions & { | ||
161 | id: number | string | ||
162 | password?: string | ||
163 | }) { | ||
164 | const path = '/api/v1/videos/' + options.id | ||
165 | |||
166 | return this.getRequestBody<VideoDetails>({ | ||
167 | ...options, | ||
168 | headers:{ | ||
169 | 'x-peertube-video-password': options.password | ||
170 | }, | ||
171 | path, | ||
172 | implicitToken: false, | ||
173 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
174 | }) | ||
175 | } | ||
176 | |||
177 | getSource (options: OverrideCommandOptions & { | ||
178 | id: number | string | ||
179 | }) { | ||
180 | const path = '/api/v1/videos/' + options.id + '/source' | ||
181 | |||
182 | return this.getRequestBody<VideoSource>({ | ||
183 | ...options, | ||
184 | |||
185 | path, | ||
186 | implicitToken: true, | ||
187 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
188 | }) | ||
189 | } | ||
190 | |||
191 | async getId (options: OverrideCommandOptions & { | ||
192 | uuid: number | string | ||
193 | }) { | ||
194 | const { uuid } = options | ||
195 | |||
196 | if (validator.default.isUUID('' + uuid) === false) return uuid as number | ||
197 | |||
198 | const { id } = await this.get({ ...options, id: uuid }) | ||
199 | |||
200 | return id | ||
201 | } | ||
202 | |||
203 | async listFiles (options: OverrideCommandOptions & { | ||
204 | id: number | string | ||
205 | }) { | ||
206 | const video = await this.get(options) | ||
207 | |||
208 | const files = video.files || [] | ||
209 | const hlsFiles = video.streamingPlaylists[0]?.files || [] | ||
210 | |||
211 | return files.concat(hlsFiles) | ||
212 | } | ||
213 | |||
214 | // --------------------------------------------------------------------------- | ||
215 | |||
216 | listMyVideos (options: OverrideCommandOptions & { | ||
217 | start?: number | ||
218 | count?: number | ||
219 | sort?: string | ||
220 | search?: string | ||
221 | isLive?: boolean | ||
222 | channelId?: number | ||
223 | } = {}) { | ||
224 | const path = '/api/v1/users/me/videos' | ||
225 | |||
226 | return this.getRequestBody<ResultList<Video>>({ | ||
227 | ...options, | ||
228 | |||
229 | path, | ||
230 | query: pick(options, [ 'start', 'count', 'sort', 'search', 'isLive', 'channelId' ]), | ||
231 | implicitToken: true, | ||
232 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
233 | }) | ||
234 | } | ||
235 | |||
236 | listMySubscriptionVideos (options: OverrideCommandOptions & VideosCommonQuery = {}) { | ||
237 | const { sort = '-createdAt' } = options | ||
238 | const path = '/api/v1/users/me/subscriptions/videos' | ||
239 | |||
240 | return this.getRequestBody<ResultList<Video>>({ | ||
241 | ...options, | ||
242 | |||
243 | path, | ||
244 | query: { sort, ...this.buildListQuery(options) }, | ||
245 | implicitToken: true, | ||
246 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
247 | }) | ||
248 | } | ||
249 | |||
250 | // --------------------------------------------------------------------------- | ||
251 | |||
252 | list (options: OverrideCommandOptions & VideosCommonQuery = {}) { | ||
253 | const path = '/api/v1/videos' | ||
254 | |||
255 | const query = this.buildListQuery(options) | ||
256 | |||
257 | return this.getRequestBody<ResultList<Video>>({ | ||
258 | ...options, | ||
259 | |||
260 | path, | ||
261 | query: { sort: 'name', ...query }, | ||
262 | implicitToken: false, | ||
263 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
264 | }) | ||
265 | } | ||
266 | |||
267 | listWithToken (options: OverrideCommandOptions & VideosCommonQuery = {}) { | ||
268 | return this.list({ | ||
269 | ...options, | ||
270 | |||
271 | token: this.buildCommonRequestToken({ ...options, implicitToken: true }) | ||
272 | }) | ||
273 | } | ||
274 | |||
275 | listAllForAdmin (options: OverrideCommandOptions & VideosCommonQuery = {}) { | ||
276 | const include = VideoInclude.NOT_PUBLISHED_STATE | VideoInclude.BLACKLISTED | VideoInclude.BLOCKED_OWNER | ||
277 | const nsfw = 'both' | ||
278 | const privacyOneOf = getAllPrivacies() | ||
279 | |||
280 | return this.list({ | ||
281 | ...options, | ||
282 | |||
283 | include, | ||
284 | nsfw, | ||
285 | privacyOneOf, | ||
286 | |||
287 | token: this.buildCommonRequestToken({ ...options, implicitToken: true }) | ||
288 | }) | ||
289 | } | ||
290 | |||
291 | listByAccount (options: OverrideCommandOptions & VideosCommonQuery & { | ||
292 | handle: string | ||
293 | }) { | ||
294 | const { handle, search } = options | ||
295 | const path = '/api/v1/accounts/' + handle + '/videos' | ||
296 | |||
297 | return this.getRequestBody<ResultList<Video>>({ | ||
298 | ...options, | ||
299 | |||
300 | path, | ||
301 | query: { search, ...this.buildListQuery(options) }, | ||
302 | implicitToken: true, | ||
303 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
304 | }) | ||
305 | } | ||
306 | |||
307 | listByChannel (options: OverrideCommandOptions & VideosCommonQuery & { | ||
308 | handle: string | ||
309 | }) { | ||
310 | const { handle } = options | ||
311 | const path = '/api/v1/video-channels/' + handle + '/videos' | ||
312 | |||
313 | return this.getRequestBody<ResultList<Video>>({ | ||
314 | ...options, | ||
315 | |||
316 | path, | ||
317 | query: this.buildListQuery(options), | ||
318 | implicitToken: true, | ||
319 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
320 | }) | ||
321 | } | ||
322 | |||
323 | // --------------------------------------------------------------------------- | ||
324 | |||
325 | async find (options: OverrideCommandOptions & { | ||
326 | name: string | ||
327 | }) { | ||
328 | const { data } = await this.list(options) | ||
329 | |||
330 | return data.find(v => v.name === options.name) | ||
331 | } | ||
332 | |||
333 | // --------------------------------------------------------------------------- | ||
334 | |||
335 | update (options: OverrideCommandOptions & { | ||
336 | id: number | string | ||
337 | attributes?: VideoEdit | ||
338 | }) { | ||
339 | const { id, attributes = {} } = options | ||
340 | const path = '/api/v1/videos/' + id | ||
341 | |||
342 | // Upload request | ||
343 | if (attributes.thumbnailfile || attributes.previewfile) { | ||
344 | const attaches: any = {} | ||
345 | if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile | ||
346 | if (attributes.previewfile) attaches.previewfile = attributes.previewfile | ||
347 | |||
348 | return this.putUploadRequest({ | ||
349 | ...options, | ||
350 | |||
351 | path, | ||
352 | fields: options.attributes, | ||
353 | attaches: { | ||
354 | thumbnailfile: attributes.thumbnailfile, | ||
355 | previewfile: attributes.previewfile | ||
356 | }, | ||
357 | implicitToken: true, | ||
358 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
359 | }) | ||
360 | } | ||
361 | |||
362 | return this.putBodyRequest({ | ||
363 | ...options, | ||
364 | |||
365 | path, | ||
366 | fields: options.attributes, | ||
367 | implicitToken: true, | ||
368 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
369 | }) | ||
370 | } | ||
371 | |||
372 | remove (options: OverrideCommandOptions & { | ||
373 | id: number | string | ||
374 | }) { | ||
375 | const path = '/api/v1/videos/' + options.id | ||
376 | |||
377 | return unwrapBody(this.deleteRequest({ | ||
378 | ...options, | ||
379 | |||
380 | path, | ||
381 | implicitToken: true, | ||
382 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
383 | })) | ||
384 | } | ||
385 | |||
386 | async removeAll () { | ||
387 | const { data } = await this.list() | ||
388 | |||
389 | for (const v of data) { | ||
390 | await this.remove({ id: v.id }) | ||
391 | } | ||
392 | } | ||
393 | |||
394 | // --------------------------------------------------------------------------- | ||
395 | |||
396 | async upload (options: OverrideCommandOptions & { | ||
397 | attributes?: VideoEdit | ||
398 | mode?: 'legacy' | 'resumable' // default legacy | ||
399 | waitTorrentGeneration?: boolean // default true | ||
400 | completedExpectedStatus?: HttpStatusCodeType | ||
401 | } = {}) { | ||
402 | const { mode = 'legacy', waitTorrentGeneration = true } = options | ||
403 | let defaultChannelId = 1 | ||
404 | |||
405 | try { | ||
406 | const { videoChannels } = await this.server.users.getMyInfo({ token: options.token }) | ||
407 | defaultChannelId = videoChannels[0].id | ||
408 | } catch (e) { /* empty */ } | ||
409 | |||
410 | // Override default attributes | ||
411 | const attributes = { | ||
412 | name: 'my super video', | ||
413 | category: 5, | ||
414 | licence: 4, | ||
415 | language: 'zh', | ||
416 | channelId: defaultChannelId, | ||
417 | nsfw: true, | ||
418 | waitTranscoding: false, | ||
419 | description: 'my super description', | ||
420 | support: 'my super support text', | ||
421 | tags: [ 'tag' ], | ||
422 | privacy: VideoPrivacy.PUBLIC, | ||
423 | commentsEnabled: true, | ||
424 | downloadEnabled: true, | ||
425 | fixture: 'video_short.webm', | ||
426 | |||
427 | ...options.attributes | ||
428 | } | ||
429 | |||
430 | const created = mode === 'legacy' | ||
431 | ? await this.buildLegacyUpload({ ...options, attributes }) | ||
432 | : await this.buildResumeUpload({ ...options, path: '/api/v1/videos/upload-resumable', attributes }) | ||
433 | |||
434 | // Wait torrent generation | ||
435 | const expectedStatus = this.buildExpectedStatus({ ...options, defaultExpectedStatus: HttpStatusCode.OK_200 }) | ||
436 | if (expectedStatus === HttpStatusCode.OK_200 && waitTorrentGeneration) { | ||
437 | let video: VideoDetails | ||
438 | |||
439 | do { | ||
440 | video = await this.getWithToken({ ...options, id: created.uuid }) | ||
441 | |||
442 | await wait(50) | ||
443 | } while (!video.files[0].torrentUrl) | ||
444 | } | ||
445 | |||
446 | return created | ||
447 | } | ||
448 | |||
449 | async buildLegacyUpload (options: OverrideCommandOptions & { | ||
450 | attributes: VideoEdit | ||
451 | }): Promise<VideoCreateResult> { | ||
452 | const path = '/api/v1/videos/upload' | ||
453 | |||
454 | return unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({ | ||
455 | ...options, | ||
456 | |||
457 | path, | ||
458 | fields: this.buildUploadFields(options.attributes), | ||
459 | attaches: this.buildUploadAttaches(options.attributes), | ||
460 | implicitToken: true, | ||
461 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
462 | })).then(body => body.video || body as any) | ||
463 | } | ||
464 | |||
465 | async buildResumeUpload (options: OverrideCommandOptions & { | ||
466 | path: string | ||
467 | attributes: { fixture?: string } & { [id: string]: any } | ||
468 | completedExpectedStatus?: HttpStatusCodeType // When the upload is finished | ||
469 | }): Promise<VideoCreateResult> { | ||
470 | const { path, attributes, expectedStatus = HttpStatusCode.OK_200, completedExpectedStatus } = options | ||
471 | |||
472 | let size = 0 | ||
473 | let videoFilePath: string | ||
474 | let mimetype = 'video/mp4' | ||
475 | |||
476 | if (attributes.fixture) { | ||
477 | videoFilePath = buildAbsoluteFixturePath(attributes.fixture) | ||
478 | size = (await stat(videoFilePath)).size | ||
479 | |||
480 | if (videoFilePath.endsWith('.mkv')) { | ||
481 | mimetype = 'video/x-matroska' | ||
482 | } else if (videoFilePath.endsWith('.webm')) { | ||
483 | mimetype = 'video/webm' | ||
484 | } | ||
485 | } | ||
486 | |||
487 | // Do not check status automatically, we'll check it manually | ||
488 | const initializeSessionRes = await this.prepareResumableUpload({ | ||
489 | ...options, | ||
490 | |||
491 | path, | ||
492 | expectedStatus: null, | ||
493 | attributes, | ||
494 | size, | ||
495 | mimetype | ||
496 | }) | ||
497 | const initStatus = initializeSessionRes.status | ||
498 | |||
499 | if (videoFilePath && initStatus === HttpStatusCode.CREATED_201) { | ||
500 | const locationHeader = initializeSessionRes.header['location'] | ||
501 | expect(locationHeader).to.not.be.undefined | ||
502 | |||
503 | const pathUploadId = locationHeader.split('?')[1] | ||
504 | |||
505 | const result = await this.sendResumableChunks({ | ||
506 | ...options, | ||
507 | |||
508 | path, | ||
509 | pathUploadId, | ||
510 | videoFilePath, | ||
511 | size, | ||
512 | expectedStatus: completedExpectedStatus | ||
513 | }) | ||
514 | |||
515 | if (result.statusCode === HttpStatusCode.OK_200) { | ||
516 | await this.endResumableUpload({ | ||
517 | ...options, | ||
518 | |||
519 | expectedStatus: HttpStatusCode.NO_CONTENT_204, | ||
520 | path, | ||
521 | pathUploadId | ||
522 | }) | ||
523 | } | ||
524 | |||
525 | return result.body?.video || result.body as any | ||
526 | } | ||
527 | |||
528 | const expectedInitStatus = expectedStatus === HttpStatusCode.OK_200 | ||
529 | ? HttpStatusCode.CREATED_201 | ||
530 | : expectedStatus | ||
531 | |||
532 | expect(initStatus).to.equal(expectedInitStatus) | ||
533 | |||
534 | return initializeSessionRes.body.video || initializeSessionRes.body | ||
535 | } | ||
536 | |||
537 | async prepareResumableUpload (options: OverrideCommandOptions & { | ||
538 | path: string | ||
539 | attributes: { fixture?: string } & { [id: string]: any } | ||
540 | size: number | ||
541 | mimetype: string | ||
542 | |||
543 | originalName?: string | ||
544 | lastModified?: number | ||
545 | }) { | ||
546 | const { path, attributes, originalName, lastModified, size, mimetype } = options | ||
547 | |||
548 | const attaches = this.buildUploadAttaches(omit(options.attributes, [ 'fixture' ])) | ||
549 | |||
550 | const uploadOptions = { | ||
551 | ...options, | ||
552 | |||
553 | path, | ||
554 | headers: { | ||
555 | 'X-Upload-Content-Type': mimetype, | ||
556 | 'X-Upload-Content-Length': size.toString() | ||
557 | }, | ||
558 | fields: { | ||
559 | filename: attributes.fixture, | ||
560 | originalName, | ||
561 | lastModified, | ||
562 | |||
563 | ...this.buildUploadFields(options.attributes) | ||
564 | }, | ||
565 | |||
566 | // Fixture will be sent later | ||
567 | attaches: this.buildUploadAttaches(omit(options.attributes, [ 'fixture' ])), | ||
568 | implicitToken: true, | ||
569 | |||
570 | defaultExpectedStatus: null | ||
571 | } | ||
572 | |||
573 | if (Object.keys(attaches).length === 0) return this.postBodyRequest(uploadOptions) | ||
574 | |||
575 | return this.postUploadRequest(uploadOptions) | ||
576 | } | ||
577 | |||
578 | sendResumableChunks (options: OverrideCommandOptions & { | ||
579 | pathUploadId: string | ||
580 | path: string | ||
581 | videoFilePath: string | ||
582 | size: number | ||
583 | contentLength?: number | ||
584 | contentRangeBuilder?: (start: number, chunk: any) => string | ||
585 | digestBuilder?: (chunk: any) => string | ||
586 | }) { | ||
587 | const { | ||
588 | path, | ||
589 | pathUploadId, | ||
590 | videoFilePath, | ||
591 | size, | ||
592 | contentLength, | ||
593 | contentRangeBuilder, | ||
594 | digestBuilder, | ||
595 | expectedStatus = HttpStatusCode.OK_200 | ||
596 | } = options | ||
597 | |||
598 | let start = 0 | ||
599 | |||
600 | const token = this.buildCommonRequestToken({ ...options, implicitToken: true }) | ||
601 | |||
602 | const readable = createReadStream(videoFilePath, { highWaterMark: 8 * 1024 }) | ||
603 | const server = this.server | ||
604 | return new Promise<GotResponse<{ video: VideoCreateResult }>>((resolve, reject) => { | ||
605 | readable.on('data', async function onData (chunk) { | ||
606 | try { | ||
607 | readable.pause() | ||
608 | |||
609 | const byterangeStart = start + chunk.length - 1 | ||
610 | |||
611 | const headers = { | ||
612 | 'Authorization': 'Bearer ' + token, | ||
613 | 'Content-Type': 'application/octet-stream', | ||
614 | 'Content-Range': contentRangeBuilder | ||
615 | ? contentRangeBuilder(start, chunk) | ||
616 | : `bytes ${start}-${byterangeStart}/${size}`, | ||
617 | 'Content-Length': contentLength ? contentLength + '' : chunk.length + '' | ||
618 | } | ||
619 | |||
620 | if (digestBuilder) { | ||
621 | Object.assign(headers, { digest: digestBuilder(chunk) }) | ||
622 | } | ||
623 | |||
624 | const res = await got<{ video: VideoCreateResult }>({ | ||
625 | url: new URL(path + '?' + pathUploadId, server.url).toString(), | ||
626 | method: 'put', | ||
627 | headers, | ||
628 | body: chunk, | ||
629 | responseType: 'json', | ||
630 | throwHttpErrors: false | ||
631 | }) | ||
632 | |||
633 | start += chunk.length | ||
634 | |||
635 | // Last request, check final status | ||
636 | if (byterangeStart + 1 === size) { | ||
637 | if (res.statusCode === expectedStatus) { | ||
638 | return resolve(res) | ||
639 | } | ||
640 | |||
641 | if (res.statusCode !== HttpStatusCode.PERMANENT_REDIRECT_308) { | ||
642 | readable.off('data', onData) | ||
643 | |||
644 | // eslint-disable-next-line max-len | ||
645 | const message = `Incorrect transient behaviour sending intermediary chunks. Status code is ${res.statusCode} instead of ${expectedStatus}` | ||
646 | return reject(new Error(message)) | ||
647 | } | ||
648 | } | ||
649 | |||
650 | readable.resume() | ||
651 | } catch (err) { | ||
652 | reject(err) | ||
653 | } | ||
654 | }) | ||
655 | }) | ||
656 | } | ||
657 | |||
658 | endResumableUpload (options: OverrideCommandOptions & { | ||
659 | path: string | ||
660 | pathUploadId: string | ||
661 | }) { | ||
662 | return this.deleteRequest({ | ||
663 | ...options, | ||
664 | |||
665 | path: options.path, | ||
666 | rawQuery: options.pathUploadId, | ||
667 | implicitToken: true, | ||
668 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
669 | }) | ||
670 | } | ||
671 | |||
672 | quickUpload (options: OverrideCommandOptions & { | ||
673 | name: string | ||
674 | nsfw?: boolean | ||
675 | privacy?: VideoPrivacyType | ||
676 | fixture?: string | ||
677 | videoPasswords?: string[] | ||
678 | }) { | ||
679 | const attributes: VideoEdit = { name: options.name } | ||
680 | if (options.nsfw) attributes.nsfw = options.nsfw | ||
681 | if (options.privacy) attributes.privacy = options.privacy | ||
682 | if (options.fixture) attributes.fixture = options.fixture | ||
683 | if (options.videoPasswords) attributes.videoPasswords = options.videoPasswords | ||
684 | |||
685 | return this.upload({ ...options, attributes }) | ||
686 | } | ||
687 | |||
688 | async randomUpload (options: OverrideCommandOptions & { | ||
689 | wait?: boolean // default true | ||
690 | additionalParams?: VideoEdit & { prefixName?: string } | ||
691 | } = {}) { | ||
692 | const { wait = true, additionalParams } = options | ||
693 | const prefixName = additionalParams?.prefixName || '' | ||
694 | const name = prefixName + buildUUID() | ||
695 | |||
696 | const attributes = { name, ...additionalParams } | ||
697 | |||
698 | const result = await this.upload({ ...options, attributes }) | ||
699 | |||
700 | if (wait) await waitJobs([ this.server ]) | ||
701 | |||
702 | return { ...result, name } | ||
703 | } | ||
704 | |||
705 | // --------------------------------------------------------------------------- | ||
706 | |||
707 | replaceSourceFile (options: OverrideCommandOptions & { | ||
708 | videoId: number | string | ||
709 | fixture: string | ||
710 | completedExpectedStatus?: HttpStatusCodeType | ||
711 | }) { | ||
712 | return this.buildResumeUpload({ | ||
713 | ...options, | ||
714 | |||
715 | path: '/api/v1/videos/' + options.videoId + '/source/replace-resumable', | ||
716 | attributes: { fixture: options.fixture } | ||
717 | }) | ||
718 | } | ||
719 | |||
720 | // --------------------------------------------------------------------------- | ||
721 | |||
722 | removeHLSPlaylist (options: OverrideCommandOptions & { | ||
723 | videoId: number | string | ||
724 | }) { | ||
725 | const path = '/api/v1/videos/' + options.videoId + '/hls' | ||
726 | |||
727 | return this.deleteRequest({ | ||
728 | ...options, | ||
729 | |||
730 | path, | ||
731 | implicitToken: true, | ||
732 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
733 | }) | ||
734 | } | ||
735 | |||
736 | removeHLSFile (options: OverrideCommandOptions & { | ||
737 | videoId: number | string | ||
738 | fileId: number | ||
739 | }) { | ||
740 | const path = '/api/v1/videos/' + options.videoId + '/hls/' + options.fileId | ||
741 | |||
742 | return this.deleteRequest({ | ||
743 | ...options, | ||
744 | |||
745 | path, | ||
746 | implicitToken: true, | ||
747 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
748 | }) | ||
749 | } | ||
750 | |||
751 | removeAllWebVideoFiles (options: OverrideCommandOptions & { | ||
752 | videoId: number | string | ||
753 | }) { | ||
754 | const path = '/api/v1/videos/' + options.videoId + '/web-videos' | ||
755 | |||
756 | return this.deleteRequest({ | ||
757 | ...options, | ||
758 | |||
759 | path, | ||
760 | implicitToken: true, | ||
761 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
762 | }) | ||
763 | } | ||
764 | |||
765 | removeWebVideoFile (options: OverrideCommandOptions & { | ||
766 | videoId: number | string | ||
767 | fileId: number | ||
768 | }) { | ||
769 | const path = '/api/v1/videos/' + options.videoId + '/web-videos/' + options.fileId | ||
770 | |||
771 | return this.deleteRequest({ | ||
772 | ...options, | ||
773 | |||
774 | path, | ||
775 | implicitToken: true, | ||
776 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
777 | }) | ||
778 | } | ||
779 | |||
780 | runTranscoding (options: OverrideCommandOptions & VideoTranscodingCreate & { | ||
781 | videoId: number | string | ||
782 | }) { | ||
783 | const path = '/api/v1/videos/' + options.videoId + '/transcoding' | ||
784 | |||
785 | return this.postBodyRequest({ | ||
786 | ...options, | ||
787 | |||
788 | path, | ||
789 | fields: pick(options, [ 'transcodingType', 'forceTranscoding' ]), | ||
790 | implicitToken: true, | ||
791 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
792 | }) | ||
793 | } | ||
794 | |||
795 | // --------------------------------------------------------------------------- | ||
796 | |||
797 | private buildListQuery (options: VideosCommonQuery) { | ||
798 | return pick(options, [ | ||
799 | 'start', | ||
800 | 'count', | ||
801 | 'sort', | ||
802 | 'nsfw', | ||
803 | 'isLive', | ||
804 | 'categoryOneOf', | ||
805 | 'licenceOneOf', | ||
806 | 'languageOneOf', | ||
807 | 'privacyOneOf', | ||
808 | 'tagsOneOf', | ||
809 | 'tagsAllOf', | ||
810 | 'isLocal', | ||
811 | 'include', | ||
812 | 'skipCount' | ||
813 | ]) | ||
814 | } | ||
815 | |||
816 | private buildUploadFields (attributes: VideoEdit) { | ||
817 | return omit(attributes, [ 'fixture', 'thumbnailfile', 'previewfile' ]) | ||
818 | } | ||
819 | |||
820 | private buildUploadAttaches (attributes: VideoEdit) { | ||
821 | const attaches: { [ name: string ]: string } = {} | ||
822 | |||
823 | for (const key of [ 'thumbnailfile', 'previewfile' ]) { | ||
824 | if (attributes[key]) attaches[key] = buildAbsoluteFixturePath(attributes[key]) | ||
825 | } | ||
826 | |||
827 | if (attributes.fixture) attaches.videofile = buildAbsoluteFixturePath(attributes.fixture) | ||
828 | |||
829 | return attaches | ||
830 | } | ||
831 | } | ||