From e78a20c7f914423736f7b5b0bf981418e43b6c36 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 9 Jan 2016 12:59:13 +0100 Subject: [PATCH] Fix upload of file which don't have any extension --- routes/api/v1/videos.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/routes/api/v1/videos.js b/routes/api/v1/videos.js index ae70d1640..95c1dffc7 100644 --- a/routes/api/v1/videos.js +++ b/routes/api/v1/videos.js @@ -3,6 +3,7 @@ var express = require('express') var config = require('config') + var crypto = require('crypto') var multer = require('multer') var router = express.Router() @@ -58,7 +59,24 @@ }) } - var reqFiles = multer({ dest: uploads }).fields([{ name: 'input_video', maxCount: 1 }]) + // multer configuration + var storage = multer.diskStorage({ + destination: function (req, file, cb) { + cb(null, uploads) + }, + + filename: function (req, file, cb) { + var extension = '' + if (file.mimetype === 'video/webm') extension = 'webm' + else if (file.mimetype === 'video/mp4') extension = 'mp4' + else if (file.mimetype === 'video/ogg') extension = 'ogv' + crypto.pseudoRandomBytes(16, function (err, raw) { + var fieldname = err ? undefined : raw.toString('hex') + cb(null, fieldname + '.' + extension) + }) + } + }) + var reqFiles = multer({ storage: storage }).fields([{ name: 'input_video', maxCount: 1 }]) router.get('/', miscMiddleware.cache(false), listVideos) router.post('/', reqFiles, reqValidator.videosAdd, miscMiddleware.cache(false), addVideos)