1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
3 import { expect } from 'chai'
4 import { createReadStream, stat } from 'fs-extra'
5 import got, { Response as GotResponse } from 'got'
6 import { omit } from 'lodash'
7 import validator from 'validator'
8 import { buildAbsoluteFixturePath, pick, wait } from '@shared/core-utils'
9 import { buildUUID } from '@shared/extra-utils'
21 VideoTranscodingCreate
22 } from '@shared/models'
23 import { unwrapBody } from '../requests'
24 import { waitJobs } from '../server'
25 import { AbstractCommand, OverrideCommandOptions } from '../shared'
27 export type VideoEdit = Partial<Omit<VideoCreate, 'thumbnailfile' | 'previewfile'>> & {
29 thumbnailfile?: string
33 export class VideosCommand extends AbstractCommand {
34 getCategories (options: OverrideCommandOptions = {}) {
35 const path = '/api/v1/videos/categories'
37 return this.getRequestBody<{ [id: number]: string }>({
42 defaultExpectedStatus: HttpStatusCode.OK_200
46 getLicences (options: OverrideCommandOptions = {}) {
47 const path = '/api/v1/videos/licences'
49 return this.getRequestBody<{ [id: number]: string }>({
54 defaultExpectedStatus: HttpStatusCode.OK_200
58 getLanguages (options: OverrideCommandOptions = {}) {
59 const path = '/api/v1/videos/languages'
61 return this.getRequestBody<{ [id: string]: string }>({
66 defaultExpectedStatus: HttpStatusCode.OK_200
70 getPrivacies (options: OverrideCommandOptions = {}) {
71 const path = '/api/v1/videos/privacies'
73 return this.getRequestBody<{ [id in VideoPrivacy]: string }>({
78 defaultExpectedStatus: HttpStatusCode.OK_200
82 // ---------------------------------------------------------------------------
84 getDescription (options: OverrideCommandOptions & {
85 descriptionPath: string
87 return this.getRequestBody<{ description: string }>({
89 path: options.descriptionPath,
92 defaultExpectedStatus: HttpStatusCode.OK_200
96 getFileMetadata (options: OverrideCommandOptions & {
99 return unwrapBody<VideoFileMetadata>(this.getRawRequest({
103 implicitToken: false,
104 defaultExpectedStatus: HttpStatusCode.OK_200
108 // ---------------------------------------------------------------------------
110 view (options: OverrideCommandOptions & {
112 xForwardedFor?: string
114 const { id, xForwardedFor } = options
115 const path = '/api/v1/videos/' + id + '/views'
117 return this.postBodyRequest({
122 implicitToken: false,
123 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
127 rate (options: OverrideCommandOptions & {
129 rating: UserVideoRateType
131 const { id, rating } = options
132 const path = '/api/v1/videos/' + id + '/rate'
134 return this.putBodyRequest({
140 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
144 // ---------------------------------------------------------------------------
146 get (options: OverrideCommandOptions & {
149 const path = '/api/v1/videos/' + options.id
151 return this.getRequestBody<VideoDetails>({
155 implicitToken: false,
156 defaultExpectedStatus: HttpStatusCode.OK_200
160 getWithToken (options: OverrideCommandOptions & {
166 token: this.buildCommonRequestToken({ ...options, implicitToken: true })
170 async getId (options: OverrideCommandOptions & {
171 uuid: number | string
173 const { uuid } = options
175 if (validator.isUUID('' + uuid) === false) return uuid as number
177 const { id } = await this.get({ ...options, id: uuid })
182 async listFiles (options: OverrideCommandOptions & {
185 const video = await this.get(options)
187 const files = video.files || []
188 const hlsFiles = video.streamingPlaylists[0]?.files || []
190 return files.concat(hlsFiles)
193 // ---------------------------------------------------------------------------
195 listMyVideos (options: OverrideCommandOptions & {
203 const path = '/api/v1/users/me/videos'
205 return this.getRequestBody<ResultList<Video>>({
209 query: pick(options, [ 'start', 'count', 'sort', 'search', 'isLive', 'channelId' ]),
211 defaultExpectedStatus: HttpStatusCode.OK_200
215 // ---------------------------------------------------------------------------
217 list (options: OverrideCommandOptions & VideosCommonQuery = {}) {
218 const path = '/api/v1/videos'
220 const query = this.buildListQuery(options)
222 return this.getRequestBody<ResultList<Video>>({
226 query: { sort: 'name', ...query },
227 implicitToken: false,
228 defaultExpectedStatus: HttpStatusCode.OK_200
232 listWithToken (options: OverrideCommandOptions & VideosCommonQuery = {}) {
236 token: this.buildCommonRequestToken({ ...options, implicitToken: true })
240 listByAccount (options: OverrideCommandOptions & VideosCommonQuery & {
243 const { handle, search } = options
244 const path = '/api/v1/accounts/' + handle + '/videos'
246 return this.getRequestBody<ResultList<Video>>({
250 query: { search, ...this.buildListQuery(options) },
252 defaultExpectedStatus: HttpStatusCode.OK_200
256 listByChannel (options: OverrideCommandOptions & VideosCommonQuery & {
259 const { handle } = options
260 const path = '/api/v1/video-channels/' + handle + '/videos'
262 return this.getRequestBody<ResultList<Video>>({
266 query: this.buildListQuery(options),
268 defaultExpectedStatus: HttpStatusCode.OK_200
272 // ---------------------------------------------------------------------------
274 async find (options: OverrideCommandOptions & {
277 const { data } = await this.list(options)
279 return data.find(v => v.name === options.name)
282 // ---------------------------------------------------------------------------
284 update (options: OverrideCommandOptions & {
286 attributes?: VideoEdit
288 const { id, attributes = {} } = options
289 const path = '/api/v1/videos/' + id
292 if (attributes.thumbnailfile || attributes.previewfile) {
293 const attaches: any = {}
294 if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
295 if (attributes.previewfile) attaches.previewfile = attributes.previewfile
297 return this.putUploadRequest({
301 fields: options.attributes,
303 thumbnailfile: attributes.thumbnailfile,
304 previewfile: attributes.previewfile
307 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
311 return this.putBodyRequest({
315 fields: options.attributes,
317 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
321 remove (options: OverrideCommandOptions & {
324 const path = '/api/v1/videos/' + options.id
326 return unwrapBody(this.deleteRequest({
331 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
336 const { data } = await this.list()
338 for (const v of data) {
339 await this.remove({ id: v.id })
343 // ---------------------------------------------------------------------------
345 async upload (options: OverrideCommandOptions & {
346 attributes?: VideoEdit
347 mode?: 'legacy' | 'resumable' // default legacy
349 const { mode = 'legacy' } = options
350 let defaultChannelId = 1
353 const { videoChannels } = await this.server.users.getMyInfo({ token: options.token })
354 defaultChannelId = videoChannels[0].id
355 } catch (e) { /* empty */ }
357 // Override default attributes
359 name: 'my super video',
363 channelId: defaultChannelId,
365 waitTranscoding: false,
366 description: 'my super description',
367 support: 'my super support text',
369 privacy: VideoPrivacy.PUBLIC,
370 commentsEnabled: true,
371 downloadEnabled: true,
372 fixture: 'video_short.webm',
374 ...options.attributes
377 const created = mode === 'legacy'
378 ? await this.buildLegacyUpload({ ...options, attributes })
379 : await this.buildResumeUpload({ ...options, attributes })
381 // Wait torrent generation
382 const expectedStatus = this.buildExpectedStatus({ ...options, defaultExpectedStatus: HttpStatusCode.OK_200 })
383 if (expectedStatus === HttpStatusCode.OK_200) {
384 let video: VideoDetails
387 video = await this.getWithToken({ ...options, id: created.uuid })
390 } while (!video.files[0].torrentUrl)
396 async buildLegacyUpload (options: OverrideCommandOptions & {
397 attributes: VideoEdit
398 }): Promise<VideoCreateResult> {
399 const path = '/api/v1/videos/upload'
401 return unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({
405 fields: this.buildUploadFields(options.attributes),
406 attaches: this.buildUploadAttaches(options.attributes),
408 defaultExpectedStatus: HttpStatusCode.OK_200
409 })).then(body => body.video || body as any)
412 async buildResumeUpload (options: OverrideCommandOptions & {
413 attributes: VideoEdit
414 }): Promise<VideoCreateResult> {
415 const { attributes, expectedStatus } = options
418 let videoFilePath: string
419 let mimetype = 'video/mp4'
421 if (attributes.fixture) {
422 videoFilePath = buildAbsoluteFixturePath(attributes.fixture)
423 size = (await stat(videoFilePath)).size
425 if (videoFilePath.endsWith('.mkv')) {
426 mimetype = 'video/x-matroska'
427 } else if (videoFilePath.endsWith('.webm')) {
428 mimetype = 'video/webm'
432 // Do not check status automatically, we'll check it manually
433 const initializeSessionRes = await this.prepareResumableUpload({ ...options, expectedStatus: null, attributes, size, mimetype })
434 const initStatus = initializeSessionRes.status
436 if (videoFilePath && initStatus === HttpStatusCode.CREATED_201) {
437 const locationHeader = initializeSessionRes.header['location']
438 expect(locationHeader).to.not.be.undefined
440 const pathUploadId = locationHeader.split('?')[1]
442 const result = await this.sendResumableChunks({ ...options, pathUploadId, videoFilePath, size })
444 if (result.statusCode === HttpStatusCode.OK_200) {
445 await this.endResumableUpload({ ...options, expectedStatus: HttpStatusCode.NO_CONTENT_204, pathUploadId })
448 return result.body?.video || result.body as any
451 const expectedInitStatus = expectedStatus === HttpStatusCode.OK_200
452 ? HttpStatusCode.CREATED_201
455 expect(initStatus).to.equal(expectedInitStatus)
457 return initializeSessionRes.body.video || initializeSessionRes.body
460 async prepareResumableUpload (options: OverrideCommandOptions & {
461 attributes: VideoEdit
465 originalName?: string
466 lastModified?: number
468 const { attributes, originalName, lastModified, size, mimetype } = options
470 const path = '/api/v1/videos/upload-resumable'
472 return this.postUploadRequest({
477 'X-Upload-Content-Type': mimetype,
478 'X-Upload-Content-Length': size.toString()
481 filename: attributes.fixture,
485 ...this.buildUploadFields(options.attributes)
488 // Fixture will be sent later
489 attaches: this.buildUploadAttaches(omit(options.attributes, 'fixture')),
492 defaultExpectedStatus: null
496 sendResumableChunks (options: OverrideCommandOptions & {
498 videoFilePath: string
500 contentLength?: number
501 contentRangeBuilder?: (start: number, chunk: any) => string
503 const { pathUploadId, videoFilePath, size, contentLength, contentRangeBuilder, expectedStatus = HttpStatusCode.OK_200 } = options
505 const path = '/api/v1/videos/upload-resumable'
508 const token = this.buildCommonRequestToken({ ...options, implicitToken: true })
509 const url = this.server.url
511 const readable = createReadStream(videoFilePath, { highWaterMark: 8 * 1024 })
512 return new Promise<GotResponse<{ video: VideoCreateResult }>>((resolve, reject) => {
513 readable.on('data', async function onData (chunk) {
517 'Authorization': 'Bearer ' + token,
518 'Content-Type': 'application/octet-stream',
519 'Content-Range': contentRangeBuilder
520 ? contentRangeBuilder(start, chunk)
521 : `bytes ${start}-${start + chunk.length - 1}/${size}`,
522 'Content-Length': contentLength ? contentLength + '' : chunk.length + ''
525 const res = await got<{ video: VideoCreateResult }>({
529 path: path + '?' + pathUploadId,
531 responseType: 'json',
532 throwHttpErrors: false
535 start += chunk.length
537 if (res.statusCode === expectedStatus) {
541 if (res.statusCode !== HttpStatusCode.PERMANENT_REDIRECT_308) {
542 readable.off('data', onData)
543 return reject(new Error('Incorrect transient behaviour sending intermediary chunks'))
551 endResumableUpload (options: OverrideCommandOptions & {
554 return this.deleteRequest({
557 path: '/api/v1/videos/upload-resumable',
558 rawQuery: options.pathUploadId,
560 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
564 quickUpload (options: OverrideCommandOptions & {
567 privacy?: VideoPrivacy
570 const attributes: VideoEdit = { name: options.name }
571 if (options.nsfw) attributes.nsfw = options.nsfw
572 if (options.privacy) attributes.privacy = options.privacy
573 if (options.fixture) attributes.fixture = options.fixture
575 return this.upload({ ...options, attributes })
578 async randomUpload (options: OverrideCommandOptions & {
579 wait?: boolean // default true
580 additionalParams?: VideoEdit & { prefixName?: string }
582 const { wait = true, additionalParams } = options
583 const prefixName = additionalParams?.prefixName || ''
584 const name = prefixName + buildUUID()
586 const attributes = { name, ...additionalParams }
588 const result = await this.upload({ ...options, attributes })
590 if (wait) await waitJobs([ this.server ])
592 return { ...result, name }
595 // ---------------------------------------------------------------------------
597 removeHLSFiles (options: OverrideCommandOptions & {
598 videoId: number | string
600 const path = '/api/v1/videos/' + options.videoId + '/hls'
602 return this.deleteRequest({
607 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
611 removeWebTorrentFiles (options: OverrideCommandOptions & {
612 videoId: number | string
614 const path = '/api/v1/videos/' + options.videoId + '/webtorrent'
616 return this.deleteRequest({
621 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
625 runTranscoding (options: OverrideCommandOptions & {
626 videoId: number | string
627 transcodingType: 'hls' | 'webtorrent'
629 const path = '/api/v1/videos/' + options.videoId + '/transcoding'
631 const fields: VideoTranscodingCreate = pick(options, [ 'transcodingType' ])
633 return this.postBodyRequest({
639 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
643 // ---------------------------------------------------------------------------
645 private buildListQuery (options: VideosCommonQuery) {
646 return pick(options, [
663 private buildUploadFields (attributes: VideoEdit) {
664 return omit(attributes, [ 'fixture', 'thumbnailfile', 'previewfile' ])
667 private buildUploadAttaches (attributes: VideoEdit) {
668 const attaches: { [ name: string ]: string } = {}
670 for (const key of [ 'thumbnailfile', 'previewfile' ]) {
671 if (attributes[key]) attaches[key] = buildAbsoluteFixturePath(attributes[key])
674 if (attributes.fixture) attaches.videofile = buildAbsoluteFixturePath(attributes.fixture)