aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/activitypub.ts26
-rw-r--r--server/helpers/custom-validators/activitypub/videos.ts6
-rw-r--r--server/helpers/custom-validators/videos.ts24
-rw-r--r--server/helpers/utils.ts22
4 files changed, 47 insertions, 31 deletions
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'
8import { pageToStartAndCount } from './core-utils' 8import { pageToStartAndCount } from './core-utils'
9 9
10function activityPubContextify <T> (data: T) { 10function activityPubContextify <T> (data: T) {
11 return Object.assign(data,{ 11 return Object.assign(data, {
12 '@context': [ 12 '@context': [
13 'https://www.w3.org/ns/activitystreams', 13 'https://www.w3.org/ns/activitystreams',
14 'https://w3id.org/security/v1', 14 'https://w3id.org/security/v1',
15 { 15 {
16 'RsaSignature2017': 'https://w3id.org/security#RsaSignature2017', 16 RsaSignature2017: 'https://w3id.org/security#RsaSignature2017',
17 'Hashtag': 'as:Hashtag', 17 Hashtag: 'as:Hashtag',
18 'uuid': 'http://schema.org/identifier', 18 uuid: 'http://schema.org/identifier',
19 'category': 'http://schema.org/category', 19 category: 'http://schema.org/category',
20 'licence': 'http://schema.org/license', 20 licence: 'http://schema.org/license',
21 'sensitive': 'as:sensitive', 21 sensitive: 'as:sensitive',
22 'language': 'http://schema.org/inLanguage', 22 language: 'http://schema.org/inLanguage',
23 'views': 'http://schema.org/Number', 23 views: 'http://schema.org/Number',
24 'size': 'http://schema.org/Number', 24 stats: 'http://schema.org/Number',
25 'commentsEnabled': 'http://schema.org/Boolean', 25 size: 'http://schema.org/Number',
26 'support': 'http://schema.org/Text' 26 commentsEnabled: 'http://schema.org/Boolean',
27 waitTranscoding: 'http://schema.org/Boolean',
28 support: 'http://schema.org/Text'
27 }, 29 },
28 { 30 {
29 likes: { 31 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 {
6 isVideoAbuseReasonValid, 6 isVideoAbuseReasonValid,
7 isVideoDurationValid, 7 isVideoDurationValid,
8 isVideoNameValid, 8 isVideoNameValid,
9 isVideoStateValid,
9 isVideoTagValid, 10 isVideoTagValid,
10 isVideoTruncatedDescriptionValid, 11 isVideoTruncatedDescriptionValid,
11 isVideoViewsValid 12 isVideoViewsValid
12} from '../videos' 13} from '../videos'
13import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' 14import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
15import { VideoState } from '../../../../shared/models/videos'
14 16
15function sanitizeAndCheckVideoTorrentCreateActivity (activity: any) { 17function sanitizeAndCheckVideoTorrentCreateActivity (activity: any) {
16 return isBaseActivityValid(activity, 'Create') && 18 return isBaseActivityValid(activity, 'Create') &&
@@ -50,6 +52,10 @@ function sanitizeAndCheckVideoTorrentObject (video: any) {
50 if (!setRemoteVideoTruncatedContent(video)) return false 52 if (!setRemoteVideoTruncatedContent(video)) return false
51 if (!setValidAttributedTo(video)) return false 53 if (!setValidAttributedTo(video)) return false
52 54
55 // Default attributes
56 if (!isVideoStateValid(video.state)) video.state = VideoState.PUBLISHED
57 if (!isBooleanValid(video.waitTranscoding)) video.waitTranscoding = false
58
53 return isActivityPubUrlValid(video.id) && 59 return isActivityPubUrlValid(video.id) &&
54 isVideoNameValid(video.name) && 60 isVideoNameValid(video.name) &&
55 isActivityPubVideoDurationValid(video.duration) && 61 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 {
10 VIDEO_LICENCES, 10 VIDEO_LICENCES,
11 VIDEO_MIMETYPE_EXT, 11 VIDEO_MIMETYPE_EXT,
12 VIDEO_PRIVACIES, 12 VIDEO_PRIVACIES,
13 VIDEO_RATE_TYPES 13 VIDEO_RATE_TYPES,
14 VIDEO_STATES
14} from '../../initializers' 15} from '../../initializers'
15import { VideoModel } from '../../models/video/video' 16import { VideoModel } from '../../models/video/video'
16import { exists, isArray, isFileValid } from './misc' 17import { exists, isArray, isFileValid } from './misc'
@@ -21,11 +22,15 @@ const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
21const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES 22const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES
22 23
23function isVideoCategoryValid (value: any) { 24function isVideoCategoryValid (value: any) {
24 return value === null || VIDEO_CATEGORIES[value] !== undefined 25 return value === null || VIDEO_CATEGORIES[ value ] !== undefined
26}
27
28function isVideoStateValid (value: any) {
29 return exists(value) && VIDEO_STATES[ value ] !== undefined
25} 30}
26 31
27function isVideoLicenceValid (value: any) { 32function isVideoLicenceValid (value: any) {
28 return value === null || VIDEO_LICENCES[value] !== undefined 33 return value === null || VIDEO_LICENCES[ value ] !== undefined
29} 34}
30 35
31function isVideoLanguageValid (value: any) { 36function isVideoLanguageValid (value: any) {
@@ -79,20 +84,22 @@ function isVideoRatingTypeValid (value: string) {
79 84
80const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`) 85const videoFileTypes = Object.keys(VIDEO_MIMETYPE_EXT).map(m => `(${m})`)
81const videoFileTypesRegex = videoFileTypes.join('|') 86const videoFileTypesRegex = videoFileTypes.join('|')
87
82function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { 88function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
83 return isFileValid(files, videoFileTypesRegex, 'videofile') 89 return isFileValid(files, videoFileTypesRegex, 'videofile')
84} 90}
85 91
86const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME 92const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
87 .map(v => v.replace('.', '')) 93 .map(v => v.replace('.', ''))
88 .join('|') 94 .join('|')
89const videoImageTypesRegex = `image/(${videoImageTypes})` 95const videoImageTypesRegex = `image/(${videoImageTypes})`
96
90function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) { 97function isVideoImage (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], field: string) {
91 return isFileValid(files, videoImageTypesRegex, field, true) 98 return isFileValid(files, videoImageTypesRegex, field, true)
92} 99}
93 100
94function isVideoPrivacyValid (value: string) { 101function isVideoPrivacyValid (value: string) {
95 return validator.isInt(value + '') && VIDEO_PRIVACIES[value] !== undefined 102 return validator.isInt(value + '') && VIDEO_PRIVACIES[ value ] !== undefined
96} 103}
97 104
98function isVideoFileInfoHashValid (value: string) { 105function isVideoFileInfoHashValid (value: string) {
@@ -118,8 +125,8 @@ async function isVideoExist (id: string, res: Response) {
118 125
119 if (!video) { 126 if (!video) {
120 res.status(404) 127 res.status(404)
121 .json({ error: 'Video not found' }) 128 .json({ error: 'Video not found' })
122 .end() 129 .end()
123 130
124 return false 131 return false
125 } 132 }
@@ -169,6 +176,7 @@ export {
169 isVideoTagsValid, 176 isVideoTagsValid,
170 isVideoAbuseReasonValid, 177 isVideoAbuseReasonValid,
171 isVideoFile, 178 isVideoFile,
179 isVideoStateValid,
172 isVideoViewsValid, 180 isVideoViewsValid,
173 isVideoRatingTypeValid, 181 isVideoRatingTypeValid,
174 isVideoDurationValid, 182 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 @@
1import { Model } from 'sequelize-typescript' 1import { Model } from 'sequelize-typescript'
2import * as ipaddr from 'ipaddr.js' 2import * as ipaddr from 'ipaddr.js'
3const isCidr = require('is-cidr')
4import { ResultList } from '../../shared' 3import { ResultList } from '../../shared'
5import { VideoResolution } from '../../shared/models/videos' 4import { VideoResolution } from '../../shared/models/videos'
6import { CONFIG } from '../initializers' 5import { CONFIG } from '../initializers'
@@ -10,6 +9,8 @@ import { ApplicationModel } from '../models/application/application'
10import { pseudoRandomBytesPromise } from './core-utils' 9import { pseudoRandomBytesPromise } from './core-utils'
11import { logger } from './logger' 10import { logger } from './logger'
12 11
12const isCidr = require('is-cidr')
13
13async function generateRandomString (size: number) { 14async function generateRandomString (size: number) {
14 const raw = await pseudoRandomBytesPromise(size) 15 const raw = await pseudoRandomBytesPromise(size)
15 16
@@ -17,22 +18,20 @@ async function generateRandomString (size: number) {
17} 18}
18 19
19interface FormattableToJSON { 20interface FormattableToJSON {
20 toFormattedJSON () 21 toFormattedJSON (args?: any)
21} 22}
22 23
23function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) { 24function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number, formattedArg?: any) {
24 const formattedObjects: U[] = [] 25 const formattedObjects: U[] = []
25 26
26 objects.forEach(object => { 27 objects.forEach(object => {
27 formattedObjects.push(object.toFormattedJSON()) 28 formattedObjects.push(object.toFormattedJSON(formattedArg))
28 }) 29 })
29 30
30 const res: ResultList<U> = { 31 return {
31 total: objectsTotal, 32 total: objectsTotal,
32 data: formattedObjects 33 data: formattedObjects
33 } 34 } as ResultList<U>
34
35 return res
36} 35}
37 36
38async function isSignupAllowed () { 37async function isSignupAllowed () {
@@ -87,16 +86,17 @@ function computeResolutionsToTranscode (videoFileHeight: number) {
87 const resolutionsEnabled: number[] = [] 86 const resolutionsEnabled: number[] = []
88 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS 87 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
89 88
89 // Put in the order we want to proceed jobs
90 const resolutions = [ 90 const resolutions = [
91 VideoResolution.H_240P,
92 VideoResolution.H_360P,
93 VideoResolution.H_480P, 91 VideoResolution.H_480P,
92 VideoResolution.H_360P,
94 VideoResolution.H_720P, 93 VideoResolution.H_720P,
94 VideoResolution.H_240P,
95 VideoResolution.H_1080P 95 VideoResolution.H_1080P
96 ] 96 ]
97 97
98 for (const resolution of resolutions) { 98 for (const resolution of resolutions) {
99 if (configResolutions[resolution + 'p'] === true && videoFileHeight > resolution) { 99 if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) {
100 resolutionsEnabled.push(resolution) 100 resolutionsEnabled.push(resolution)
101 } 101 }
102 } 102 }