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 { VideoInclude } from '@shared/models'
11 import { ServerErrorCode, UserRight, VideoPrivacy } from '../../../../shared'
12 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
24 } from '../../../helpers/custom-validators/misc'
25 import { isBooleanBothQueryValid, isNumberArray, isStringArray } from '../../../helpers/custom-validators/search'
27 isScheduleVideoUpdatePrivacyValid,
29 isVideoDescriptionValid,
30 isVideoFileMimeTypeValid,
38 isVideoOriginallyPublishedAtValid,
42 } from '../../../helpers/custom-validators/videos'
43 import { cleanUpReqFiles } from '../../../helpers/express-utils'
44 import { getDurationFromVideoFile } from '../../../helpers/ffprobe-utils'
45 import { logger } from '../../../helpers/logger'
46 import { deleteFileAndCatch } from '../../../helpers/utils'
47 import { getVideoWithAttributes } from '../../../helpers/video'
48 import { CONFIG } from '../../../initializers/config'
49 import { CONSTRAINTS_FIELDS, OVERVIEWS } from '../../../initializers/constants'
50 import { isLocalVideoAccepted } from '../../../lib/moderation'
51 import { Hooks } from '../../../lib/plugins/hooks'
52 import { VideoModel } from '../../../models/video/video'
53 import { authenticatePromiseIfNeeded } from '../../auth'
56 checkUserCanManageVideo,
57 doesVideoChannelOfAccountExist,
59 doesVideoFileOfVideoExist,
63 const videosAddLegacyValidator = getCommonVideoEditAttributes().concat([
65 .custom((value, { req }) => isFileFieldValid(req.files, 'videofile'))
66 .withMessage('Should have a file'),
69 .custom(isVideoNameValid).withMessage(
70 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
73 .customSanitizer(toIntOrNull)
74 .custom(isIdValid).withMessage('Should have correct video channel id'),
76 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
77 logger.debug('Checking videosAdd parameters', { parameters: req.body, files: req.files })
79 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
81 const videoFile: express.VideoUploadFile = req.files['videofile'][0]
82 const user = res.locals.oauth.token.User
84 if (!await commonVideoChecksPass({ req, res, user, videoFileSize: videoFile.size, files: req.files })) {
85 return cleanUpReqFiles(req)
89 if (!videoFile.duration) await addDurationToVideo(videoFile)
91 logger.error('Invalid input file in videosAddLegacyValidator.', { err })
94 status: HttpStatusCode.UNPROCESSABLE_ENTITY_422,
95 message: 'Video file unreadable.'
97 return cleanUpReqFiles(req)
100 if (!await isVideoAccepted(req, res, videoFile)) return cleanUpReqFiles(req)
106 const videosResumableUploadIdValidator = [
107 (req: express.Request, res: express.Response, next: express.NextFunction) => {
108 const user = res.locals.oauth.token.User
109 const uploadId = req.query.upload_id
111 if (uploadId.startsWith(user.id + '-') !== true) {
113 status: HttpStatusCode.FORBIDDEN_403,
114 message: 'You cannot send chunks in another user upload'
123 * Gets called after the last PUT request
125 const videosAddResumableValidator = [
126 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
127 const user = res.locals.oauth.token.User
128 const body: express.CustomUploadXFile<express.UploadXFileMetadata> = req.body
129 const file = { ...body, duration: undefined, path: getResumableUploadPath(body.name), filename: body.metadata.filename }
130 const cleanup = () => deleteFileAndCatch(file.path)
132 const uploadId = req.query.upload_id
133 const sessionExists = await Redis.Instance.doesUploadSessionExist(uploadId)
136 const sessionResponse = await Redis.Instance.getUploadSession(uploadId)
138 if (!sessionResponse) {
139 res.setHeader('Retry-After', 300) // ask to retry after 5 min, knowing the upload_id is kept for up to 15 min after completion
142 status: HttpStatusCode.SERVICE_UNAVAILABLE_503,
143 message: 'The upload is already being processed'
147 if (isTestInstance()) {
148 res.setHeader('x-resumable-upload-cached', 'true')
151 return res.json(sessionResponse)
154 await Redis.Instance.setUploadSession(uploadId)
156 if (!await doesVideoChannelOfAccountExist(file.metadata.channelId, user, res)) return cleanup()
159 if (!file.duration) await addDurationToVideo(file)
161 logger.error('Invalid input file in videosAddResumableValidator.', { err })
164 status: HttpStatusCode.UNPROCESSABLE_ENTITY_422,
165 message: 'Video file unreadable.'
170 if (!await isVideoAccepted(req, res, file)) return cleanup()
172 res.locals.videoFileResumable = file
179 * File is created in POST initialisation, and its body is saved as a 'metadata' field is saved by uploadx for later use.
180 * see https://github.com/kukhariev/node-uploadx/blob/dc9fb4a8ac5a6f481902588e93062f591ec6ef03/packages/core/src/handlers/uploadx.ts
182 * Uploadx doesn't use next() until the upload completes, so this middleware has to be placed before uploadx
183 * see https://github.com/kukhariev/node-uploadx/blob/dc9fb4a8ac5a6f481902588e93062f591ec6ef03/packages/core/src/handlers/base-handler.ts
186 const videosAddResumableInitValidator = getCommonVideoEditAttributes().concat([
190 .withMessage('Should have a valid filename'),
193 .custom(isVideoNameValid).withMessage(
194 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
197 .customSanitizer(toIntOrNull)
198 .custom(isIdValid).withMessage('Should have correct video channel id'),
200 header('x-upload-content-length')
203 .withMessage('Should specify the file length'),
204 header('x-upload-content-type')
207 .withMessage('Should specify the file mimetype'),
209 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
210 const videoFileMetadata = {
211 mimetype: req.headers['x-upload-content-type'] as string,
212 size: +req.headers['x-upload-content-length'],
213 originalname: req.body.name
216 const user = res.locals.oauth.token.User
217 const cleanup = () => cleanUpReqFiles(req)
219 logger.debug('Checking videosAddResumableInitValidator parameters and headers', {
220 parameters: req.body,
221 headers: req.headers,
225 if (areValidationErrors(req, res)) return cleanup()
227 const files = { videofile: [ videoFileMetadata ] }
228 if (!await commonVideoChecksPass({ req, res, user, videoFileSize: videoFileMetadata.size, files })) return cleanup()
230 // multer required unsetting the Content-Type, now we can set it for node-uploadx
231 req.headers['content-type'] = 'application/json; charset=utf-8'
232 // place previewfile in metadata so that uploadx saves it in .META
233 if (req.files?.['previewfile']) req.body.previewfile = req.files['previewfile']
239 const videosUpdateValidator = getCommonVideoEditAttributes().concat([
240 isValidVideoIdParam('id'),
245 .custom(isVideoNameValid).withMessage(
246 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
250 .customSanitizer(toIntOrNull)
251 .custom(isIdValid).withMessage('Should have correct video channel id'),
253 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
254 logger.debug('Checking videosUpdate parameters', { parameters: req.body })
256 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
257 if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
258 if (!await doesVideoExist(req.params.id, res)) return cleanUpReqFiles(req)
260 // Check if the user who did the request is able to update the video
261 const user = res.locals.oauth.token.User
262 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
264 if (req.body.channelId && !await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
270 async function checkVideoFollowConstraints (req: express.Request, res: express.Response, next: express.NextFunction) {
271 const video = getVideoWithAttributes(res)
273 // Anybody can watch local videos
274 if (video.isOwned() === true) return next()
277 if (res.locals.oauth) {
278 // Users can search or watch remote videos
279 if (CONFIG.SEARCH.REMOTE_URI.USERS === true) return next()
282 // Anybody can search or watch remote videos
283 if (CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true) return next()
285 // Check our instance follows an actor that shared this video
286 const serverActor = await getServerActor()
287 if (await VideoModel.checkVideoHasInstanceFollow(video.id, serverActor.id) === true) return next()
290 status: HttpStatusCode.FORBIDDEN_403,
291 message: 'Cannot get this video regarding follow constraints',
292 type: ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS,
299 const videosCustomGetValidator = (
300 fetchType: 'for-api' | 'all' | 'only-video' | 'only-immutable-attributes',
301 authenticateInQuery = false
304 isValidVideoIdParam('id'),
306 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
307 logger.debug('Checking videosGet parameters', { parameters: req.params })
309 if (areValidationErrors(req, res)) return
310 if (!await doesVideoExist(req.params.id, res, fetchType)) return
312 // Controllers does not need to check video rights
313 if (fetchType === 'only-immutable-attributes') return next()
315 const video = getVideoWithAttributes(res) as MVideoFullLight
317 // Video private or blacklisted
318 if (video.requiresAuth()) {
319 await authenticatePromiseIfNeeded(req, res, authenticateInQuery)
321 const user = res.locals.oauth ? res.locals.oauth.token.User : null
323 // Only the owner or a user that have blocklist rights can see the video
324 if (!user || !user.canGetVideo(video)) {
326 status: HttpStatusCode.FORBIDDEN_403,
327 message: 'Cannot get this private/internal or blocklisted video'
334 // Video is public, anyone can access it
335 if (video.privacy === VideoPrivacy.PUBLIC) return next()
337 // Video is unlisted, check we used the uuid to fetch it
338 if (video.privacy === VideoPrivacy.UNLISTED) {
339 if (isUUIDValid(req.params.id)) return next()
341 // Don't leak this unlisted video
343 status: HttpStatusCode.NOT_FOUND_404,
344 message: 'Video not found'
351 const videosGetValidator = videosCustomGetValidator('all')
352 const videosDownloadValidator = videosCustomGetValidator('all', true)
354 const videoFileMetadataGetValidator = getCommonVideoEditAttributes().concat([
355 isValidVideoIdParam('id'),
358 .custom(isIdValid).not().isEmpty().withMessage('Should have a valid videoFileId'),
360 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
361 logger.debug('Checking videoFileMetadataGet parameters', { parameters: req.params })
363 if (areValidationErrors(req, res)) return
364 if (!await doesVideoFileOfVideoExist(+req.params.videoFileId, req.params.id, res)) return
370 const videosRemoveValidator = [
371 isValidVideoIdParam('id'),
373 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
374 logger.debug('Checking videosRemove parameters', { parameters: req.params })
376 if (areValidationErrors(req, res)) return
377 if (!await doesVideoExist(req.params.id, res)) return
379 // Check if the user who did the request is able to delete the video
380 if (!checkUserCanManageVideo(res.locals.oauth.token.User, res.locals.videoAll, UserRight.REMOVE_ANY_VIDEO, res)) return
386 const videosOverviewValidator = [
389 .isInt({ min: 1, max: OVERVIEWS.VIDEOS.SAMPLES_COUNT })
390 .withMessage('Should have a valid pagination'),
392 (req: express.Request, res: express.Response, next: express.NextFunction) => {
393 if (areValidationErrors(req, res)) return
399 function getCommonVideoEditAttributes () {
401 body('thumbnailfile')
402 .custom((value, { req }) => isVideoImage(req.files, 'thumbnailfile')).withMessage(
403 'This thumbnail file is not supported or too large. Please, make sure it is of the following type: ' +
404 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
407 .custom((value, { req }) => isVideoImage(req.files, 'previewfile')).withMessage(
408 'This preview file is not supported or too large. Please, make sure it is of the following type: ' +
409 CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
414 .customSanitizer(toIntOrNull)
415 .custom(isVideoCategoryValid).withMessage('Should have a valid category'),
418 .customSanitizer(toIntOrNull)
419 .custom(isVideoLicenceValid).withMessage('Should have a valid licence'),
422 .customSanitizer(toValueOrNull)
423 .custom(isVideoLanguageValid).withMessage('Should have a valid language'),
426 .customSanitizer(toBooleanOrNull)
427 .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'),
428 body('waitTranscoding')
430 .customSanitizer(toBooleanOrNull)
431 .custom(isBooleanValid).withMessage('Should have a valid wait transcoding attribute'),
434 .customSanitizer(toValueOrNull)
435 .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'),
438 .customSanitizer(toValueOrNull)
439 .custom(isVideoDescriptionValid).withMessage('Should have a valid description'),
442 .customSanitizer(toValueOrNull)
443 .custom(isVideoSupportValid).withMessage('Should have a valid support text'),
446 .customSanitizer(toValueOrNull)
447 .custom(isVideoTagsValid)
449 `Should have an array of up to ${CONSTRAINTS_FIELDS.VIDEOS.TAGS.max} tags between ` +
450 `${CONSTRAINTS_FIELDS.VIDEOS.TAG.min} and ${CONSTRAINTS_FIELDS.VIDEOS.TAG.max} characters each`
452 body('commentsEnabled')
454 .customSanitizer(toBooleanOrNull)
455 .custom(isBooleanValid).withMessage('Should have comments enabled boolean'),
456 body('downloadEnabled')
458 .customSanitizer(toBooleanOrNull)
459 .custom(isBooleanValid).withMessage('Should have downloading enabled boolean'),
460 body('originallyPublishedAt')
462 .customSanitizer(toValueOrNull)
463 .custom(isVideoOriginallyPublishedAtValid).withMessage('Should have a valid original publication date'),
464 body('scheduleUpdate')
466 .customSanitizer(toValueOrNull),
467 body('scheduleUpdate.updateAt')
469 .custom(isDateValid).withMessage('Should have a schedule update date that conforms to ISO 8601'),
470 body('scheduleUpdate.privacy')
472 .customSanitizer(toIntOrNull)
473 .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy')
474 ] as (ValidationChain | ExpressPromiseHandler)[]
477 const commonVideosFiltersValidator = [
478 query('categoryOneOf')
480 .customSanitizer(toArray)
481 .custom(isNumberArray).withMessage('Should have a valid one of category array'),
482 query('licenceOneOf')
484 .customSanitizer(toArray)
485 .custom(isNumberArray).withMessage('Should have a valid one of licence array'),
486 query('languageOneOf')
488 .customSanitizer(toArray)
489 .custom(isStringArray).withMessage('Should have a valid one of language array'),
492 .customSanitizer(toArray)
493 .custom(isStringArray).withMessage('Should have a valid one of tags array'),
496 .customSanitizer(toArray)
497 .custom(isStringArray).withMessage('Should have a valid all of tags array'),
500 .custom(isBooleanBothQueryValid).withMessage('Should have a valid NSFW attribute'),
503 .customSanitizer(toBooleanOrNull)
504 .custom(isBooleanValid).withMessage('Should have a valid live boolean'),
507 .custom(isVideoFilterValid).withMessage('Should have a valid filter attribute'),
510 .custom(isVideoIncludeValid).withMessage('Should have a valid include attribute'),
513 .customSanitizer(toBooleanOrNull)
514 .custom(isBooleanValid).withMessage('Should have a valid local boolean'),
517 .customSanitizer(toBooleanOrNull)
518 .custom(isBooleanValid).withMessage('Should have a valid has hls boolean'),
519 query('hasWebtorrentFiles')
521 .customSanitizer(toBooleanOrNull)
522 .custom(isBooleanValid).withMessage('Should have a valid has webtorrent boolean'),
525 .customSanitizer(toBooleanOrNull)
526 .custom(isBooleanValid).withMessage('Should have a valid skip count boolean'),
529 .custom(exists).withMessage('Should have a valid search'),
531 (req: express.Request, res: express.Response, next: express.NextFunction) => {
532 logger.debug('Checking commons video filters query', { parameters: req.query })
534 if (areValidationErrors(req, res)) return
536 // FIXME: deprecated in 4.0, to remove
538 if (req.query.filter === 'all-local') {
539 req.query.include = VideoInclude.NOT_PUBLISHED_STATE | VideoInclude.HIDDEN_PRIVACY
540 req.query.isLocal = true
541 } else if (req.query.filter === 'all') {
542 req.query.include = VideoInclude.NOT_PUBLISHED_STATE | VideoInclude.HIDDEN_PRIVACY
543 } else if (req.query.filter === 'local') {
544 req.query.isLocal = true
547 req.query.filter = undefined
550 const user = res.locals.oauth?.token.User
552 if ((!user || user.hasRight(UserRight.SEE_ALL_VIDEOS) !== true)) {
553 if (req.query.include) {
555 status: HttpStatusCode.UNAUTHORIZED_401,
556 message: 'You are not allowed to see all videos.'
565 // ---------------------------------------------------------------------------
568 videosAddLegacyValidator,
569 videosAddResumableValidator,
570 videosAddResumableInitValidator,
571 videosResumableUploadIdValidator,
573 videosUpdateValidator,
575 videoFileMetadataGetValidator,
576 videosDownloadValidator,
577 checkVideoFollowConstraints,
578 videosCustomGetValidator,
579 videosRemoveValidator,
581 getCommonVideoEditAttributes,
583 commonVideosFiltersValidator,
585 videosOverviewValidator
588 // ---------------------------------------------------------------------------
590 function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) {
591 if (req.body.scheduleUpdate) {
592 if (!req.body.scheduleUpdate.updateAt) {
593 logger.warn('Invalid parameters: scheduleUpdate.updateAt is mandatory.')
595 res.fail({ message: 'Schedule update at is mandatory.' })
603 async function commonVideoChecksPass (parameters: {
605 res: express.Response
607 videoFileSize: number
608 files: express.UploadFilesForCheck
609 }): Promise<boolean> {
610 const { req, res, user, videoFileSize, files } = parameters
612 if (areErrorsInScheduleUpdate(req, res)) return false
614 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return false
616 if (!isVideoFileMimeTypeValid(files)) {
618 status: HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415,
619 message: 'This file is not supported. Please, make sure it is of the following type: ' +
620 CONSTRAINTS_FIELDS.VIDEOS.EXTNAME.join(', ')
625 if (!isVideoFileSizeValid(videoFileSize.toString())) {
627 status: HttpStatusCode.PAYLOAD_TOO_LARGE_413,
628 message: 'This file is too large. It exceeds the maximum file size authorized.',
629 type: ServerErrorCode.MAX_FILE_SIZE_REACHED
634 if (await isAbleToUploadVideo(user.id, videoFileSize) === false) {
636 status: HttpStatusCode.PAYLOAD_TOO_LARGE_413,
637 message: 'The user video quota is exceeded with this video.',
638 type: ServerErrorCode.QUOTA_REACHED
646 export async function isVideoAccepted (
647 req: express.Request,
648 res: express.Response,
649 videoFile: express.VideoUploadFile
651 // Check we accept this video
652 const acceptParameters = {
655 user: res.locals.oauth.token.User
657 const acceptedResult = await Hooks.wrapFun(
658 isLocalVideoAccepted,
660 'filter:api.video.upload.accept.result'
663 if (!acceptedResult || acceptedResult.accepted !== true) {
664 logger.info('Refused local video.', { acceptedResult, acceptParameters })
666 status: HttpStatusCode.FORBIDDEN_403,
667 message: acceptedResult.errorMessage || 'Refused local video'
675 async function addDurationToVideo (videoFile: { path: string, duration?: number }) {
676 const duration: number = await getDurationFromVideoFile(videoFile.path)
678 if (isNaN(duration)) throw new Error(`Couldn't get video duration`)
680 videoFile.duration = duration