diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-18 14:28:37 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | b4055e1c23eeefb0c8a85a77f312b2827d98f483 (patch) | |
tree | 51b6b04c1ad10897047817d2eaaa037d1331fa6a /server/middlewares/validators/videos/videos.ts | |
parent | 66e001c848c009412c65cbce41be344d8985fd83 (diff) | |
download | PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.tar.gz PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.tar.zst PeerTube-b4055e1c23eeefb0c8a85a77f312b2827d98f483.zip |
Add server hooks
Diffstat (limited to 'server/middlewares/validators/videos/videos.ts')
-rw-r--r-- | server/middlewares/validators/videos/videos.ts | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/server/middlewares/validators/videos/videos.ts b/server/middlewares/validators/videos/videos.ts index b1c05ab2d..cb2c071ba 100644 --- a/server/middlewares/validators/videos/videos.ts +++ b/server/middlewares/validators/videos/videos.ts | |||
@@ -33,7 +33,7 @@ import { | |||
33 | import { getDurationFromVideoFile } from '../../../helpers/ffmpeg-utils' | 33 | import { getDurationFromVideoFile } from '../../../helpers/ffmpeg-utils' |
34 | import { logger } from '../../../helpers/logger' | 34 | import { logger } from '../../../helpers/logger' |
35 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' | 35 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' |
36 | import { authenticatePromiseIfNeeded } from '../../oauth' | 36 | import { authenticate, authenticatePromiseIfNeeded } from '../../oauth' |
37 | import { areValidationErrors } from '../utils' | 37 | import { areValidationErrors } from '../utils' |
38 | import { cleanUpReqFiles } from '../../../helpers/express-utils' | 38 | import { cleanUpReqFiles } from '../../../helpers/express-utils' |
39 | import { VideoModel } from '../../../models/video/video' | 39 | import { VideoModel } from '../../../models/video/video' |
@@ -44,6 +44,8 @@ import { VideoFetchType } from '../../../helpers/video' | |||
44 | import { isNSFWQueryValid, isNumberArray, isStringArray } from '../../../helpers/custom-validators/search' | 44 | import { isNSFWQueryValid, isNumberArray, isStringArray } from '../../../helpers/custom-validators/search' |
45 | import { getServerActor } from '../../../helpers/utils' | 45 | import { getServerActor } from '../../../helpers/utils' |
46 | import { CONFIG } from '../../../initializers/config' | 46 | import { CONFIG } from '../../../initializers/config' |
47 | import { isLocalVideoAccepted } from '../../../lib/moderation' | ||
48 | import { Hooks } from '../../../lib/plugins/hooks' | ||
47 | 49 | ||
48 | const videosAddValidator = getCommonVideoEditAttributes().concat([ | 50 | const videosAddValidator = getCommonVideoEditAttributes().concat([ |
49 | body('videofile') | 51 | body('videofile') |
@@ -62,14 +64,12 @@ const videosAddValidator = getCommonVideoEditAttributes().concat([ | |||
62 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) | 64 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) |
63 | if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req) | 65 | if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req) |
64 | 66 | ||
65 | const videoFile: Express.Multer.File = req.files['videofile'][0] | 67 | const videoFile: Express.Multer.File & { duration?: number } = req.files['videofile'][0] |
66 | const user = res.locals.oauth.token.User | 68 | const user = res.locals.oauth.token.User |
67 | 69 | ||
68 | if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) | 70 | if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) |
69 | 71 | ||
70 | const isAble = await user.isAbleToUploadVideo(videoFile) | 72 | if (await user.isAbleToUploadVideo(videoFile) === false) { |
71 | |||
72 | if (isAble === false) { | ||
73 | res.status(403) | 73 | res.status(403) |
74 | .json({ error: 'The user video quota is exceeded with this video.' }) | 74 | .json({ error: 'The user video quota is exceeded with this video.' }) |
75 | 75 | ||
@@ -88,7 +88,9 @@ const videosAddValidator = getCommonVideoEditAttributes().concat([ | |||
88 | return cleanUpReqFiles(req) | 88 | return cleanUpReqFiles(req) |
89 | } | 89 | } |
90 | 90 | ||
91 | videoFile['duration'] = duration | 91 | videoFile.duration = duration |
92 | |||
93 | if (!await isVideoAccepted(req, res, videoFile)) return cleanUpReqFiles(req) | ||
92 | 94 | ||
93 | return next() | 95 | return next() |
94 | } | 96 | } |
@@ -434,3 +436,26 @@ function areErrorsInScheduleUpdate (req: express.Request, res: express.Response) | |||
434 | 436 | ||
435 | return false | 437 | return false |
436 | } | 438 | } |
439 | |||
440 | async function isVideoAccepted (req: express.Request, res: express.Response, videoFile: Express.Multer.File & { duration?: number }) { | ||
441 | // Check we accept this video | ||
442 | const acceptParameters = { | ||
443 | videoBody: req.body, | ||
444 | videoFile, | ||
445 | user: res.locals.oauth.token.User | ||
446 | } | ||
447 | const acceptedResult = await Hooks.wrapObject( | ||
448 | isLocalVideoAccepted(acceptParameters), | ||
449 | 'filter:api.video.upload.accept.result' | ||
450 | ) | ||
451 | |||
452 | if (!acceptedResult || acceptedResult.accepted !== true) { | ||
453 | logger.info('Refused local video.', { acceptedResult, acceptParameters }) | ||
454 | res.status(403) | ||
455 | .json({ error: acceptedResult.errorMessage || 'Refused local video' }) | ||
456 | |||
457 | return false | ||
458 | } | ||
459 | |||
460 | return true | ||
461 | } | ||