From 2186386cca113506791583cb07d6ccacba7af4e0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 12 Jun 2018 20:04:58 +0200 Subject: Add concept of video state, and add ability to wait transcoding before publishing a video --- server/helpers/activitypub.ts | 26 ++++++++++++---------- .../custom-validators/activitypub/videos.ts | 6 +++++ server/helpers/custom-validators/videos.ts | 24 +++++++++++++------- server/helpers/utils.ts | 22 +++++++++--------- 4 files changed, 47 insertions(+), 31 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts index d1f3ec02d..37a251697 100644 --- a/server/helpers/activitypub.ts +++ b/server/helpers/activitypub.ts @@ -8,22 +8,24 @@ import { signObject } from './peertube-crypto' import { pageToStartAndCount } from './core-utils' function activityPubContextify (data: T) { - return Object.assign(data,{ + return Object.assign(data, { '@context': [ 'https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1', { - 'RsaSignature2017': 'https://w3id.org/security#RsaSignature2017', - 'Hashtag': 'as:Hashtag', - 'uuid': 'http://schema.org/identifier', - 'category': 'http://schema.org/category', - 'licence': 'http://schema.org/license', - 'sensitive': 'as:sensitive', - 'language': 'http://schema.org/inLanguage', - 'views': 'http://schema.org/Number', - 'size': 'http://schema.org/Number', - 'commentsEnabled': 'http://schema.org/Boolean', - 'support': 'http://schema.org/Text' + RsaSignature2017: 'https://w3id.org/security#RsaSignature2017', + Hashtag: 'as:Hashtag', + uuid: 'http://schema.org/identifier', + category: 'http://schema.org/category', + licence: 'http://schema.org/license', + sensitive: 'as:sensitive', + language: 'http://schema.org/inLanguage', + views: 'http://schema.org/Number', + stats: 'http://schema.org/Number', + size: 'http://schema.org/Number', + commentsEnabled: 'http://schema.org/Boolean', + waitTranscoding: 'http://schema.org/Boolean', + support: 'http://schema.org/Text' }, { likes: { diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index 7e1d57c34..37c90a0c8 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts @@ -6,11 +6,13 @@ import { isVideoAbuseReasonValid, isVideoDurationValid, isVideoNameValid, + isVideoStateValid, isVideoTagValid, isVideoTruncatedDescriptionValid, isVideoViewsValid } from '../videos' import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' +import { VideoState } from '../../../../shared/models/videos' function sanitizeAndCheckVideoTorrentCreateActivity (activity: any) { return isBaseActivityValid(activity, 'Create') && @@ -50,6 +52,10 @@ function sanitizeAndCheckVideoTorrentObject (video: any) { if (!setRemoteVideoTruncatedContent(video)) return false if (!setValidAttributedTo(video)) return false + // Default attributes + if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED + if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false + return isActivityPubUrlValid(video.id) && isVideoNameValid(video.name) && isActivityPubVideoDurationValid(video.duration) && diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index f365df985..8496e679a 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -10,7 +10,8 @@ import { VIDEO_LICENCES, VIDEO_MIMETYPE_EXT, VIDEO_PRIVACIES, - VIDEO_RATE_TYPES + VIDEO_RATE_TYPES, + VIDEO_STATES } from '../../initializers' import { VideoModel } from '../../models/video/video' import { exists, isArray, isFileValid } from './misc' @@ -21,11 +22,15 @@ const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES function isVideoCategoryValid (value: any) { - return value === null || VIDEO_CATEGORIES[value] !== undefined + return value === null || VIDEO_CATEGORIES[ value ] !== undefined +} + +function isVideoStateValid (value: any) { + return exists(value) && VIDEO_STATES[ value ] !== undefined } function isVideoLicenceValid (value: any) { - return value === null || VIDEO_LICENCES[value] !== undefined + return value === null || VIDEO_LICENCES[ value ] !== undefined } function isVideoLanguageValid (value: any) { @@ -79,20 +84,22 @@ function isVideoRatingTypeValid (value: string) { const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`) const videoFileTypesRegex = videoFileTypes.join('|') + function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { return isFileValid(files, videoFileTypesRegex, 'videofile') } const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME - .map(v => v.replace('.', '')) - .join('|') + .map(v => v.replace('.', '')) + .join('|') const videoImageTypesRegex = `image/(${videoImageTypes})` + function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) { return isFileValid(files, videoImageTypesRegex, field, true) } function isVideoPrivacyValid (value: string) { - return validator.isInt(value + '') && VIDEO_PRIVACIES[value] !== undefined + return validator.isInt(value + '') && VIDEO_PRIVACIES[ value ] !== undefined } function isVideoFileInfoHashValid (value: string) { @@ -118,8 +125,8 @@ async function isVideoExist (id: string, res: Response) { if (!video) { res.status(404) - .json({ error: 'Video not found' }) - .end() + .json({ error: 'Video not found' }) + .end() return false } @@ -169,6 +176,7 @@ export { isVideoTagsValid, isVideoAbuseReasonValid, isVideoFile, + isVideoStateValid, isVideoViewsValid, isVideoRatingTypeValid, isVideoDurationValid, diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts index e4556fa12..8fa861281 100644 --- a/server/helpers/utils.ts +++ b/server/helpers/utils.ts @@ -1,6 +1,5 @@ import { Model } from 'sequelize-typescript' import * as ipaddr from 'ipaddr.js' -const isCidr = require('is-cidr') import { ResultList } from '../../shared' import { VideoResolution } from '../../shared/models/videos' import { CONFIG } from '../initializers' @@ -10,6 +9,8 @@ import { ApplicationModel } from '../models/application/application' import { pseudoRandomBytesPromise } from './core-utils' import { logger } from './logger' +const isCidr = require('is-cidr') + async function generateRandomString (size: number) { const raw = await pseudoRandomBytesPromise(size) @@ -17,22 +18,20 @@ async function generateRandomString (size: number) { } interface FormattableToJSON { - toFormattedJSON () + toFormattedJSON (args?: any) } -function getFormattedObjects (objects: T[], objectsTotal: number) { +function getFormattedObjects (objects: T[], objectsTotal: number, formattedArg?: any) { const formattedObjects: U[] = [] objects.forEach(object => { - formattedObjects.push(object.toFormattedJSON()) + formattedObjects.push(object.toFormattedJSON(formattedArg)) }) - const res: ResultList = { + return { total: objectsTotal, data: formattedObjects - } - - return res + } as ResultList } async function isSignupAllowed () { @@ -87,16 +86,17 @@ function computeResolutionsToTranscode (videoFileHeight: number) { const resolutionsEnabled: number[] = [] const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS + // Put in the order we want to proceed jobs const resolutions = [ - VideoResolution.H_240P, - VideoResolution.H_360P, VideoResolution.H_480P, + VideoResolution.H_360P, VideoResolution.H_720P, + VideoResolution.H_240P, VideoResolution.H_1080P ] for (const resolution of resolutions) { - if (configResolutions[resolution + 'p'] === true && videoFileHeight > resolution) { + if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) { resolutionsEnabled.push(resolution) } } -- cgit v1.2.3