+ if (!await isVideoAccepted(req, res, file)) return cleanup()
+
+ res.locals.videoFileResumable = file
+
+ return next()
+ }
+]
+
+/**
+ * File is created in POST initialisation, and its body is saved as a 'metadata' field is saved by uploadx for later use.
+ * see https://github.com/kukhariev/node-uploadx/blob/dc9fb4a8ac5a6f481902588e93062f591ec6ef03/packages/core/src/handlers/uploadx.ts
+ *
+ * Uploadx doesn't use next() until the upload completes, so this middleware has to be placed before uploadx
+ * see https://github.com/kukhariev/node-uploadx/blob/dc9fb4a8ac5a6f481902588e93062f591ec6ef03/packages/core/src/handlers/base-handler.ts
+ *
+ */
+const videosAddResumableInitValidator = getCommonVideoEditAttributes().concat([
+ body('filename')
+ .isString()
+ .exists()
+ .withMessage('Should have a valid filename'),
+ body('name')
+ .trim()
+ .custom(isVideoNameValid).withMessage(
+ `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
+ ),
+ body('channelId')
+ .customSanitizer(toIntOrNull)
+ .custom(isIdValid).withMessage('Should have correct video channel id'),
+
+ header('x-upload-content-length')
+ .isNumeric()
+ .exists()
+ .withMessage('Should specify the file length'),
+ header('x-upload-content-type')
+ .isString()
+ .exists()
+ .withMessage('Should specify the file mimetype'),
+
+ async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+ const videoFileMetadata = {
+ mimetype: req.headers['x-upload-content-type'] as string,
+ size: +req.headers['x-upload-content-length'],
+ originalname: req.body.filename
+ }
+
+ const user = res.locals.oauth.token.User
+ const cleanup = () => cleanUpReqFiles(req)
+
+ logger.debug('Checking videosAddResumableInitValidator parameters and headers', {
+ parameters: req.body,
+ headers: req.headers,
+ files: req.files
+ })
+
+ if (areValidationErrors(req, res)) return cleanup()
+
+ const files = { videofile: [ videoFileMetadata ] }
+ if (!await commonVideoChecksPass({ req, res, user, videoFileSize: videoFileMetadata.size, files })) return cleanup()
+
+ // multer required unsetting the Content-Type, now we can set it for node-uploadx
+ req.headers['content-type'] = 'application/json; charset=utf-8'
+ // place previewfile in metadata so that uploadx saves it in .META
+ if (req.files?.['previewfile']) req.body.previewfile = req.files['previewfile']