1 import express from 'express'
2 import { body, header, param, query, ValidationChain } from 'express-validator'
3 import { getResumableUploadPath } from '@server/helpers/upload'
4 import { isAbleToUploadVideo } from '@server/lib/user'
5 import { getServerActor } from '@server/models/application/application'
6 import { ExpressPromiseHandler } from '@server/types/express'
7 import { MUserAccountId, MVideoFullLight } from '@server/types/models'
8 import { ServerErrorCode, UserRight, VideoPrivacy } from '../../../../shared'
9 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
21 } from '../../../helpers/custom-validators/misc'
22 import { isBooleanBothQueryValid, isNumberArray, isStringArray } from '../../../helpers/custom-validators/search'
24 isScheduleVideoUpdatePrivacyValid,
26 isVideoDescriptionValid,
27 isVideoFileMimeTypeValid,
34 isVideoOriginallyPublishedAtValid,
38 } from '../../../helpers/custom-validators/videos'
39 import { cleanUpReqFiles } from '../../../helpers/express-utils'
40 import { getDurationFromVideoFile } from '../../../helpers/ffprobe-utils'
41 import { logger } from '../../../helpers/logger'
42 import { deleteFileAndCatch } from '../../../helpers/utils'
43 import { getVideoWithAttributes } from '../../../helpers/video'
44 import { CONFIG } from '../../../initializers/config'
45 import { CONSTRAINTS_FIELDS, OVERVIEWS } from '../../../initializers/constants'
46 import { isLocalVideoAccepted } from '../../../lib/moderation'
47 import { Hooks } from '../../../lib/plugins/hooks'
48 import { VideoModel } from '../../../models/video/video'
49 import { authenticatePromiseIfNeeded } from '../../auth'
52 checkUserCanManageVideo,
53 doesVideoChannelOfAccountExist,
55 doesVideoFileOfVideoExist,
59 const videosAddLegacyValidator = getCommonVideoEditAttributes().concat([
61 .custom((value, { req }) => isFileFieldValid(req.files, 'videofile'))
62 .withMessage('Should have a file'),
65 .custom(isVideoNameValid).withMessage(
66 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
69 .customSanitizer(toIntOrNull)
70 .custom(isIdValid).withMessage('Should have correct video channel id'),
72 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
73 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
75 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
77 const videoFile: express.VideoUploadFile = req.files['videofile'][0]
78 const user = res.locals.oauth.token.User
80 if (!await commonVideoChecksPass({ req, res, user, videoFileSize: videoFile.size, files: req.files })) {
81 return cleanUpReqFiles(req)
85 if (!videoFile.duration) await addDurationToVideo(videoFile)
87 logger.error('Invalid input file in videosAddLegacyValidator.', { err })
90 status: HttpStatusCode.UNPROCESSABLE_ENTITY_422,
91 message: 'Video file unreadable.'
93 return cleanUpReqFiles(req)
96 if (!await isVideoAccepted(req, res, videoFile)) return cleanUpReqFiles(req)
103 * Gets called after the last PUT request
105 const videosAddResumableValidator = [
106 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
107 const user = res.locals.oauth.token.User
109 const body: express.CustomUploadXFile<express.UploadXFileMetadata> = req.body
110 const file = { ...body, duration: undefined, path: getResumableUploadPath(body.id), filename: body.metadata.filename }
112 const cleanup = () => deleteFileAndCatch(file.path)
114 if (!await doesVideoChannelOfAccountExist(file.metadata.channelId, user, res)) return cleanup()
117 if (!file.duration) await addDurationToVideo(file)
119 logger.error('Invalid input file in videosAddResumableValidator.', { err })
122 status: HttpStatusCode.UNPROCESSABLE_ENTITY_422,
123 message: 'Video file unreadable.'
128 if (!await isVideoAccepted(req, res, file)) return cleanup()
130 res.locals.videoFileResumable = file
137 * File is created in POST initialisation, and its body is saved as a 'metadata' field is saved by uploadx for later use.
138 * see https://github.com/kukhariev/node-uploadx/blob/dc9fb4a8ac5a6f481902588e93062f591ec6ef03/packages/core/src/handlers/uploadx.ts
140 * Uploadx doesn't use next() until the upload completes, so this middleware has to be placed before uploadx
141 * see https://github.com/kukhariev/node-uploadx/blob/dc9fb4a8ac5a6f481902588e93062f591ec6ef03/packages/core/src/handlers/base-handler.ts
144 const videosAddResumableInitValidator = getCommonVideoEditAttributes().concat([
148 .withMessage('Should have a valid filename'),
151 .custom(isVideoNameValid).withMessage(
152 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
155 .customSanitizer(toIntOrNull)
156 .custom(isIdValid).withMessage('Should have correct video channel id'),
158 header('x-upload-content-length')
161 .withMessage('Should specify the file length'),
162 header('x-upload-content-type')
165 .withMessage('Should specify the file mimetype'),
167 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
168 const videoFileMetadata = {
169 mimetype: req.headers['x-upload-content-type'] as string,
170 size: +req.headers['x-upload-content-length'],
171 originalname: req.body.name
174 const user = res.locals.oauth.token.User
175 const cleanup = () => cleanUpReqFiles(req)
177 logger.debug('Checking videosAddResumableInitValidator parameters and headers', {
178 parameters: req.body,
179 headers: req.headers,
183 if (areValidationErrors(req, res)) return cleanup()
185 const files = { videofile: [ videoFileMetadata ] }
186 if (!await commonVideoChecksPass({ req, res, user, videoFileSize: videoFileMetadata.size, files })) return cleanup()
188 // multer required unsetting the Content-Type, now we can set it for node-uploadx
189 req.headers['content-type'] = 'application/json; charset=utf-8'
190 // place previewfile in metadata so that uploadx saves it in .META
191 if (req.files?.['previewfile']) req.body.previewfile = req.files['previewfile']
197 const videosUpdateValidator = getCommonVideoEditAttributes().concat([
198 isValidVideoIdParam('id'),
203 .custom(isVideoNameValid).withMessage(
204 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
208 .customSanitizer(toIntOrNull)
209 .custom(isIdValid).withMessage('Should have correct video channel id'),
211 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
212 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
214 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
215 if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
216 if (!await doesVideoExist(req.params.id, res)) return cleanUpReqFiles(req)
218 // Check if the user who did the request is able to update the video
219 const user = res.locals.oauth.token.User
220 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
222 if (req.body.channelId && !await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
228 async function checkVideoFollowConstraints (req: express.Request, res: express.Response, next: express.NextFunction) {
229 const video = getVideoWithAttributes(res)
231 // Anybody can watch local videos
232 if (video.isOwned() === true) return next()
235 if (res.locals.oauth) {
236 // Users can search or watch remote videos
237 if (CONFIG.SEARCH.REMOTE_URI.USERS === true) return next()
240 // Anybody can search or watch remote videos
241 if (CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true) return next()
243 // Check our instance follows an actor that shared this video
244 const serverActor = await getServerActor()
245 if (await VideoModel.checkVideoHasInstanceFollow(video.id, serverActor.id) === true) return next()
248 status: HttpStatusCode.FORBIDDEN_403,
249 message: 'Cannot get this video regarding follow constraints',
250 type: ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS,
257 const videosCustomGetValidator = (
258 fetchType: 'for-api' | 'all' | 'only-video' | 'only-immutable-attributes',
259 authenticateInQuery = false
262 isValidVideoIdParam('id'),
264 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
265 logger.debug('Checking videosGet parameters', { parameters: req.params })
267 if (areValidationErrors(req, res)) return
268 if (!await doesVideoExist(req.params.id, res, fetchType)) return
270 // Controllers does not need to check video rights
271 if (fetchType === 'only-immutable-attributes') return next()
273 const video = getVideoWithAttributes(res) as MVideoFullLight
275 // Video private or blacklisted
276 if (video.requiresAuth()) {
277 await authenticatePromiseIfNeeded(req, res, authenticateInQuery)
279 const user = res.locals.oauth ? res.locals.oauth.token.User : null
281 // Only the owner or a user that have blocklist rights can see the video
282 if (!user || !user.canGetVideo(video)) {
284 status: HttpStatusCode.FORBIDDEN_403,
285 message: 'Cannot get this private/internal or blocklisted video'
292 // Video is public, anyone can access it
293 if (video.privacy === VideoPrivacy.PUBLIC) return next()
295 // Video is unlisted, check we used the uuid to fetch it
296 if (video.privacy === VideoPrivacy.UNLISTED) {
297 if (isUUIDValid(req.params.id)) return next()
299 // Don't leak this unlisted video
301 status: HttpStatusCode.NOT_FOUND_404,
302 message: 'Video not found'
309 const videosGetValidator = videosCustomGetValidator('all')
310 const videosDownloadValidator = videosCustomGetValidator('all', true)
312 const videoFileMetadataGetValidator = getCommonVideoEditAttributes().concat([
313 isValidVideoIdParam('id'),
316 .custom(isIdValid).not().isEmpty().withMessage('Should have a valid videoFileId'),
318 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
319 logger.debug('Checking videoFileMetadataGet parameters', { parameters: req.params })
321 if (areValidationErrors(req, res)) return
322 if (!await doesVideoFileOfVideoExist(+req.params.videoFileId, req.params.id, res)) return
328 const videosRemoveValidator = [
329 isValidVideoIdParam('id'),
331 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
332 logger.debug('Checking videosRemove parameters', { parameters: req.params })
334 if (areValidationErrors(req, res)) return
335 if (!await doesVideoExist(req.params.id, res)) return
337 // Check if the user who did the request is able to delete the video
338 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.REMOVE_ANY_VIDEO, res)) return
344 const videosOverviewValidator = [
347 .isInt({ min: 1, max: OVERVIEWS.VIDEOS.SAMPLES_COUNT })
348 .withMessage('Should have a valid pagination'),
350 (req: express.Request, res: express.Response, next: express.NextFunction) => {
351 if (areValidationErrors(req, res)) return
357 function getCommonVideoEditAttributes () {
359 body('thumbnailfile')
360 .custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
361 'This thumbnail file is not supported or too large. Please, make sure it is of the following type: ' +
362 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
365 .custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage(
366 'This preview file is not supported or too large. Please, make sure it is of the following type: ' +
367 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
372 .customSanitizer(toIntOrNull)
373 .custom(isVideoCategoryValid).withMessage('Should have a valid category'),
376 .customSanitizer(toIntOrNull)
377 .custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
380 .customSanitizer(toValueOrNull)
381 .custom(isVideoLanguageValid).withMessage('Should have a valid language'),
384 .customSanitizer(toBooleanOrNull)
385 .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
386 body('waitTranscoding')
388 .customSanitizer(toBooleanOrNull)
389 .custom(isBooleanValid).withMessage('Should have a valid wait transcoding attribute'),
392 .customSanitizer(toValueOrNull)
393 .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
396 .customSanitizer(toValueOrNull)
397 .custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
400 .customSanitizer(toValueOrNull)
401 .custom(isVideoSupportValid).withMessage('Should have a valid support text'),
404 .customSanitizer(toValueOrNull)
405 .custom(isVideoTagsValid)
407 `Should have an array of up to ${CONSTRAINTS_FIELDS.VIDEOS.TAGS.max} tags between ` +
408 `${CONSTRAINTS_FIELDS.VIDEOS.TAG.min} and ${CONSTRAINTS_FIELDS.VIDEOS.TAG.max} characters each`
410 body('commentsEnabled')
412 .customSanitizer(toBooleanOrNull)
413 .custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
414 body('downloadEnabled')
416 .customSanitizer(toBooleanOrNull)
417 .custom(isBooleanValid).withMessage('Should have downloading enabled boolean'),
418 body('originallyPublishedAt')
420 .customSanitizer(toValueOrNull)
421 .custom(isVideoOriginallyPublishedAtValid).withMessage('Should have a valid original publication date'),
422 body('scheduleUpdate')
424 .customSanitizer(toValueOrNull),
425 body('scheduleUpdate.updateAt')
427 .custom(isDateValid).withMessage('Should have a schedule update date that conforms to ISO 8601'),
428 body('scheduleUpdate.privacy')
430 .customSanitizer(toIntOrNull)
431 .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy')
432 ] as (ValidationChain | ExpressPromiseHandler)[]
435 const commonVideosFiltersValidator = [
436 query('categoryOneOf')
438 .customSanitizer(toArray)
439 .custom(isNumberArray).withMessage('Should have a valid one of category array'),
440 query('licenceOneOf')
442 .customSanitizer(toArray)
443 .custom(isNumberArray).withMessage('Should have a valid one of licence array'),
444 query('languageOneOf')
446 .customSanitizer(toArray)
447 .custom(isStringArray).withMessage('Should have a valid one of language array'),
450 .customSanitizer(toArray)
451 .custom(isStringArray).withMessage('Should have a valid one of tags array'),
454 .customSanitizer(toArray)
455 .custom(isStringArray).withMessage('Should have a valid all of tags array'),
458 .custom(isBooleanBothQueryValid).withMessage('Should have a valid NSFW attribute'),
461 .customSanitizer(toBooleanOrNull)
462 .custom(isBooleanValid).withMessage('Should have a valid live boolean'),
465 .custom(isVideoFilterValid).withMessage('Should have a valid filter attribute'),
468 .customSanitizer(toBooleanOrNull)
469 .custom(isBooleanValid).withMessage('Should have a valid skip count boolean'),
472 .custom(exists).withMessage('Should have a valid search'),
474 (req: express.Request, res: express.Response, next: express.NextFunction) => {
475 logger.debug('Checking commons video filters query', { parameters: req.query })
477 if (areValidationErrors(req, res)) return
479 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
481 (req.query.filter === 'all-local' || req.query.filter === 'all') &&
482 (!user || user.hasRight(UserRight.SEE_ALL_VIDEOS) === false)
485 status: HttpStatusCode.UNAUTHORIZED_401,
486 message: 'You are not allowed to see all local videos.'
495 // ---------------------------------------------------------------------------
498 videosAddLegacyValidator,
499 videosAddResumableValidator,
500 videosAddResumableInitValidator,
502 videosUpdateValidator,
504 videoFileMetadataGetValidator,
505 videosDownloadValidator,
506 checkVideoFollowConstraints,
507 videosCustomGetValidator,
508 videosRemoveValidator,
510 getCommonVideoEditAttributes,
512 commonVideosFiltersValidator,
514 videosOverviewValidator
517 // ---------------------------------------------------------------------------
519 function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) {
520 if (req.body.scheduleUpdate) {
521 if (!req.body.scheduleUpdate.updateAt) {
522 logger.warn('Invalid parameters: scheduleUpdate.updateAt is mandatory.')
524 res.fail({ message: 'Schedule update at is mandatory.' })
532 async function commonVideoChecksPass (parameters: {
534 res: express.Response
536 videoFileSize: number
537 files: express.UploadFilesForCheck
538 }): Promise<boolean> {
539 const { req, res, user, videoFileSize, files } = parameters
541 if (areErrorsInScheduleUpdate(req, res)) return false
543 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return false
545 if (!isVideoFileMimeTypeValid(files)) {
547 status: HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415,
548 message: 'This file is not supported. Please, make sure it is of the following type: ' +
549 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
554 if (!isVideoFileSizeValid(videoFileSize.toString())) {
556 status: HttpStatusCode.PAYLOAD_TOO_LARGE_413,
557 message: 'This file is too large. It exceeds the maximum file size authorized.',
558 type: ServerErrorCode.MAX_FILE_SIZE_REACHED
563 if (await isAbleToUploadVideo(user.id, videoFileSize) === false) {
565 status: HttpStatusCode.PAYLOAD_TOO_LARGE_413,
566 message: 'The user video quota is exceeded with this video.',
567 type: ServerErrorCode.QUOTA_REACHED
575 export async function isVideoAccepted (
576 req: express.Request,
577 res: express.Response,
578 videoFile: express.VideoUploadFile
580 // Check we accept this video
581 const acceptParameters = {
584 user: res.locals.oauth.token.User
586 const acceptedResult = await Hooks.wrapFun(
587 isLocalVideoAccepted,
589 'filter:api.video.upload.accept.result'
592 if (!acceptedResult || acceptedResult.accepted !== true) {
593 logger.info('Refused local video.', { acceptedResult, acceptParameters })
595 status: HttpStatusCode.FORBIDDEN_403,
596 message: acceptedResult.errorMessage || 'Refused local video'
604 async function addDurationToVideo (videoFile: { path: string, duration?: number }) {
605 const duration: number = await getDurationFromVideoFile(videoFile.path)
607 if (isNaN(duration)) throw new Error(`Couldn't get video duration`)
609 videoFile.duration = duration