2023-06-29 03:48:55 -04:00
|
|
|
import { Response, Request, UploadFilesForCheck } from 'express'
|
2023-05-22 11:04:39 -04:00
|
|
|
import { decode as magnetUriDecode } from 'magnet-uri'
|
2020-01-07 08:56:07 -05:00
|
|
|
import validator from 'validator'
|
2023-06-29 03:48:55 -04:00
|
|
|
import { HttpStatusCode, VideoFilter, VideoInclude, VideoPrivacy, VideoRateType } from '@shared/models'
|
2017-12-12 11:53:50 -05:00
|
|
|
import {
|
2019-04-11 09:38:53 -04:00
|
|
|
CONSTRAINTS_FIELDS,
|
|
|
|
MIMETYPES,
|
2017-12-12 11:53:50 -05:00
|
|
|
VIDEO_CATEGORIES,
|
2018-06-07 03:43:18 -04:00
|
|
|
VIDEO_LICENCES,
|
2021-05-10 05:13:41 -04:00
|
|
|
VIDEO_LIVE,
|
2017-12-12 11:53:50 -05:00
|
|
|
VIDEO_PRIVACIES,
|
2018-06-12 14:04:58 -04:00
|
|
|
VIDEO_RATE_TYPES,
|
2021-05-10 05:13:41 -04:00
|
|
|
VIDEO_STATES
|
2019-04-11 08:26:41 -04:00
|
|
|
} from '../../initializers/constants'
|
2022-02-11 04:51:33 -05:00
|
|
|
import { exists, isArray, isDateValid, isFileValid } from './misc'
|
2023-06-29 03:48:55 -04:00
|
|
|
import { getVideoWithAttributes } from '@server/helpers/video'
|
2017-05-15 16:22:03 -04:00
|
|
|
|
|
|
|
const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS
|
2015-12-04 10:13:32 -05:00
|
|
|
|
2018-10-10 05:46:50 -04:00
|
|
|
function isVideoFilterValid (filter: VideoFilter) {
|
2020-11-18 09:29:38 -05:00
|
|
|
return filter === 'local' || filter === 'all-local' || filter === 'all'
|
2018-10-10 05:46:50 -04:00
|
|
|
}
|
|
|
|
|
2021-10-27 08:37:04 -04:00
|
|
|
function isVideoIncludeValid (include: VideoInclude) {
|
|
|
|
return exists(include) && validator.isInt('' + include)
|
|
|
|
}
|
|
|
|
|
2018-04-23 08:39:52 -04:00
|
|
|
function isVideoCategoryValid (value: any) {
|
2020-01-31 10:56:52 -05:00
|
|
|
return value === null || VIDEO_CATEGORIES[value] !== undefined
|
2018-06-12 14:04:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function isVideoStateValid (value: any) {
|
2020-01-31 10:56:52 -05:00
|
|
|
return exists(value) && VIDEO_STATES[value] !== undefined
|
2017-03-22 16:15:55 -04:00
|
|
|
}
|
|
|
|
|
2018-04-23 08:39:52 -04:00
|
|
|
function isVideoLicenceValid (value: any) {
|
2020-01-31 10:56:52 -05:00
|
|
|
return value === null || VIDEO_LICENCES[value] !== undefined
|
2017-03-27 14:53:11 -04:00
|
|
|
}
|
|
|
|
|
2018-04-23 08:39:52 -04:00
|
|
|
function isVideoLanguageValid (value: any) {
|
|
|
|
return value === null ||
|
|
|
|
(typeof value === 'string' && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.LANGUAGE))
|
2017-04-07 06:13:37 -04:00
|
|
|
}
|
|
|
|
|
2017-11-15 10:28:35 -05:00
|
|
|
function isVideoDurationValid (value: string) {
|
|
|
|
return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function isVideoDescriptionValid (value: string) {
|
2017-12-08 11:31:21 -05:00
|
|
|
return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION))
|
2016-06-06 08:15:03 -04:00
|
|
|
}
|
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
function isVideoSupportValid (value: string) {
|
|
|
|
return value === null || (exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.SUPPORT))
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function isVideoNameValid (value: string) {
|
|
|
|
return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
|
2016-06-06 08:15:03 -04:00
|
|
|
}
|
|
|
|
|
2017-11-10 08:34:45 -05:00
|
|
|
function isVideoTagValid (tag: string) {
|
|
|
|
return exists(tag) && validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
|
|
|
|
}
|
|
|
|
|
2022-02-11 04:51:33 -05:00
|
|
|
function areVideoTagsValid (tags: string[]) {
|
2018-05-16 03:28:18 -04:00
|
|
|
return tags === null || (
|
|
|
|
isArray(tags) &&
|
|
|
|
validator.isInt(tags.length.toString(), VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
|
|
|
|
tags.every(tag => isVideoTagValid(tag))
|
|
|
|
)
|
2016-06-06 08:15:03 -04:00
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function isVideoViewsValid (value: string) {
|
|
|
|
return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS)
|
2017-02-21 15:35:59 -05:00
|
|
|
}
|
|
|
|
|
2022-08-17 09:36:03 -04:00
|
|
|
const ratingTypes = new Set(Object.values(VIDEO_RATE_TYPES))
|
2017-06-10 16:15:25 -04:00
|
|
|
function isVideoRatingTypeValid (value: string) {
|
2022-08-17 09:36:03 -04:00
|
|
|
return value === 'none' || ratingTypes.has(value as VideoRateType)
|
2017-03-08 15:35:43 -05:00
|
|
|
}
|
|
|
|
|
2018-12-11 08:52:50 -05:00
|
|
|
function isVideoFileExtnameValid (value: string) {
|
2020-09-17 03:20:52 -04:00
|
|
|
return exists(value) && (value === VIDEO_LIVE.EXTENSION || MIMETYPES.VIDEO.EXT_MIMETYPE[value] !== undefined)
|
2018-12-11 08:52:50 -05:00
|
|
|
}
|
2018-06-12 14:04:58 -04:00
|
|
|
|
2022-02-11 04:51:33 -05:00
|
|
|
function isVideoFileMimeTypeValid (files: UploadFilesForCheck, field = 'videofile') {
|
|
|
|
return isFileValid({
|
|
|
|
files,
|
|
|
|
mimeTypeRegex: MIMETYPES.VIDEO.MIMETYPES_REGEX,
|
|
|
|
field,
|
|
|
|
maxSize: null
|
|
|
|
})
|
2018-02-13 12:17:05 -05:00
|
|
|
}
|
2017-02-10 05:27:14 -05:00
|
|
|
|
2018-02-13 12:17:05 -05:00
|
|
|
const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME
|
2018-06-12 14:04:58 -04:00
|
|
|
.map(v => v.replace('.', ''))
|
|
|
|
.join('|')
|
2018-02-13 12:17:05 -05:00
|
|
|
const videoImageTypesRegex = `image/(${videoImageTypes})`
|
2018-06-12 14:04:58 -04:00
|
|
|
|
2022-02-11 04:51:33 -05:00
|
|
|
function isVideoImageValid (files: UploadFilesForCheck, field: string, optional = true) {
|
|
|
|
return isFileValid({
|
|
|
|
files,
|
|
|
|
mimeTypeRegex: videoImageTypesRegex,
|
|
|
|
field,
|
|
|
|
maxSize: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max,
|
|
|
|
optional
|
|
|
|
})
|
2017-02-10 05:27:14 -05:00
|
|
|
}
|
|
|
|
|
2018-06-14 12:06:56 -04:00
|
|
|
function isVideoPrivacyValid (value: number) {
|
2020-01-31 10:56:52 -05:00
|
|
|
return VIDEO_PRIVACIES[value] !== undefined
|
2017-11-23 12:04:48 -05:00
|
|
|
}
|
|
|
|
|
2023-06-29 03:48:55 -04:00
|
|
|
function isVideoReplayPrivacyValid (value: number) {
|
|
|
|
return VIDEO_PRIVACIES[value] !== undefined && value !== VideoPrivacy.PASSWORD_PROTECTED
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:06:56 -04:00
|
|
|
function isScheduleVideoUpdatePrivacyValid (value: number) {
|
2019-12-12 09:47:47 -05:00
|
|
|
return value === VideoPrivacy.UNLISTED || value === VideoPrivacy.PUBLIC || value === VideoPrivacy.INTERNAL
|
2018-06-14 12:06:56 -04:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:45:23 -05:00
|
|
|
function isVideoOriginallyPublishedAtValid (value: string | null) {
|
|
|
|
return value === null || isDateValid(value)
|
|
|
|
}
|
|
|
|
|
2018-07-26 04:45:10 -04:00
|
|
|
function isVideoFileInfoHashValid (value: string | null | undefined) {
|
2017-08-25 05:36:23 -04:00
|
|
|
return exists(value) && validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.INFO_HASH)
|
|
|
|
}
|
|
|
|
|
2017-11-23 12:04:48 -05:00
|
|
|
function isVideoFileResolutionValid (value: string) {
|
|
|
|
return exists(value) && validator.isInt(value + '')
|
|
|
|
}
|
|
|
|
|
2018-06-29 10:41:29 -04:00
|
|
|
function isVideoFPSResolutionValid (value: string) {
|
|
|
|
return value === null || validator.isInt(value + '')
|
|
|
|
}
|
|
|
|
|
2017-11-23 12:04:48 -05:00
|
|
|
function isVideoFileSizeValid (value: string) {
|
|
|
|
return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.FILE_SIZE)
|
2017-11-23 11:53:38 -05:00
|
|
|
}
|
|
|
|
|
2018-08-06 11:13:39 -04:00
|
|
|
function isVideoMagnetUriValid (value: string) {
|
|
|
|
if (!exists(value)) return false
|
|
|
|
|
2023-05-22 11:04:39 -04:00
|
|
|
const parsed = magnetUriDecode(value)
|
2018-08-06 11:13:39 -04:00
|
|
|
return parsed && isVideoFileInfoHashValid(parsed.infoHash)
|
|
|
|
}
|
|
|
|
|
2023-06-29 03:48:55 -04:00
|
|
|
function isPasswordValid (password: string) {
|
|
|
|
return password.length >= CONSTRAINTS_FIELDS.VIDEO_PASSWORD.LENGTH.min &&
|
|
|
|
password.length < CONSTRAINTS_FIELDS.VIDEO_PASSWORD.LENGTH.max
|
|
|
|
}
|
|
|
|
|
|
|
|
function isValidPasswordProtectedPrivacy (req: Request, res: Response) {
|
|
|
|
const fail = (message: string) => {
|
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.BAD_REQUEST_400,
|
|
|
|
message
|
|
|
|
})
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
let privacy: VideoPrivacy
|
|
|
|
const video = getVideoWithAttributes(res)
|
|
|
|
|
|
|
|
if (exists(req.body?.privacy)) privacy = req.body.privacy
|
|
|
|
else if (exists(video?.privacy)) privacy = video.privacy
|
|
|
|
|
|
|
|
if (privacy !== VideoPrivacy.PASSWORD_PROTECTED) return true
|
|
|
|
|
|
|
|
if (!exists(req.body.videoPasswords) && !exists(req.body.passwords)) return fail('Video passwords are missing.')
|
|
|
|
|
|
|
|
const passwords = req.body.videoPasswords || req.body.passwords
|
|
|
|
|
|
|
|
if (passwords.length === 0) return fail('At least one video password is required.')
|
|
|
|
|
|
|
|
if (new Set(passwords).size !== passwords.length) return fail('Duplicate video passwords are not allowed.')
|
|
|
|
|
|
|
|
for (const password of passwords) {
|
|
|
|
if (typeof password !== 'string') {
|
|
|
|
return fail('Video password should be a string.')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isPasswordValid(password)) {
|
|
|
|
return fail('Invalid video password. Password length should be at least 2 characters and no more than 100 characters.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-01-04 14:59:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
|
|
|
isVideoCategoryValid,
|
|
|
|
isVideoLicenceValid,
|
|
|
|
isVideoLanguageValid,
|
|
|
|
isVideoDescriptionValid,
|
2017-08-25 05:36:23 -04:00
|
|
|
isVideoFileInfoHashValid,
|
2017-05-15 16:22:03 -04:00
|
|
|
isVideoNameValid,
|
2022-02-11 04:51:33 -05:00
|
|
|
areVideoTagsValid,
|
2018-06-29 10:41:29 -04:00
|
|
|
isVideoFPSResolutionValid,
|
2018-06-14 12:06:56 -04:00
|
|
|
isScheduleVideoUpdatePrivacyValid,
|
2019-01-12 08:45:23 -05:00
|
|
|
isVideoOriginallyPublishedAtValid,
|
2018-08-06 11:13:39 -04:00
|
|
|
isVideoMagnetUriValid,
|
2018-06-12 14:04:58 -04:00
|
|
|
isVideoStateValid,
|
2021-10-27 08:37:04 -04:00
|
|
|
isVideoIncludeValid,
|
2017-05-15 16:22:03 -04:00
|
|
|
isVideoViewsValid,
|
|
|
|
isVideoRatingTypeValid,
|
2018-12-11 08:52:50 -05:00
|
|
|
isVideoFileExtnameValid,
|
2020-12-08 15:16:10 -05:00
|
|
|
isVideoFileMimeTypeValid,
|
2017-11-15 10:28:35 -05:00
|
|
|
isVideoDurationValid,
|
2017-11-10 08:34:45 -05:00
|
|
|
isVideoTagValid,
|
2017-11-23 11:53:38 -05:00
|
|
|
isVideoPrivacyValid,
|
2023-06-29 03:48:55 -04:00
|
|
|
isVideoReplayPrivacyValid,
|
2017-11-23 12:04:48 -05:00
|
|
|
isVideoFileResolutionValid,
|
|
|
|
isVideoFileSizeValid,
|
2022-02-11 04:51:33 -05:00
|
|
|
isVideoImageValid,
|
2018-10-10 05:46:50 -04:00
|
|
|
isVideoSupportValid,
|
2023-06-29 03:48:55 -04:00
|
|
|
isVideoFilterValid,
|
|
|
|
isPasswordValid,
|
|
|
|
isValidPasswordProtectedPrivacy
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|