2021-07-15 03:02:54 -05:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
|
|
|
|
|
|
|
|
import { expect } from 'chai'
|
|
|
|
import { createReadStream, stat } from 'fs-extra'
|
|
|
|
import got, { Response as GotResponse } from 'got'
|
|
|
|
import validator from 'validator'
|
2022-10-25 04:51:20 -05:00
|
|
|
import { buildAbsoluteFixturePath, getAllPrivacies, omit, pick, wait } from '@shared/core-utils'
|
2021-12-29 07:44:58 -06:00
|
|
|
import { buildUUID } from '@shared/extra-utils'
|
2021-07-15 03:02:54 -05:00
|
|
|
import {
|
2021-07-16 07:27:30 -05:00
|
|
|
HttpStatusCode,
|
2021-07-15 03:02:54 -05:00
|
|
|
ResultList,
|
|
|
|
UserVideoRateType,
|
|
|
|
Video,
|
|
|
|
VideoCreate,
|
|
|
|
VideoCreateResult,
|
|
|
|
VideoDetails,
|
|
|
|
VideoFileMetadata,
|
2022-10-25 04:51:20 -05:00
|
|
|
VideoInclude,
|
2021-07-15 03:02:54 -05:00
|
|
|
VideoPrivacy,
|
2021-11-18 07:35:08 -06:00
|
|
|
VideosCommonQuery,
|
|
|
|
VideoTranscodingCreate
|
2021-07-15 03:02:54 -05:00
|
|
|
} from '@shared/models'
|
2022-07-29 07:50:41 -05:00
|
|
|
import { VideoSource } from '@shared/models/videos/video-source'
|
2021-07-15 03:02:54 -05:00
|
|
|
import { unwrapBody } from '../requests'
|
2021-11-02 13:11:20 -05:00
|
|
|
import { waitJobs } from '../server'
|
2021-07-15 03:02:54 -05:00
|
|
|
import { AbstractCommand, OverrideCommandOptions } from '../shared'
|
|
|
|
|
|
|
|
export type VideoEdit = Partial<Omit<VideoCreate, 'thumbnailfile' | 'previewfile'>> & {
|
|
|
|
fixture?: string
|
|
|
|
thumbnailfile?: string
|
|
|
|
previewfile?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export class VideosCommand extends AbstractCommand {
|
|
|
|
getCategories (options: OverrideCommandOptions = {}) {
|
|
|
|
const path = '/api/v1/videos/categories'
|
|
|
|
|
|
|
|
return this.getRequestBody<{ [id: number]: string }>({
|
|
|
|
...options,
|
|
|
|
path,
|
|
|
|
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
getLicences (options: OverrideCommandOptions = {}) {
|
|
|
|
const path = '/api/v1/videos/licences'
|
|
|
|
|
|
|
|
return this.getRequestBody<{ [id: number]: string }>({
|
|
|
|
...options,
|
|
|
|
path,
|
|
|
|
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
getLanguages (options: OverrideCommandOptions = {}) {
|
|
|
|
const path = '/api/v1/videos/languages'
|
|
|
|
|
|
|
|
return this.getRequestBody<{ [id: string]: string }>({
|
|
|
|
...options,
|
|
|
|
path,
|
|
|
|
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
getPrivacies (options: OverrideCommandOptions = {}) {
|
|
|
|
const path = '/api/v1/videos/privacies'
|
|
|
|
|
|
|
|
return this.getRequestBody<{ [id in VideoPrivacy]: string }>({
|
|
|
|
...options,
|
|
|
|
path,
|
|
|
|
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
getDescription (options: OverrideCommandOptions & {
|
|
|
|
descriptionPath: string
|
|
|
|
}) {
|
|
|
|
return this.getRequestBody<{ description: string }>({
|
|
|
|
...options,
|
|
|
|
path: options.descriptionPath,
|
|
|
|
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
getFileMetadata (options: OverrideCommandOptions & {
|
|
|
|
url: string
|
|
|
|
}) {
|
|
|
|
return unwrapBody<VideoFileMetadata>(this.getRawRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
url: options.url,
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
rate (options: OverrideCommandOptions & {
|
|
|
|
id: number | string
|
|
|
|
rating: UserVideoRateType
|
2023-06-29 02:48:55 -05:00
|
|
|
videoPassword?: string
|
2021-07-15 03:02:54 -05:00
|
|
|
}) {
|
2023-06-29 02:48:55 -05:00
|
|
|
const { id, rating, videoPassword } = options
|
2021-07-15 03:02:54 -05:00
|
|
|
const path = '/api/v1/videos/' + id + '/rate'
|
|
|
|
|
|
|
|
return this.putBodyRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
fields: { rating },
|
2023-06-29 02:48:55 -05:00
|
|
|
headers: this.buildVideoPasswordHeader(videoPassword),
|
2021-07-15 03:02:54 -05:00
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
get (options: OverrideCommandOptions & {
|
|
|
|
id: number | string
|
|
|
|
}) {
|
|
|
|
const path = '/api/v1/videos/' + options.id
|
|
|
|
|
|
|
|
return this.getRequestBody<VideoDetails>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
getWithToken (options: OverrideCommandOptions & {
|
|
|
|
id: number | string
|
|
|
|
}) {
|
|
|
|
return this.get({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
token: this.buildCommonRequestToken({ ...options, implicitToken: true })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-29 02:48:55 -05:00
|
|
|
getWithPassword (options: OverrideCommandOptions & {
|
|
|
|
id: number | string
|
|
|
|
password?: string
|
|
|
|
}) {
|
|
|
|
const path = '/api/v1/videos/' + options.id
|
|
|
|
|
|
|
|
return this.getRequestBody<VideoDetails>({
|
|
|
|
...options,
|
|
|
|
headers:{
|
|
|
|
'x-peertube-video-password': options.password
|
|
|
|
},
|
|
|
|
path,
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-21 08:31:25 -05:00
|
|
|
getSource (options: OverrideCommandOptions & {
|
|
|
|
id: number | string
|
|
|
|
}) {
|
|
|
|
const path = '/api/v1/videos/' + options.id + '/source'
|
|
|
|
|
|
|
|
return this.getRequestBody<VideoSource>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-15 03:02:54 -05:00
|
|
|
async getId (options: OverrideCommandOptions & {
|
|
|
|
uuid: number | string
|
|
|
|
}) {
|
|
|
|
const { uuid } = options
|
|
|
|
|
|
|
|
if (validator.isUUID('' + uuid) === false) return uuid as number
|
|
|
|
|
|
|
|
const { id } = await this.get({ ...options, id: uuid })
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2021-08-17 01:26:20 -05:00
|
|
|
async listFiles (options: OverrideCommandOptions & {
|
|
|
|
id: number | string
|
|
|
|
}) {
|
|
|
|
const video = await this.get(options)
|
|
|
|
|
|
|
|
const files = video.files || []
|
|
|
|
const hlsFiles = video.streamingPlaylists[0]?.files || []
|
|
|
|
|
|
|
|
return files.concat(hlsFiles)
|
|
|
|
}
|
|
|
|
|
2021-07-15 03:02:54 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
listMyVideos (options: OverrideCommandOptions & {
|
|
|
|
start?: number
|
|
|
|
count?: number
|
|
|
|
sort?: string
|
|
|
|
search?: string
|
|
|
|
isLive?: boolean
|
2021-10-20 02:05:43 -05:00
|
|
|
channelId?: number
|
2021-07-15 03:02:54 -05:00
|
|
|
} = {}) {
|
|
|
|
const path = '/api/v1/users/me/videos'
|
|
|
|
|
|
|
|
return this.getRequestBody<ResultList<Video>>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
2021-10-20 02:05:43 -05:00
|
|
|
query: pick(options, [ 'start', 'count', 'sort', 'search', 'isLive', 'channelId' ]),
|
2021-07-15 03:02:54 -05:00
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-02 07:50:55 -06:00
|
|
|
listMySubscriptionVideos (options: OverrideCommandOptions & VideosCommonQuery = {}) {
|
|
|
|
const { sort = '-createdAt' } = options
|
|
|
|
const path = '/api/v1/users/me/subscriptions/videos'
|
|
|
|
|
|
|
|
return this.getRequestBody<ResultList<Video>>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
query: { sort, ...this.buildListQuery(options) },
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-15 03:02:54 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
list (options: OverrideCommandOptions & VideosCommonQuery = {}) {
|
|
|
|
const path = '/api/v1/videos'
|
|
|
|
|
|
|
|
const query = this.buildListQuery(options)
|
|
|
|
|
|
|
|
return this.getRequestBody<ResultList<Video>>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
query: { sort: 'name', ...query },
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
listWithToken (options: OverrideCommandOptions & VideosCommonQuery = {}) {
|
|
|
|
return this.list({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
token: this.buildCommonRequestToken({ ...options, implicitToken: true })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-25 04:51:20 -05:00
|
|
|
listAllForAdmin (options: OverrideCommandOptions & VideosCommonQuery = {}) {
|
|
|
|
const include = VideoInclude.NOT_PUBLISHED_STATE | VideoInclude.BLACKLISTED | VideoInclude.BLOCKED_OWNER
|
|
|
|
const nsfw = 'both'
|
|
|
|
const privacyOneOf = getAllPrivacies()
|
|
|
|
|
|
|
|
return this.list({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
include,
|
|
|
|
nsfw,
|
|
|
|
privacyOneOf,
|
|
|
|
|
|
|
|
token: this.buildCommonRequestToken({ ...options, implicitToken: true })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-27 07:37:04 -05:00
|
|
|
listByAccount (options: OverrideCommandOptions & VideosCommonQuery & {
|
2021-07-16 03:42:24 -05:00
|
|
|
handle: string
|
2021-07-15 03:02:54 -05:00
|
|
|
}) {
|
2021-07-16 03:42:24 -05:00
|
|
|
const { handle, search } = options
|
|
|
|
const path = '/api/v1/accounts/' + handle + '/videos'
|
2021-07-15 03:02:54 -05:00
|
|
|
|
|
|
|
return this.getRequestBody<ResultList<Video>>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
query: { search, ...this.buildListQuery(options) },
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-27 07:37:04 -05:00
|
|
|
listByChannel (options: OverrideCommandOptions & VideosCommonQuery & {
|
2021-07-16 03:42:24 -05:00
|
|
|
handle: string
|
2021-07-15 03:02:54 -05:00
|
|
|
}) {
|
2021-07-16 03:42:24 -05:00
|
|
|
const { handle } = options
|
|
|
|
const path = '/api/v1/video-channels/' + handle + '/videos'
|
2021-07-15 03:02:54 -05:00
|
|
|
|
|
|
|
return this.getRequestBody<ResultList<Video>>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
query: this.buildListQuery(options),
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-07-20 07:15:15 -05:00
|
|
|
async find (options: OverrideCommandOptions & {
|
|
|
|
name: string
|
|
|
|
}) {
|
|
|
|
const { data } = await this.list(options)
|
|
|
|
|
|
|
|
return data.find(v => v.name === options.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-07-15 03:02:54 -05:00
|
|
|
update (options: OverrideCommandOptions & {
|
|
|
|
id: number | string
|
|
|
|
attributes?: VideoEdit
|
|
|
|
}) {
|
|
|
|
const { id, attributes = {} } = options
|
|
|
|
const path = '/api/v1/videos/' + id
|
|
|
|
|
|
|
|
// Upload request
|
|
|
|
if (attributes.thumbnailfile || attributes.previewfile) {
|
|
|
|
const attaches: any = {}
|
|
|
|
if (attributes.thumbnailfile) attaches.thumbnailfile = attributes.thumbnailfile
|
|
|
|
if (attributes.previewfile) attaches.previewfile = attributes.previewfile
|
|
|
|
|
|
|
|
return this.putUploadRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
fields: options.attributes,
|
|
|
|
attaches: {
|
|
|
|
thumbnailfile: attributes.thumbnailfile,
|
|
|
|
previewfile: attributes.previewfile
|
|
|
|
},
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.putBodyRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
fields: options.attributes,
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
remove (options: OverrideCommandOptions & {
|
|
|
|
id: number | string
|
|
|
|
}) {
|
|
|
|
const path = '/api/v1/videos/' + options.id
|
|
|
|
|
2021-07-16 03:42:24 -05:00
|
|
|
return unwrapBody(this.deleteRequest({
|
2021-07-15 03:02:54 -05:00
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
2021-07-16 03:42:24 -05:00
|
|
|
}))
|
2021-07-15 03:02:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async removeAll () {
|
|
|
|
const { data } = await this.list()
|
|
|
|
|
|
|
|
for (const v of data) {
|
|
|
|
await this.remove({ id: v.id })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async upload (options: OverrideCommandOptions & {
|
|
|
|
attributes?: VideoEdit
|
|
|
|
mode?: 'legacy' | 'resumable' // default legacy
|
2022-10-12 09:09:02 -05:00
|
|
|
waitTorrentGeneration?: boolean // default true
|
2021-07-15 03:02:54 -05:00
|
|
|
} = {}) {
|
2022-10-21 07:38:10 -05:00
|
|
|
const { mode = 'legacy', waitTorrentGeneration = true } = options
|
2021-07-15 03:02:54 -05:00
|
|
|
let defaultChannelId = 1
|
|
|
|
|
|
|
|
try {
|
2021-07-16 02:04:35 -05:00
|
|
|
const { videoChannels } = await this.server.users.getMyInfo({ token: options.token })
|
2021-07-15 03:02:54 -05:00
|
|
|
defaultChannelId = videoChannels[0].id
|
|
|
|
} catch (e) { /* empty */ }
|
|
|
|
|
|
|
|
// Override default attributes
|
|
|
|
const attributes = {
|
|
|
|
name: 'my super video',
|
|
|
|
category: 5,
|
|
|
|
licence: 4,
|
|
|
|
language: 'zh',
|
|
|
|
channelId: defaultChannelId,
|
|
|
|
nsfw: true,
|
|
|
|
waitTranscoding: false,
|
|
|
|
description: 'my super description',
|
|
|
|
support: 'my super support text',
|
|
|
|
tags: [ 'tag' ],
|
|
|
|
privacy: VideoPrivacy.PUBLIC,
|
|
|
|
commentsEnabled: true,
|
|
|
|
downloadEnabled: true,
|
|
|
|
fixture: 'video_short.webm',
|
|
|
|
|
|
|
|
...options.attributes
|
|
|
|
}
|
|
|
|
|
2021-07-16 07:27:30 -05:00
|
|
|
const created = mode === 'legacy'
|
2021-07-15 03:02:54 -05:00
|
|
|
? await this.buildLegacyUpload({ ...options, attributes })
|
|
|
|
: await this.buildResumeUpload({ ...options, attributes })
|
|
|
|
|
|
|
|
// Wait torrent generation
|
2021-07-16 07:27:30 -05:00
|
|
|
const expectedStatus = this.buildExpectedStatus({ ...options, defaultExpectedStatus: HttpStatusCode.OK_200 })
|
2022-10-12 09:09:02 -05:00
|
|
|
if (expectedStatus === HttpStatusCode.OK_200 && waitTorrentGeneration) {
|
2021-07-15 03:02:54 -05:00
|
|
|
let video: VideoDetails
|
|
|
|
|
|
|
|
do {
|
2021-07-16 07:27:30 -05:00
|
|
|
video = await this.getWithToken({ ...options, id: created.uuid })
|
2021-07-15 03:02:54 -05:00
|
|
|
|
|
|
|
await wait(50)
|
|
|
|
} while (!video.files[0].torrentUrl)
|
|
|
|
}
|
|
|
|
|
2021-07-16 07:27:30 -05:00
|
|
|
return created
|
2021-07-15 03:02:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async buildLegacyUpload (options: OverrideCommandOptions & {
|
|
|
|
attributes: VideoEdit
|
|
|
|
}): Promise<VideoCreateResult> {
|
|
|
|
const path = '/api/v1/videos/upload'
|
|
|
|
|
|
|
|
return unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
fields: this.buildUploadFields(options.attributes),
|
|
|
|
attaches: this.buildUploadAttaches(options.attributes),
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})).then(body => body.video || body as any)
|
|
|
|
}
|
|
|
|
|
|
|
|
async buildResumeUpload (options: OverrideCommandOptions & {
|
|
|
|
attributes: VideoEdit
|
2021-07-16 03:42:24 -05:00
|
|
|
}): Promise<VideoCreateResult> {
|
2021-07-15 03:02:54 -05:00
|
|
|
const { attributes, expectedStatus } = options
|
|
|
|
|
|
|
|
let size = 0
|
|
|
|
let videoFilePath: string
|
|
|
|
let mimetype = 'video/mp4'
|
|
|
|
|
|
|
|
if (attributes.fixture) {
|
|
|
|
videoFilePath = buildAbsoluteFixturePath(attributes.fixture)
|
|
|
|
size = (await stat(videoFilePath)).size
|
|
|
|
|
|
|
|
if (videoFilePath.endsWith('.mkv')) {
|
|
|
|
mimetype = 'video/x-matroska'
|
|
|
|
} else if (videoFilePath.endsWith('.webm')) {
|
|
|
|
mimetype = 'video/webm'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 03:42:24 -05:00
|
|
|
// Do not check status automatically, we'll check it manually
|
|
|
|
const initializeSessionRes = await this.prepareResumableUpload({ ...options, expectedStatus: null, attributes, size, mimetype })
|
2021-07-15 03:02:54 -05:00
|
|
|
const initStatus = initializeSessionRes.status
|
|
|
|
|
|
|
|
if (videoFilePath && initStatus === HttpStatusCode.CREATED_201) {
|
|
|
|
const locationHeader = initializeSessionRes.header['location']
|
|
|
|
expect(locationHeader).to.not.be.undefined
|
|
|
|
|
|
|
|
const pathUploadId = locationHeader.split('?')[1]
|
|
|
|
|
|
|
|
const result = await this.sendResumableChunks({ ...options, pathUploadId, videoFilePath, size })
|
|
|
|
|
2021-09-09 02:55:20 -05:00
|
|
|
if (result.statusCode === HttpStatusCode.OK_200) {
|
|
|
|
await this.endResumableUpload({ ...options, expectedStatus: HttpStatusCode.NO_CONTENT_204, pathUploadId })
|
|
|
|
}
|
2021-09-09 02:31:50 -05:00
|
|
|
|
2021-07-16 03:42:24 -05:00
|
|
|
return result.body?.video || result.body as any
|
2021-07-15 03:02:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const expectedInitStatus = expectedStatus === HttpStatusCode.OK_200
|
|
|
|
? HttpStatusCode.CREATED_201
|
|
|
|
: expectedStatus
|
|
|
|
|
|
|
|
expect(initStatus).to.equal(expectedInitStatus)
|
|
|
|
|
2021-07-16 03:42:24 -05:00
|
|
|
return initializeSessionRes.body.video || initializeSessionRes.body
|
2021-07-15 03:02:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async prepareResumableUpload (options: OverrideCommandOptions & {
|
|
|
|
attributes: VideoEdit
|
|
|
|
size: number
|
|
|
|
mimetype: string
|
2021-11-10 02:42:37 -06:00
|
|
|
|
|
|
|
originalName?: string
|
|
|
|
lastModified?: number
|
2021-07-15 03:02:54 -05:00
|
|
|
}) {
|
2021-11-10 02:42:37 -06:00
|
|
|
const { attributes, originalName, lastModified, size, mimetype } = options
|
2021-07-15 03:02:54 -05:00
|
|
|
|
|
|
|
const path = '/api/v1/videos/upload-resumable'
|
|
|
|
|
|
|
|
return this.postUploadRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
headers: {
|
|
|
|
'X-Upload-Content-Type': mimetype,
|
|
|
|
'X-Upload-Content-Length': size.toString()
|
|
|
|
},
|
2021-11-10 02:42:37 -06:00
|
|
|
fields: {
|
|
|
|
filename: attributes.fixture,
|
|
|
|
originalName,
|
|
|
|
lastModified,
|
|
|
|
|
|
|
|
...this.buildUploadFields(options.attributes)
|
|
|
|
},
|
|
|
|
|
2021-07-16 03:42:24 -05:00
|
|
|
// Fixture will be sent later
|
2022-08-17 08:25:58 -05:00
|
|
|
attaches: this.buildUploadAttaches(omit(options.attributes, [ 'fixture' ])),
|
2021-07-15 03:02:54 -05:00
|
|
|
implicitToken: true,
|
2021-07-16 03:42:24 -05:00
|
|
|
|
2021-07-15 03:02:54 -05:00
|
|
|
defaultExpectedStatus: null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sendResumableChunks (options: OverrideCommandOptions & {
|
|
|
|
pathUploadId: string
|
|
|
|
videoFilePath: string
|
|
|
|
size: number
|
|
|
|
contentLength?: number
|
|
|
|
contentRangeBuilder?: (start: number, chunk: any) => string
|
2022-04-19 07:25:33 -05:00
|
|
|
digestBuilder?: (chunk: any) => string
|
2021-07-15 03:02:54 -05:00
|
|
|
}) {
|
2022-04-19 07:25:33 -05:00
|
|
|
const {
|
|
|
|
pathUploadId,
|
|
|
|
videoFilePath,
|
|
|
|
size,
|
|
|
|
contentLength,
|
|
|
|
contentRangeBuilder,
|
|
|
|
digestBuilder,
|
|
|
|
expectedStatus = HttpStatusCode.OK_200
|
|
|
|
} = options
|
2021-07-15 03:02:54 -05:00
|
|
|
|
|
|
|
const path = '/api/v1/videos/upload-resumable'
|
|
|
|
let start = 0
|
|
|
|
|
|
|
|
const token = this.buildCommonRequestToken({ ...options, implicitToken: true })
|
|
|
|
const url = this.server.url
|
|
|
|
|
|
|
|
const readable = createReadStream(videoFilePath, { highWaterMark: 8 * 1024 })
|
|
|
|
return new Promise<GotResponse<{ video: VideoCreateResult }>>((resolve, reject) => {
|
|
|
|
readable.on('data', async function onData (chunk) {
|
|
|
|
readable.pause()
|
|
|
|
|
|
|
|
const headers = {
|
|
|
|
'Authorization': 'Bearer ' + token,
|
|
|
|
'Content-Type': 'application/octet-stream',
|
|
|
|
'Content-Range': contentRangeBuilder
|
|
|
|
? contentRangeBuilder(start, chunk)
|
|
|
|
: `bytes ${start}-${start + chunk.length - 1}/${size}`,
|
|
|
|
'Content-Length': contentLength ? contentLength + '' : chunk.length + ''
|
|
|
|
}
|
|
|
|
|
2022-04-19 07:25:33 -05:00
|
|
|
if (digestBuilder) {
|
|
|
|
Object.assign(headers, { digest: digestBuilder(chunk) })
|
|
|
|
}
|
|
|
|
|
2021-07-15 03:02:54 -05:00
|
|
|
const res = await got<{ video: VideoCreateResult }>({
|
|
|
|
url,
|
|
|
|
method: 'put',
|
|
|
|
headers,
|
|
|
|
path: path + '?' + pathUploadId,
|
|
|
|
body: chunk,
|
|
|
|
responseType: 'json',
|
|
|
|
throwHttpErrors: false
|
|
|
|
})
|
|
|
|
|
|
|
|
start += chunk.length
|
|
|
|
|
|
|
|
if (res.statusCode === expectedStatus) {
|
|
|
|
return resolve(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.statusCode !== HttpStatusCode.PERMANENT_REDIRECT_308) {
|
|
|
|
readable.off('data', onData)
|
|
|
|
return reject(new Error('Incorrect transient behaviour sending intermediary chunks'))
|
|
|
|
}
|
|
|
|
|
|
|
|
readable.resume()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-09 02:31:50 -05:00
|
|
|
endResumableUpload (options: OverrideCommandOptions & {
|
|
|
|
pathUploadId: string
|
|
|
|
}) {
|
|
|
|
return this.deleteRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path: '/api/v1/videos/upload-resumable',
|
|
|
|
rawQuery: options.pathUploadId,
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-15 03:02:54 -05:00
|
|
|
quickUpload (options: OverrideCommandOptions & {
|
|
|
|
name: string
|
|
|
|
nsfw?: boolean
|
|
|
|
privacy?: VideoPrivacy
|
|
|
|
fixture?: string
|
2023-06-29 02:48:55 -05:00
|
|
|
videoPasswords?: string[]
|
2021-07-15 03:02:54 -05:00
|
|
|
}) {
|
|
|
|
const attributes: VideoEdit = { name: options.name }
|
|
|
|
if (options.nsfw) attributes.nsfw = options.nsfw
|
|
|
|
if (options.privacy) attributes.privacy = options.privacy
|
|
|
|
if (options.fixture) attributes.fixture = options.fixture
|
2023-06-29 02:48:55 -05:00
|
|
|
if (options.videoPasswords) attributes.videoPasswords = options.videoPasswords
|
2021-07-15 03:02:54 -05:00
|
|
|
|
|
|
|
return this.upload({ ...options, attributes })
|
|
|
|
}
|
|
|
|
|
|
|
|
async randomUpload (options: OverrideCommandOptions & {
|
|
|
|
wait?: boolean // default true
|
2021-07-16 07:27:30 -05:00
|
|
|
additionalParams?: VideoEdit & { prefixName?: string }
|
2021-07-15 03:02:54 -05:00
|
|
|
} = {}) {
|
|
|
|
const { wait = true, additionalParams } = options
|
|
|
|
const prefixName = additionalParams?.prefixName || ''
|
|
|
|
const name = prefixName + buildUUID()
|
|
|
|
|
2021-07-16 07:27:30 -05:00
|
|
|
const attributes = { name, ...additionalParams }
|
2021-07-15 03:02:54 -05:00
|
|
|
|
|
|
|
const result = await this.upload({ ...options, attributes })
|
|
|
|
|
2021-07-16 03:42:24 -05:00
|
|
|
if (wait) await waitJobs([ this.server ])
|
|
|
|
|
2021-07-15 03:02:54 -05:00
|
|
|
return { ...result, name }
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-07-29 07:50:41 -05:00
|
|
|
removeHLSPlaylist (options: OverrideCommandOptions & {
|
2021-11-17 09:04:53 -06:00
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
const path = '/api/v1/videos/' + options.videoId + '/hls'
|
|
|
|
|
|
|
|
return this.deleteRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-29 07:50:41 -05:00
|
|
|
removeHLSFile (options: OverrideCommandOptions & {
|
|
|
|
videoId: number | string
|
|
|
|
fileId: number
|
|
|
|
}) {
|
|
|
|
const path = '/api/v1/videos/' + options.videoId + '/hls/' + options.fileId
|
|
|
|
|
|
|
|
return this.deleteRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
removeAllWebTorrentFiles (options: OverrideCommandOptions & {
|
2021-11-17 09:04:53 -06:00
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
const path = '/api/v1/videos/' + options.videoId + '/webtorrent'
|
|
|
|
|
|
|
|
return this.deleteRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
implicitToken: true,
|
2021-11-18 07:35:08 -06:00
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-29 07:50:41 -05:00
|
|
|
removeWebTorrentFile (options: OverrideCommandOptions & {
|
|
|
|
videoId: number | string
|
|
|
|
fileId: number
|
|
|
|
}) {
|
|
|
|
const path = '/api/v1/videos/' + options.videoId + '/webtorrent/' + options.fileId
|
|
|
|
|
|
|
|
return this.deleteRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-18 07:35:08 -06:00
|
|
|
runTranscoding (options: OverrideCommandOptions & {
|
|
|
|
videoId: number | string
|
|
|
|
transcodingType: 'hls' | 'webtorrent'
|
|
|
|
}) {
|
|
|
|
const path = '/api/v1/videos/' + options.videoId + '/transcoding'
|
|
|
|
|
|
|
|
const fields: VideoTranscodingCreate = pick(options, [ 'transcodingType' ])
|
|
|
|
|
|
|
|
return this.postBodyRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
fields,
|
|
|
|
implicitToken: true,
|
2021-11-17 09:04:53 -06:00
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-07-15 03:02:54 -05:00
|
|
|
private buildListQuery (options: VideosCommonQuery) {
|
|
|
|
return pick(options, [
|
|
|
|
'start',
|
|
|
|
'count',
|
|
|
|
'sort',
|
|
|
|
'nsfw',
|
|
|
|
'isLive',
|
|
|
|
'categoryOneOf',
|
|
|
|
'licenceOneOf',
|
|
|
|
'languageOneOf',
|
2022-10-12 09:09:02 -05:00
|
|
|
'privacyOneOf',
|
2021-07-15 03:02:54 -05:00
|
|
|
'tagsOneOf',
|
|
|
|
'tagsAllOf',
|
2021-10-27 07:37:04 -05:00
|
|
|
'isLocal',
|
|
|
|
'include',
|
2021-07-15 03:02:54 -05:00
|
|
|
'skipCount'
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildUploadFields (attributes: VideoEdit) {
|
2021-07-16 03:42:24 -05:00
|
|
|
return omit(attributes, [ 'fixture', 'thumbnailfile', 'previewfile' ])
|
2021-07-15 03:02:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private buildUploadAttaches (attributes: VideoEdit) {
|
|
|
|
const attaches: { [ name: string ]: string } = {}
|
|
|
|
|
|
|
|
for (const key of [ 'thumbnailfile', 'previewfile' ]) {
|
|
|
|
if (attributes[key]) attaches[key] = buildAbsoluteFixturePath(attributes[key])
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attributes.fixture) attaches.videofile = buildAbsoluteFixturePath(attributes.fixture)
|
|
|
|
|
|
|
|
return attaches
|
|
|
|
}
|
|
|
|
}
|