From 3545e72c686ff1725bbdfd8d16d693e2f4aa75a3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 12 Oct 2022 16:09:02 +0200 Subject: Put private videos under a specific subdirectory --- shared/core-utils/common/url.ts | 12 +++++++++ shared/models/server/debug.model.ts | 1 + shared/models/videos/index.ts | 2 ++ shared/models/videos/video-token.model.ts | 6 +++++ shared/server-commands/requests/requests.ts | 19 ++++++++++--- shared/server-commands/server/server.ts | 3 +++ shared/server-commands/videos/index.ts | 1 + shared/server-commands/videos/live-command.ts | 26 ++++++++++++++++++ shared/server-commands/videos/live.ts | 7 +++-- .../server-commands/videos/video-token-command.ts | 31 ++++++++++++++++++++++ shared/server-commands/videos/videos-command.ts | 6 +++-- 11 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 shared/models/videos/video-token.model.ts create mode 100644 shared/server-commands/videos/video-token-command.ts (limited to 'shared') diff --git a/shared/core-utils/common/url.ts b/shared/core-utils/common/url.ts index fd54e7594..d1c399f7b 100644 --- a/shared/core-utils/common/url.ts +++ b/shared/core-utils/common/url.ts @@ -1,6 +1,16 @@ import { Video, VideoPlaylist } from '../../models' import { secondsToTime } from './date' +function addQueryParams (url: string, params: { [ id: string ]: string }) { + const objUrl = new URL(url) + + for (const key of Object.keys(params)) { + objUrl.searchParams.append(key, params[key]) + } + + return objUrl.toString() +} + function buildPlaylistLink (playlist: Pick, base?: string) { return (base ?? window.location.origin) + buildPlaylistWatchPath(playlist) } @@ -103,6 +113,8 @@ function decoratePlaylistLink (options: { // --------------------------------------------------------------------------- export { + addQueryParams, + buildPlaylistLink, buildVideoLink, diff --git a/shared/models/server/debug.model.ts b/shared/models/server/debug.model.ts index 1c4597b8b..41f2109af 100644 --- a/shared/models/server/debug.model.ts +++ b/shared/models/server/debug.model.ts @@ -8,4 +8,5 @@ export interface SendDebugCommand { | 'process-video-views-buffer' | 'process-video-viewers' | 'process-video-channel-sync-latest' + | 'process-update-videos-scheduler' } diff --git a/shared/models/videos/index.ts b/shared/models/videos/index.ts index f8e6976d3..4c1790228 100644 --- a/shared/models/videos/index.ts +++ b/shared/models/videos/index.ts @@ -33,6 +33,8 @@ export * from './video-storage.enum' export * from './video-streaming-playlist.model' export * from './video-streaming-playlist.type' +export * from './video-token.model' + export * from './video-update.model' export * from './video-view.model' export * from './video.model' diff --git a/shared/models/videos/video-token.model.ts b/shared/models/videos/video-token.model.ts new file mode 100644 index 000000000..aefea565f --- /dev/null +++ b/shared/models/videos/video-token.model.ts @@ -0,0 +1,6 @@ +export interface VideoToken { + files: { + token: string + expires: string | Date + } +} diff --git a/shared/server-commands/requests/requests.ts b/shared/server-commands/requests/requests.ts index 8cc1245e0..b247017fd 100644 --- a/shared/server-commands/requests/requests.ts +++ b/shared/server-commands/requests/requests.ts @@ -3,7 +3,7 @@ import { decode } from 'querystring' import request from 'supertest' import { URL } from 'url' -import { buildAbsoluteFixturePath } from '@shared/core-utils' +import { buildAbsoluteFixturePath, pick } from '@shared/core-utils' import { HttpStatusCode } from '@shared/models' export type CommonRequestParams = { @@ -21,10 +21,21 @@ export type CommonRequestParams = { expectedStatus?: HttpStatusCode } -function makeRawRequest (url: string, expectedStatus?: HttpStatusCode, range?: string) { - const { host, protocol, pathname } = new URL(url) +function makeRawRequest (options: { + url: string + token?: string + expectedStatus?: HttpStatusCode + range?: string + query?: { [ id: string ]: string } +}) { + const { host, protocol, pathname } = new URL(options.url) + + return makeGetRequest({ + url: `${protocol}//${host}`, + path: pathname, - return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, expectedStatus, range }) + ...pick(options, [ 'expectedStatus', 'range', 'token', 'query' ]) + }) } function makeGetRequest (options: CommonRequestParams & { diff --git a/shared/server-commands/server/server.ts b/shared/server-commands/server/server.ts index 7096faf21..c062e6986 100644 --- a/shared/server-commands/server/server.ts +++ b/shared/server-commands/server/server.ts @@ -36,6 +36,7 @@ import { StreamingPlaylistsCommand, VideosCommand, VideoStudioCommand, + VideoTokenCommand, ViewsCommand } from '../videos' import { CommentsCommand } from '../videos/comments-command' @@ -145,6 +146,7 @@ export class PeerTubeServer { videoStats?: VideoStatsCommand views?: ViewsCommand twoFactor?: TwoFactorCommand + videoToken?: VideoTokenCommand constructor (options: { serverNumber: number } | { url: string }) { if ((options as any).url) { @@ -427,5 +429,6 @@ export class PeerTubeServer { this.videoStats = new VideoStatsCommand(this) this.views = new ViewsCommand(this) this.twoFactor = new TwoFactorCommand(this) + this.videoToken = new VideoTokenCommand(this) } } diff --git a/shared/server-commands/videos/index.ts b/shared/server-commands/videos/index.ts index b4d6fa37b..c17f6ef20 100644 --- a/shared/server-commands/videos/index.ts +++ b/shared/server-commands/videos/index.ts @@ -14,5 +14,6 @@ export * from './services-command' export * from './streaming-playlists-command' export * from './comments-command' export * from './video-studio-command' +export * from './video-token-command' export * from './views-command' export * from './videos-command' diff --git a/shared/server-commands/videos/live-command.ts b/shared/server-commands/videos/live-command.ts index b163f7189..de193fa49 100644 --- a/shared/server-commands/videos/live-command.ts +++ b/shared/server-commands/videos/live-command.ts @@ -12,6 +12,7 @@ import { ResultList, VideoCreateResult, VideoDetails, + VideoPrivacy, VideoState } from '@shared/models' import { unwrapBody } from '../requests' @@ -115,6 +116,31 @@ export class LiveCommand extends AbstractCommand { return body.video } + async quickCreate (options: OverrideCommandOptions & { + saveReplay: boolean + permanentLive: boolean + privacy?: VideoPrivacy + }) { + const { saveReplay, permanentLive, privacy } = options + + const { uuid } = await this.create({ + ...options, + + fields: { + name: 'live', + permanentLive, + saveReplay, + channelId: this.server.store.channel.id, + privacy + } + }) + + const video = await this.server.videos.getWithToken({ id: uuid }) + const live = await this.get({ videoId: uuid }) + + return { video, live } + } + // --------------------------------------------------------------------------- async sendRTMPStreamInVideo (options: OverrideCommandOptions & { diff --git a/shared/server-commands/videos/live.ts b/shared/server-commands/videos/live.ts index 0d9c32aab..ee7444b64 100644 --- a/shared/server-commands/videos/live.ts +++ b/shared/server-commands/videos/live.ts @@ -1,6 +1,6 @@ import ffmpeg, { FfmpegCommand } from 'fluent-ffmpeg' import { buildAbsoluteFixturePath, wait } from '@shared/core-utils' -import { VideoDetails, VideoInclude } from '@shared/models' +import { VideoDetails, VideoInclude, VideoPrivacy } from '@shared/models' import { PeerTubeServer } from '../server/server' function sendRTMPStream (options: { @@ -98,7 +98,10 @@ async function waitUntilLiveReplacedByReplayOnAllServers (servers: PeerTubeServe } async function findExternalSavedVideo (server: PeerTubeServer, liveDetails: VideoDetails) { - const { data } = await server.videos.list({ token: server.accessToken, sort: '-publishedAt', include: VideoInclude.BLACKLISTED }) + const include = VideoInclude.BLACKLISTED + const privacyOneOf = [ VideoPrivacy.INTERNAL, VideoPrivacy.PRIVATE, VideoPrivacy.PUBLIC, VideoPrivacy.UNLISTED ] + + const { data } = await server.videos.list({ token: server.accessToken, sort: '-publishedAt', include, privacyOneOf }) return data.find(v => v.name === liveDetails.name + ' - ' + new Date(liveDetails.publishedAt).toLocaleString()) } diff --git a/shared/server-commands/videos/video-token-command.ts b/shared/server-commands/videos/video-token-command.ts new file mode 100644 index 000000000..0531bee65 --- /dev/null +++ b/shared/server-commands/videos/video-token-command.ts @@ -0,0 +1,31 @@ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ + +import { HttpStatusCode, VideoToken } from '@shared/models' +import { unwrapBody } from '../requests' +import { AbstractCommand, OverrideCommandOptions } from '../shared' + +export class VideoTokenCommand extends AbstractCommand { + + create (options: OverrideCommandOptions & { + videoId: number | string + }) { + const { videoId } = options + const path = '/api/v1/videos/' + videoId + '/token' + + return unwrapBody(this.postBodyRequest({ + ...options, + + path, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.OK_200 + })) + } + + async getVideoFileToken (options: OverrideCommandOptions & { + videoId: number | string + }) { + const { files } = await this.create(options) + + return files.token + } +} diff --git a/shared/server-commands/videos/videos-command.ts b/shared/server-commands/videos/videos-command.ts index 168391523..5ec3b6ba8 100644 --- a/shared/server-commands/videos/videos-command.ts +++ b/shared/server-commands/videos/videos-command.ts @@ -342,8 +342,9 @@ export class VideosCommand extends AbstractCommand { async upload (options: OverrideCommandOptions & { attributes?: VideoEdit mode?: 'legacy' | 'resumable' // default legacy + waitTorrentGeneration?: boolean // default true } = {}) { - const { mode = 'legacy' } = options + const { mode = 'legacy', waitTorrentGeneration } = options let defaultChannelId = 1 try { @@ -377,7 +378,7 @@ export class VideosCommand extends AbstractCommand { // Wait torrent generation const expectedStatus = this.buildExpectedStatus({ ...options, defaultExpectedStatus: HttpStatusCode.OK_200 }) - if (expectedStatus === HttpStatusCode.OK_200) { + if (expectedStatus === HttpStatusCode.OK_200 && waitTorrentGeneration) { let video: VideoDetails do { @@ -692,6 +693,7 @@ export class VideosCommand extends AbstractCommand { 'categoryOneOf', 'licenceOneOf', 'languageOneOf', + 'privacyOneOf', 'tagsOneOf', 'tagsAllOf', 'isLocal', -- cgit v1.2.3