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