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'
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'
52 import { authenticatePromiseIfNeeded } from '../../auth'
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)
105 const videosResumableUploadIdValidator = [
106 (req: express.Request, res: express.Response, next: express.NextFunction) => {
107 const user = res.locals.oauth.token.User
108 const uploadId = req.query.upload_id
110 if (uploadId.startsWith(user.id + '-') !== true) {
112 status: HttpStatusCode.FORBIDDEN_403,
113 message: 'You cannot send chunks in another user upload'
122 * Gets called after the last PUT request
124 const videosAddResumableValidator = [
125 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
126 const user = res.locals.oauth.token.User
127 const body: express.CustomUploadXFile<express.UploadXFileMetadata> = req.body
128 const file = { ...body, duration: undefined, path: getResumableUploadPath(body.name), filename: body.metadata.filename }
129 const cleanup = () => deleteFileAndCatch(file.path)
131 const uploadId = req.query.upload_id
132 const sessionExists = await Redis.Instance.doesUploadSessionExist(uploadId)
135 const sessionResponse = await Redis.Instance.getUploadSession(uploadId)
137 if (!sessionResponse) {
138 res.setHeader('Retry-After', 300) // ask to retry after 5 min, knowing the upload_id is kept for up to 15 min after completion
141 status: HttpStatusCode.SERVICE_UNAVAILABLE_503,
142 message: 'The upload is already being processed'
146 if (isTestInstance()) {
147 res.setHeader('x-resumable-upload-cached', 'true')
150 return res.json(sessionResponse)
153 await Redis.Instance.setUploadSession(uploadId)
155 if (!await doesVideoChannelOfAccountExist(file.metadata.channelId, user, res)) return cleanup()
158 if (!file.duration) await addDurationToVideo(file)
160 logger.error('Invalid input file in videosAddResumableValidator.', { err })
163 status: HttpStatusCode.UNPROCESSABLE_ENTITY_422,
164 message: 'Video file unreadable.'
169 if (!await isVideoAccepted(req, res, file)) return cleanup()
171 res.locals.videoFileResumable = file
178 * File is created in POST initialisation, and its body is saved as a 'metadata' field is saved by uploadx for later use.
179 * see https://github.com/kukhariev/node-uploadx/blob/dc9fb4a8ac5a6f481902588e93062f591ec6ef03/packages/core/src/handlers/uploadx.ts
181 * Uploadx doesn't use next() until the upload completes, so this middleware has to be placed before uploadx
182 * see https://github.com/kukhariev/node-uploadx/blob/dc9fb4a8ac5a6f481902588e93062f591ec6ef03/packages/core/src/handlers/base-handler.ts
185 const videosAddResumableInitValidator = getCommonVideoEditAttributes().concat([
189 .withMessage('Should have a valid filename'),
192 .custom(isVideoNameValid).withMessage(
193 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
196 .customSanitizer(toIntOrNull)
197 .custom(isIdValid).withMessage('Should have correct video channel id'),
199 header('x-upload-content-length')
202 .withMessage('Should specify the file length'),
203 header('x-upload-content-type')
206 .withMessage('Should specify the file mimetype'),
208 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
209 const videoFileMetadata = {
210 mimetype: req.headers['x-upload-content-type'] as string,
211 size: +req.headers['x-upload-content-length'],
212 originalname: req.body.filename
215 const user = res.locals.oauth.token.User
216 const cleanup = () => cleanUpReqFiles(req)
218 logger.debug('Checking videosAddResumableInitValidator parameters and headers', {
219 parameters: req.body,
220 headers: req.headers,
224 if (areValidationErrors(req, res)) return cleanup()
226 const files = { videofile: [ videoFileMetadata ] }
227 if (!await commonVideoChecksPass({ req, res, user, videoFileSize: videoFileMetadata.size, files })) return cleanup()
229 // multer required unsetting the Content-Type, now we can set it for node-uploadx
230 req.headers['content-type'] = 'application/json; charset=utf-8'
231 // place previewfile in metadata so that uploadx saves it in .META
232 if (req.files?.['previewfile']) req.body.previewfile = req.files['previewfile']
238 const videosUpdateValidator = getCommonVideoEditAttributes().concat([
239 isValidVideoIdParam('id'),
244 .custom(isVideoNameValid).withMessage(
245 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
249 .customSanitizer(toIntOrNull)
250 .custom(isIdValid).withMessage('Should have correct video channel id'),
252 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
253 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
255 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
256 if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
257 if (!await doesVideoExist(req.params.id, res)) return cleanUpReqFiles(req)
259 // Check if the user who did the request is able to update the video
260 const user = res.locals.oauth.token.User
261 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
263 if (req.body.channelId && !await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
269 async function checkVideoFollowConstraints (req: express.Request, res: express.Response, next: express.NextFunction) {
270 const video = getVideoWithAttributes(res)
272 // Anybody can watch local videos
273 if (video.isOwned() === true) return next()
276 if (res.locals.oauth) {
277 // Users can search or watch remote videos
278 if (CONFIG.SEARCH.REMOTE_URI.USERS === true) return next()
281 // Anybody can search or watch remote videos
282 if (CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true) return next()
284 // Check our instance follows an actor that shared this video
285 const serverActor = await getServerActor()
286 if (await VideoModel.checkVideoHasInstanceFollow(video.id, serverActor.id) === true) return next()
289 status: HttpStatusCode.FORBIDDEN_403,
290 message: 'Cannot get this video regarding follow constraints',
291 type: ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS,
298 const videosCustomGetValidator = (
299 fetchType: 'for-api' | 'all' | 'only-video' | 'only-immutable-attributes',
300 authenticateInQuery = false
303 isValidVideoIdParam('id'),
305 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
306 logger.debug('Checking videosGet parameters', { parameters: req.params })
308 if (areValidationErrors(req, res)) return
309 if (!await doesVideoExist(req.params.id, res, fetchType)) return
311 // Controllers does not need to check video rights
312 if (fetchType === 'only-immutable-attributes') return next()
314 const video = getVideoWithAttributes(res) as MVideoFullLight
316 // Video private or blacklisted
317 if (video.requiresAuth()) {
318 await authenticatePromiseIfNeeded(req, res, authenticateInQuery)
320 const user = res.locals.oauth ? res.locals.oauth.token.User : null
322 // Only the owner or a user that have blocklist rights can see the video
323 if (!user || !user.canGetVideo(video)) {
325 status: HttpStatusCode.FORBIDDEN_403,
326 message: 'Cannot get this private/internal or blocklisted video'
333 // Video is public, anyone can access it
334 if (video.privacy === VideoPrivacy.PUBLIC) return next()
336 // Video is unlisted, check we used the uuid to fetch it
337 if (video.privacy === VideoPrivacy.UNLISTED) {
338 if (isUUIDValid(req.params.id)) return next()
340 // Don't leak this unlisted video
342 status: HttpStatusCode.NOT_FOUND_404,
343 message: 'Video not found'
350 const videosGetValidator = videosCustomGetValidator('all')
351 const videosDownloadValidator = videosCustomGetValidator('all', true)
353 const videoFileMetadataGetValidator = getCommonVideoEditAttributes().concat([
354 isValidVideoIdParam('id'),
357 .custom(isIdValid).not().isEmpty().withMessage('Should have a valid videoFileId'),
359 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
360 logger.debug('Checking videoFileMetadataGet parameters', { parameters: req.params })
362 if (areValidationErrors(req, res)) return
363 if (!await doesVideoFileOfVideoExist(+req.params.videoFileId, req.params.id, res)) return
369 const videosRemoveValidator = [
370 isValidVideoIdParam('id'),
372 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
373 logger.debug('Checking videosRemove parameters', { parameters: req.params })
375 if (areValidationErrors(req, res)) return
376 if (!await doesVideoExist(req.params.id, res)) return
378 // Check if the user who did the request is able to delete the video
379 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.REMOVE_ANY_VIDEO, res)) return
385 const videosOverviewValidator = [
388 .isInt({ min: 1, max: OVERVIEWS.VIDEOS.SAMPLES_COUNT })
389 .withMessage('Should have a valid pagination'),
391 (req: express.Request, res: express.Response, next: express.NextFunction) => {
392 if (areValidationErrors(req, res)) return
398 function getCommonVideoEditAttributes () {
400 body('thumbnailfile')
401 .custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
402 'This thumbnail file is not supported or too large. Please, make sure it is of the following type: ' +
403 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
406 .custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage(
407 'This preview file is not supported or too large. Please, make sure it is of the following type: ' +
408 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
413 .customSanitizer(toIntOrNull)
414 .custom(isVideoCategoryValid).withMessage('Should have a valid category'),
417 .customSanitizer(toIntOrNull)
418 .custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
421 .customSanitizer(toValueOrNull)
422 .custom(isVideoLanguageValid).withMessage('Should have a valid language'),
425 .customSanitizer(toBooleanOrNull)
426 .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
427 body('waitTranscoding')
429 .customSanitizer(toBooleanOrNull)
430 .custom(isBooleanValid).withMessage('Should have a valid wait transcoding attribute'),
433 .customSanitizer(toValueOrNull)
434 .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
437 .customSanitizer(toValueOrNull)
438 .custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
441 .customSanitizer(toValueOrNull)
442 .custom(isVideoSupportValid).withMessage('Should have a valid support text'),
445 .customSanitizer(toValueOrNull)
446 .custom(isVideoTagsValid)
448 `Should have an array of up to ${CONSTRAINTS_FIELDS.VIDEOS.TAGS.max} tags between ` +
449 `${CONSTRAINTS_FIELDS.VIDEOS.TAG.min} and ${CONSTRAINTS_FIELDS.VIDEOS.TAG.max} characters each`
451 body('commentsEnabled')
453 .customSanitizer(toBooleanOrNull)
454 .custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
455 body('downloadEnabled')
457 .customSanitizer(toBooleanOrNull)
458 .custom(isBooleanValid).withMessage('Should have downloading enabled boolean'),
459 body('originallyPublishedAt')
461 .customSanitizer(toValueOrNull)
462 .custom(isVideoOriginallyPublishedAtValid).withMessage('Should have a valid original publication date'),
463 body('scheduleUpdate')
465 .customSanitizer(toValueOrNull),
466 body('scheduleUpdate.updateAt')
468 .custom(isDateValid).withMessage('Should have a schedule update date that conforms to ISO 8601'),
469 body('scheduleUpdate.privacy')
471 .customSanitizer(toIntOrNull)
472 .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy')
473 ] as (ValidationChain | ExpressPromiseHandler)[]
476 const commonVideosFiltersValidator = [
477 query('categoryOneOf')
479 .customSanitizer(toArray)
480 .custom(isNumberArray).withMessage('Should have a valid one of category array'),
481 query('licenceOneOf')
483 .customSanitizer(toArray)
484 .custom(isNumberArray).withMessage('Should have a valid one of licence array'),
485 query('languageOneOf')
487 .customSanitizer(toArray)
488 .custom(isStringArray).withMessage('Should have a valid one of language array'),
489 query('privacyOneOf')
491 .customSanitizer(toArray)
492 .custom(isNumberArray).withMessage('Should have a valid one of privacy array'),
495 .customSanitizer(toArray)
496 .custom(isStringArray).withMessage('Should have a valid one of tags array'),
499 .customSanitizer(toArray)
500 .custom(isStringArray).withMessage('Should have a valid all of tags array'),
503 .custom(isBooleanBothQueryValid).withMessage('Should have a valid NSFW attribute'),
506 .customSanitizer(toBooleanOrNull)
507 .custom(isBooleanValid).withMessage('Should have a valid live boolean'),
510 .custom(isVideoFilterValid).withMessage('Should have a valid filter attribute'),
513 .custom(isVideoIncludeValid).withMessage('Should have a valid include attribute'),
516 .customSanitizer(toBooleanOrNull)
517 .custom(isBooleanValid).withMessage('Should have a valid local boolean'),
520 .customSanitizer(toBooleanOrNull)
521 .custom(isBooleanValid).withMessage('Should have a valid has hls boolean'),
522 query('hasWebtorrentFiles')
524 .customSanitizer(toBooleanOrNull)
525 .custom(isBooleanValid).withMessage('Should have a valid has webtorrent boolean'),
528 .customSanitizer(toBooleanOrNull)
529 .custom(isBooleanValid).withMessage('Should have a valid skip count boolean'),
532 .custom(exists).withMessage('Should have a valid search'),
534 (req: express.Request, res: express.Response, next: express.NextFunction) => {
535 logger.debug('Checking commons video filters query', { parameters: req.query })
537 if (areValidationErrors(req, res)) return
539 // FIXME: deprecated in 4.0, to remove
541 if (req.query.filter === 'all-local') {
542 req.query.include = VideoInclude.NOT_PUBLISHED_STATE
543 req.query.isLocal = true
544 req.query.privacyOneOf = getAllPrivacies()
545 } else if (req.query.filter === 'all') {
546 req.query.include = VideoInclude.NOT_PUBLISHED_STATE
547 req.query.privacyOneOf = getAllPrivacies()
548 } else if (req.query.filter === 'local') {
549 req.query.isLocal = true
552 req.query.filter = undefined
555 const user = res.locals.oauth?.token.User
557 if ((!user || user.hasRight(UserRight.SEE_ALL_VIDEOS) !== true)) {
558 if (req.query.include || req.query.privacyOneOf) {
560 status: HttpStatusCode.UNAUTHORIZED_401,
561 message: 'You are not allowed to see all videos.'
570 // ---------------------------------------------------------------------------
573 videosAddLegacyValidator,
574 videosAddResumableValidator,
575 videosAddResumableInitValidator,
576 videosResumableUploadIdValidator,
578 videosUpdateValidator,
580 videoFileMetadataGetValidator,
581 videosDownloadValidator,
582 checkVideoFollowConstraints,
583 videosCustomGetValidator,
584 videosRemoveValidator,
586 getCommonVideoEditAttributes,
588 commonVideosFiltersValidator,
590 videosOverviewValidator
593 // ---------------------------------------------------------------------------
595 function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) {
596 if (req.body.scheduleUpdate) {
597 if (!req.body.scheduleUpdate.updateAt) {
598 logger.warn('Invalid parameters: scheduleUpdate.updateAt is mandatory.')
600 res.fail({ message: 'Schedule update at is mandatory.' })
608 async function commonVideoChecksPass (parameters: {
610 res: express.Response
612 videoFileSize: number
613 files: express.UploadFilesForCheck
614 }): Promise<boolean> {
615 const { req, res, user, videoFileSize, files } = parameters
617 if (areErrorsInScheduleUpdate(req, res)) return false
619 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return false
621 if (!isVideoFileMimeTypeValid(files)) {
623 status: HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415,
624 message: 'This file is not supported. Please, make sure it is of the following type: ' +
625 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
630 if (!isVideoFileSizeValid(videoFileSize.toString())) {
632 status: HttpStatusCode.PAYLOAD_TOO_LARGE_413,
633 message: 'This file is too large. It exceeds the maximum file size authorized.',
634 type: ServerErrorCode.MAX_FILE_SIZE_REACHED
639 if (await isAbleToUploadVideo(user.id, videoFileSize) === false) {
641 status: HttpStatusCode.PAYLOAD_TOO_LARGE_413,
642 message: 'The user video quota is exceeded with this video.',
643 type: ServerErrorCode.QUOTA_REACHED
651 export async function isVideoAccepted (
652 req: express.Request,
653 res: express.Response,
654 videoFile: express.VideoUploadFile
656 // Check we accept this video
657 const acceptParameters = {
660 user: res.locals.oauth.token.User
662 const acceptedResult = await Hooks.wrapFun(
663 isLocalVideoAccepted,
665 'filter:api.video.upload.accept.result'
668 if (!acceptedResult || acceptedResult.accepted !== true) {
669 logger.info('Refused local video.', { acceptedResult, acceptParameters })
671 status: HttpStatusCode.FORBIDDEN_403,
672 message: acceptedResult.errorMessage || 'Refused local video'
680 async function addDurationToVideo (videoFile: { path: string, duration?: number }) {
681 const duration: number = await getDurationFromVideoFile(videoFile.path)
683 if (isNaN(duration)) throw new Error(`Couldn't get video duration`)
685 videoFile.duration = duration