- checkErrors(req, res, () => {
- const videoFile: Express.Multer.File = req.files['videofile'][0]
- const user = res.locals.oauth.token.User
-
- user.isAbleToUploadVideo(videoFile)
- .then(isAble => {
- if (isAble === false) {
- res.status(403)
- .json({ error: 'The user video quota is exceeded with this video.' })
- .end()
-
- return undefined
- }
-
- return db.Video.getDurationFromFile(videoFile.path)
- .catch(err => {
- logger.error('Invalid input file in videosAddValidator.', err)
- res.status(400)
- .json({ error: 'Invalid input file.' })
- .end()
-
- return undefined
- })
- })
- .then(duration => {
- // Previous test failed, abort
- if (duration === undefined) return
-
- if (!isVideoDurationValid('' + duration)) {
- return res.status(400)
- .json({
- error: 'Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).'
- })
- .end()
- }
-
- videoFile['duration'] = duration
- next()
- })
- .catch(err => {
- logger.error('Error in video add validator', err)
- res.sendStatus(500)
-
- return undefined
- })
- })
+ if (areValidationErrors(req, res)) return
+
+ const videoFile: Express.Multer.File = req.files['videofile'][0]
+ const user = res.locals.oauth.token.User
+
+ const videoChannel = await db.VideoChannel.loadByIdAndAccount(req.body.channelId, user.Account.id)
+ if (!videoChannel) {
+ res.status(400)
+ .json({ error: 'Unknown video video channel for this account.' })
+ .end()
+
+ return
+ }
+
+ res.locals.videoChannel = videoChannel
+
+ const isAble = await user.isAbleToUploadVideo(videoFile)
+ if (isAble === false) {
+ res.status(403)
+ .json({ error: 'The user video quota is exceeded with this video.' })
+ .end()
+
+ return
+ }
+
+ let duration: number
+
+ try {
+ duration = await getDurationFromVideoFile(videoFile.path)
+ } catch (err) {
+ logger.error('Invalid input file in videosAddValidator.', err)
+ res.status(400)
+ .json({ error: 'Invalid input file.' })
+ .end()
+
+ return
+ }
+
+ videoFile['duration'] = duration
+
+ return next()