1 import express from 'express'
2 import { body, header, param, query, ValidationChain } from 'express-validator'
3 import { isTestInstance } from '@server/helpers/core-utils'
4 import { getResumableUploadPath } from '@server/helpers/upload'
5 import { Redis } from '@server/lib/redis'
6 import { isAbleToUploadVideo } from '@server/lib/user'
7 import { getServerActor } from '@server/models/application/application'
8 import { ExpressPromiseHandler } from '@server/types/express-handler'
9 import { MUserAccountId, MVideoFullLight } from '@server/types/models'
10 import { getAllPrivacies } from '@shared/core-utils'
11 import { HttpStatusCode, ServerErrorCode, UserRight, VideoInclude, VideoPrivacy } from '@shared/models'
23 } from '../../../helpers/custom-validators/misc'
24 import { isBooleanBothQueryValid, isNumberArray, isStringArray } from '../../../helpers/custom-validators/search'
26 isScheduleVideoUpdatePrivacyValid,
28 isVideoDescriptionValid,
29 isVideoFileMimeTypeValid,
37 isVideoOriginallyPublishedAtValid,
41 } from '../../../helpers/custom-validators/videos'
42 import { cleanUpReqFiles } from '../../../helpers/express-utils'
43 import { getDurationFromVideoFile } from '../../../helpers/ffprobe-utils'
44 import { logger } from '../../../helpers/logger'
45 import { deleteFileAndCatch } from '../../../helpers/utils'
46 import { getVideoWithAttributes } from '../../../helpers/video'
47 import { CONFIG } from '../../../initializers/config'
48 import { CONSTRAINTS_FIELDS, OVERVIEWS } from '../../../initializers/constants'
49 import { isLocalVideoAccepted } from '../../../lib/moderation'
50 import { Hooks } from '../../../lib/plugins/hooks'
51 import { VideoModel } from '../../../models/video/video'
54 checkCanSeePrivateVideo,
55 checkUserCanManageVideo,
56 doesVideoChannelOfAccountExist,
58 doesVideoFileOfVideoExist,
62 const videosAddLegacyValidator = getCommonVideoEditAttributes().concat([
64 .custom((value, { req }) => isFileFieldValid(req.files, 'videofile'))
65 .withMessage('Should have a file'),
68 .custom(isVideoNameValid).withMessage(
69 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
72 .customSanitizer(toIntOrNull)
73 .custom(isIdValid).withMessage('Should have correct video channel id'),
75 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
76 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
78 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
80 const videoFile: express.VideoUploadFile = req.files['videofile'][0]
81 const user = res.locals.oauth.token.User
83 if (!await commonVideoChecksPass({ req, res, user, videoFileSize: videoFile.size, files: req.files })) {
84 return cleanUpReqFiles(req)
88 if (!videoFile.duration) await addDurationToVideo(videoFile)
90 logger.error('Invalid input file in videosAddLegacyValidator.', { err })
93 status: HttpStatusCode.UNPROCESSABLE_ENTITY_422,
94 message: 'Video file unreadable.'
96 return cleanUpReqFiles(req)
99 if (!await isVideoAccepted(req, res, videoFile)) return cleanUpReqFiles(req)
106 * Gets called after the last PUT request
108 const videosAddResumableValidator = [
109 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
110 const user = res.locals.oauth.token.User
111 const body: express.CustomUploadXFile<express.UploadXFileMetadata> = req.body
112 const file = { ...body, duration: undefined, path: getResumableUploadPath(body.name), filename: body.metadata.filename }
113 const cleanup = () => deleteFileAndCatch(file.path)
115 const uploadId = req.query.upload_id
116 const sessionExists = await Redis.Instance.doesUploadSessionExist(uploadId)
119 const sessionResponse = await Redis.Instance.getUploadSession(uploadId)
121 if (!sessionResponse) {
122 res.setHeader('Retry-After', 300) // ask to retry after 5 min, knowing the upload_id is kept for up to 15 min after completion
125 status: HttpStatusCode.SERVICE_UNAVAILABLE_503,
126 message: 'The upload is already being processed'
130 if (isTestInstance()) {
131 res.setHeader('x-resumable-upload-cached', 'true')
134 return res.json(sessionResponse)
137 await Redis.Instance.setUploadSession(uploadId)
139 if (!await doesVideoChannelOfAccountExist(file.metadata.channelId, user, res)) return cleanup()
142 if (!file.duration) await addDurationToVideo(file)
144 logger.error('Invalid input file in videosAddResumableValidator.', { err })
147 status: HttpStatusCode.UNPROCESSABLE_ENTITY_422,
148 message: 'Video file unreadable.'
153 if (!await isVideoAccepted(req, res, file)) return cleanup()
155 res.locals.videoFileResumable = file
162 * File is created in POST initialisation, and its body is saved as a 'metadata' field is saved by uploadx for later use.
163 * see https://github.com/kukhariev/node-uploadx/blob/dc9fb4a8ac5a6f481902588e93062f591ec6ef03/packages/core/src/handlers/uploadx.ts
165 * Uploadx doesn't use next() until the upload completes, so this middleware has to be placed before uploadx
166 * see https://github.com/kukhariev/node-uploadx/blob/dc9fb4a8ac5a6f481902588e93062f591ec6ef03/packages/core/src/handlers/base-handler.ts
169 const videosAddResumableInitValidator = getCommonVideoEditAttributes().concat([
173 .withMessage('Should have a valid filename'),
176 .custom(isVideoNameValid).withMessage(
177 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
180 .customSanitizer(toIntOrNull)
181 .custom(isIdValid).withMessage('Should have correct video channel id'),
183 header('x-upload-content-length')
186 .withMessage('Should specify the file length'),
187 header('x-upload-content-type')
190 .withMessage('Should specify the file mimetype'),
192 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
193 const videoFileMetadata = {
194 mimetype: req.headers['x-upload-content-type'] as string,
195 size: +req.headers['x-upload-content-length'],
196 originalname: req.body.filename
199 const user = res.locals.oauth.token.User
200 const cleanup = () => cleanUpReqFiles(req)
202 logger.debug('Checking videosAddResumableInitValidator parameters and headers', {
203 parameters: req.body,
204 headers: req.headers,
208 if (areValidationErrors(req, res)) return cleanup()
210 const files = { videofile: [ videoFileMetadata ] }
211 if (!await commonVideoChecksPass({ req, res, user, videoFileSize: videoFileMetadata.size, files })) return cleanup()
213 // multer required unsetting the Content-Type, now we can set it for node-uploadx
214 req.headers['content-type'] = 'application/json; charset=utf-8'
215 // place previewfile in metadata so that uploadx saves it in .META
216 if (req.files?.['previewfile']) req.body.previewfile = req.files['previewfile']
222 const videosUpdateValidator = getCommonVideoEditAttributes().concat([
223 isValidVideoIdParam('id'),
228 .custom(isVideoNameValid).withMessage(
229 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
233 .customSanitizer(toIntOrNull)
234 .custom(isIdValid).withMessage('Should have correct video channel id'),
236 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
237 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
239 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
240 if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
241 if (!await doesVideoExist(req.params.id, res)) return cleanUpReqFiles(req)
243 // Check if the user who did the request is able to update the video
244 const user = res.locals.oauth.token.User
245 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
247 if (req.body.channelId && !await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
253 async function checkVideoFollowConstraints (req: express.Request, res: express.Response, next: express.NextFunction) {
254 const video = getVideoWithAttributes(res)
256 // Anybody can watch local videos
257 if (video.isOwned() === true) return next()
260 if (res.locals.oauth) {
261 // Users can search or watch remote videos
262 if (CONFIG.SEARCH.REMOTE_URI.USERS === true) return next()
265 // Anybody can search or watch remote videos
266 if (CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true) return next()
268 // Check our instance follows an actor that shared this video
269 const serverActor = await getServerActor()
270 if (await VideoModel.checkVideoHasInstanceFollow(video.id, serverActor.id) === true) return next()
273 status: HttpStatusCode.FORBIDDEN_403,
274 message: 'Cannot get this video regarding follow constraints',
275 type: ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS,
282 const videosCustomGetValidator = (
283 fetchType: 'for-api' | 'all' | 'only-video' | 'only-immutable-attributes',
284 authenticateInQuery = false
287 isValidVideoIdParam('id'),
289 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
290 logger.debug('Checking videosGet parameters', { parameters: req.params })
292 if (areValidationErrors(req, res)) return
293 if (!await doesVideoExist(req.params.id, res, fetchType)) return
295 // Controllers does not need to check video rights
296 if (fetchType === 'only-immutable-attributes') return next()
298 const video = getVideoWithAttributes(res) as MVideoFullLight
300 // Video private or blacklisted
301 if (video.requiresAuth()) {
302 if (await checkCanSeePrivateVideo(req, res, video, authenticateInQuery)) return next()
305 status: HttpStatusCode.FORBIDDEN_403,
306 message: 'Cannot get this private/internal or blocklisted video'
310 // Video is public, anyone can access it
311 if (video.privacy === VideoPrivacy.PUBLIC) return next()
313 // Video is unlisted, check we used the uuid to fetch it
314 if (video.privacy === VideoPrivacy.UNLISTED) {
315 if (isUUIDValid(req.params.id)) return next()
317 // Don't leak this unlisted video
319 status: HttpStatusCode.NOT_FOUND_404,
320 message: 'Video not found'
327 const videosGetValidator = videosCustomGetValidator('all')
328 const videosDownloadValidator = videosCustomGetValidator('all', true)
330 const videoFileMetadataGetValidator = getCommonVideoEditAttributes().concat([
331 isValidVideoIdParam('id'),
334 .custom(isIdValid).not().isEmpty().withMessage('Should have a valid videoFileId'),
336 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
337 logger.debug('Checking videoFileMetadataGet parameters', { parameters: req.params })
339 if (areValidationErrors(req, res)) return
340 if (!await doesVideoFileOfVideoExist(+req.params.videoFileId, req.params.id, res)) return
346 const videosRemoveValidator = [
347 isValidVideoIdParam('id'),
349 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
350 logger.debug('Checking videosRemove parameters', { parameters: req.params })
352 if (areValidationErrors(req, res)) return
353 if (!await doesVideoExist(req.params.id, res)) return
355 // Check if the user who did the request is able to delete the video
356 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.REMOVE_ANY_VIDEO, res)) return
362 const videosOverviewValidator = [
365 .isInt({ min: 1, max: OVERVIEWS.VIDEOS.SAMPLES_COUNT })
366 .withMessage('Should have a valid pagination'),
368 (req: express.Request, res: express.Response, next: express.NextFunction) => {
369 if (areValidationErrors(req, res)) return
375 function getCommonVideoEditAttributes () {
377 body('thumbnailfile')
378 .custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
379 'This thumbnail file is not supported or too large. Please, make sure it is of the following type: ' +
380 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
383 .custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage(
384 'This preview file is not supported or too large. Please, make sure it is of the following type: ' +
385 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
390 .customSanitizer(toIntOrNull)
391 .custom(isVideoCategoryValid).withMessage('Should have a valid category'),
394 .customSanitizer(toIntOrNull)
395 .custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
398 .customSanitizer(toValueOrNull)
399 .custom(isVideoLanguageValid).withMessage('Should have a valid language'),
402 .customSanitizer(toBooleanOrNull)
403 .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
404 body('waitTranscoding')
406 .customSanitizer(toBooleanOrNull)
407 .custom(isBooleanValid).withMessage('Should have a valid wait transcoding attribute'),
410 .customSanitizer(toValueOrNull)
411 .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
414 .customSanitizer(toValueOrNull)
415 .custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
418 .customSanitizer(toValueOrNull)
419 .custom(isVideoSupportValid).withMessage('Should have a valid support text'),
422 .customSanitizer(toValueOrNull)
423 .custom(isVideoTagsValid)
425 `Should have an array of up to ${CONSTRAINTS_FIELDS.VIDEOS.TAGS.max} tags between ` +
426 `${CONSTRAINTS_FIELDS.VIDEOS.TAG.min} and ${CONSTRAINTS_FIELDS.VIDEOS.TAG.max} characters each`
428 body('commentsEnabled')
430 .customSanitizer(toBooleanOrNull)
431 .custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
432 body('downloadEnabled')
434 .customSanitizer(toBooleanOrNull)
435 .custom(isBooleanValid).withMessage('Should have downloading enabled boolean'),
436 body('originallyPublishedAt')
438 .customSanitizer(toValueOrNull)
439 .custom(isVideoOriginallyPublishedAtValid).withMessage('Should have a valid original publication date'),
440 body('scheduleUpdate')
442 .customSanitizer(toValueOrNull),
443 body('scheduleUpdate.updateAt')
445 .custom(isDateValid).withMessage('Should have a schedule update date that conforms to ISO 8601'),
446 body('scheduleUpdate.privacy')
448 .customSanitizer(toIntOrNull)
449 .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy')
450 ] as (ValidationChain | ExpressPromiseHandler)[]
453 const commonVideosFiltersValidator = [
454 query('categoryOneOf')
456 .customSanitizer(toArray)
457 .custom(isNumberArray).withMessage('Should have a valid one of category array'),
458 query('licenceOneOf')
460 .customSanitizer(toArray)
461 .custom(isNumberArray).withMessage('Should have a valid one of licence array'),
462 query('languageOneOf')
464 .customSanitizer(toArray)
465 .custom(isStringArray).withMessage('Should have a valid one of language array'),
466 query('privacyOneOf')
468 .customSanitizer(toArray)
469 .custom(isNumberArray).withMessage('Should have a valid one of privacy array'),
472 .customSanitizer(toArray)
473 .custom(isStringArray).withMessage('Should have a valid one of tags array'),
476 .customSanitizer(toArray)
477 .custom(isStringArray).withMessage('Should have a valid all of tags array'),
480 .custom(isBooleanBothQueryValid).withMessage('Should have a valid NSFW attribute'),
483 .customSanitizer(toBooleanOrNull)
484 .custom(isBooleanValid).withMessage('Should have a valid live boolean'),
487 .custom(isVideoFilterValid).withMessage('Should have a valid filter attribute'),
490 .custom(isVideoIncludeValid).withMessage('Should have a valid include attribute'),
493 .customSanitizer(toBooleanOrNull)
494 .custom(isBooleanValid).withMessage('Should have a valid local boolean'),
497 .customSanitizer(toBooleanOrNull)
498 .custom(isBooleanValid).withMessage('Should have a valid has hls boolean'),
499 query('hasWebtorrentFiles')
501 .customSanitizer(toBooleanOrNull)
502 .custom(isBooleanValid).withMessage('Should have a valid has webtorrent boolean'),
505 .customSanitizer(toBooleanOrNull)
506 .custom(isBooleanValid).withMessage('Should have a valid skip count boolean'),
509 .custom(exists).withMessage('Should have a valid search'),
511 (req: express.Request, res: express.Response, next: express.NextFunction) => {
512 logger.debug('Checking commons video filters query', { parameters: req.query })
514 if (areValidationErrors(req, res)) return
516 // FIXME: deprecated in 4.0, to remove
518 if (req.query.filter === 'all-local') {
519 req.query.include = VideoInclude.NOT_PUBLISHED_STATE
520 req.query.isLocal = true
521 req.query.privacyOneOf = getAllPrivacies()
522 } else if (req.query.filter === 'all') {
523 req.query.include = VideoInclude.NOT_PUBLISHED_STATE
524 req.query.privacyOneOf = getAllPrivacies()
525 } else if (req.query.filter === 'local') {
526 req.query.isLocal = true
529 req.query.filter = undefined
532 const user = res.locals.oauth?.token.User
534 if ((!user || user.hasRight(UserRight.SEE_ALL_VIDEOS) !== true)) {
535 if (req.query.include || req.query.privacyOneOf) {
537 status: HttpStatusCode.UNAUTHORIZED_401,
538 message: 'You are not allowed to see all videos.'
547 // ---------------------------------------------------------------------------
550 videosAddLegacyValidator,
551 videosAddResumableValidator,
552 videosAddResumableInitValidator,
554 videosUpdateValidator,
556 videoFileMetadataGetValidator,
557 videosDownloadValidator,
558 checkVideoFollowConstraints,
559 videosCustomGetValidator,
560 videosRemoveValidator,
562 getCommonVideoEditAttributes,
564 commonVideosFiltersValidator,
566 videosOverviewValidator
569 // ---------------------------------------------------------------------------
571 function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) {
572 if (req.body.scheduleUpdate) {
573 if (!req.body.scheduleUpdate.updateAt) {
574 logger.warn('Invalid parameters: scheduleUpdate.updateAt is mandatory.')
576 res.fail({ message: 'Schedule update at is mandatory.' })
584 async function commonVideoChecksPass (parameters: {
586 res: express.Response
588 videoFileSize: number
589 files: express.UploadFilesForCheck
590 }): Promise<boolean> {
591 const { req, res, user, videoFileSize, files } = parameters
593 if (areErrorsInScheduleUpdate(req, res)) return false
595 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return false
597 if (!isVideoFileMimeTypeValid(files)) {
599 status: HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415,
600 message: 'This file is not supported. Please, make sure it is of the following type: ' +
601 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
606 if (!isVideoFileSizeValid(videoFileSize.toString())) {
608 status: HttpStatusCode.PAYLOAD_TOO_LARGE_413,
609 message: 'This file is too large. It exceeds the maximum file size authorized.',
610 type: ServerErrorCode.MAX_FILE_SIZE_REACHED
615 if (await isAbleToUploadVideo(user.id, videoFileSize) === false) {
617 status: HttpStatusCode.PAYLOAD_TOO_LARGE_413,
618 message: 'The user video quota is exceeded with this video.',
619 type: ServerErrorCode.QUOTA_REACHED
627 export async function isVideoAccepted (
628 req: express.Request,
629 res: express.Response,
630 videoFile: express.VideoUploadFile
632 // Check we accept this video
633 const acceptParameters = {
636 user: res.locals.oauth.token.User
638 const acceptedResult = await Hooks.wrapFun(
639 isLocalVideoAccepted,
641 'filter:api.video.upload.accept.result'
644 if (!acceptedResult || acceptedResult.accepted !== true) {
645 logger.info('Refused local video.', { acceptedResult, acceptParameters })
647 status: HttpStatusCode.FORBIDDEN_403,
648 message: acceptedResult.errorMessage || 'Refused local video'
656 async function addDurationToVideo (videoFile: { path: string, duration?: number }) {
657 const duration: number = await getDurationFromVideoFile(videoFile.path)
659 if (isNaN(duration)) throw new Error(`Couldn't get video duration`)
661 videoFile.duration = duration