I have implemented imaged upload using multer and I am receiving the images back to my front end app built with angular. Now I want to do the same with video, upload the video such that I accept all video formats but store on mp4 or other acceptable formats. I have searched everywhere but cant not seem to find out if multer can process video. Here is my implementation so far.
const router = require('express-promise-router')();
const VideosController = require('./video.controller');
const { validateParam, validateBody, schemas } = require('../_middleware/routehelpers');
const db = require('../_helpers/db');
const Video = require('./video.model');
const passport = require('passport');
const passportConfig = require('../passport');
const passportLogin = passport.authenticate('local', {session: false});
const passportJwt = passport.authenticate('jwt', { session: false });
const multer = require('multer');
const videoFilter = require('./videoFilter');
// Save file to server storage
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './public/videos');
},
filename: (req, file, cb) => {
var filetype = '';
if (file.mimetype === 'video/gif') {
filetype = 'gif';
}
if (file.mimetype === 'video/mp4') {
filetype = 'mp4';
}
if (file.mimetype === 'video/ogg') {
filetype = 'ogg';
}
if (file.mimetype === 'video/wmv') {
filetype = 'wmv';
}
if (file.mimetype === 'video/x-flv') {
//filetype = mime.getExtension('video/flv');
filetype = 'flv';
}
if (file.mimetype === 'video/avi') {
filetype = 'avi';
}
if (file.mimetype === 'video/webm') {
filetype = 'webm';
}
if (file.mimetype === 'video/mkv') {
filetype = 'mkv';
}
if (file.mimetype === 'video/avchd') {
filetype = 'avchd';
}
if (file.mimetype === 'video/mov') {
filetype = 'mov';
}
cb(null, 'video-' + Date.now() + '.' + filetype);
}
});
var upload = multer({ storage: storage, fileFilter: videoFilter.videoFilter });
// my routes
router.route('/')
.post(passportJwt,upload.single('file'),VideosController.addNewVideo);
//my controller
addNewVideo: async (req, res, next) => {
// check file for Validation Error
if (req.fileValidationError) {
return res.send(req.fileValidationError);
}
else if (!req.file) {
return res.status(500).send({ message: 'Upload fail'});
} else {
// if no Validation Error
// hbjs.spawn({ input: 'something.avi', output: 'something.m4v' })
// console.log('req.file.filename',req.file.mimetype )
req.body.videoUrl = 'http://localhost:3000/videos/' + req.file.filename;
//get the body
const newVideo = req.body;
//get the user
const userId = req.user._id;
//assign new video to the user
newVideo.userId = userId;
// create the new video
const video = await new Video(newVideo);
// save the new Video
await video.save();
//get the user from db
const user = await db.User.findById(userId);
// save the new video to user collections
await user.user_videos.push(video);
await user.save();
//respond to client
res.status(200).json(video);
}
},
////// I tested with .flv, the file is saved on the public/videos but I wish to convert all files to popular format only