aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/server-commands/videos/videos-command.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/server-commands/videos/videos-command.ts')
-rw-r--r--shared/server-commands/videos/videos-command.ts829
1 files changed, 0 insertions, 829 deletions
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
3import { expect } from 'chai'
4import { createReadStream, stat } from 'fs-extra'
5import got, { Response as GotResponse } from 'got'
6import validator from 'validator'
7import { buildAbsoluteFixturePath, getAllPrivacies, omit, pick, wait } from '@shared/core-utils'
8import { buildUUID } from '@shared/extra-utils'
9import {
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'
23import { VideoSource } from '@shared/models/videos/video-source'
24import { unwrapBody } from '../requests'
25import { waitJobs } from '../server'
26import { AbstractCommand, OverrideCommandOptions } from '../shared'
27
28export type VideoEdit = Partial<Omit<VideoCreate, 'thumbnailfile' | 'previewfile'>> & {
29 fixture?: string
30 thumbnailfile?: string
31 previewfile?: string
32}
33
34export 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}