From e58cc6ac0aef2747e6864d2321455c04eab5bf8c Mon Sep 17 00:00:00 2001 From: ToasterBR Date: Sun, 30 Apr 2017 16:20:03 -0300 Subject: [PATCH] Mission Upload broken after Express 4 update --- lib/missions.js | 2 +- package.json | 1 + routes/missions.js | 13 ++++++++++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/missions.js b/lib/missions.js index 8eb7095..8f6cfba 100644 --- a/lib/missions.js +++ b/lib/missions.js @@ -47,7 +47,7 @@ Missions.prototype.list = function (cb){ }; Missions.prototype.handleUpload = function (uploadedFile, cb) { - var filename = decodeURI(uploadedFile.name.toLowerCase()); + var filename = decodeURI(uploadedFile.originalname.toLowerCase()); fs.rename(uploadedFile.path, path.join(this.missionsPath(), filename), function (err) { cb(err); }); diff --git a/package.json b/package.json index 9fd6a20..95fa4e2 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "gamedig": "0.2.25", "lodash": "^3.6.0", "morgan": "^1.8.1", + "multer": "^1.3.0", "playwithsix": "0.0.11", "serve-static": "^1.12.1", "slugify": "^1.1.0", diff --git a/routes/missions.js b/routes/missions.js index a417872..1499572 100644 --- a/routes/missions.js +++ b/routes/missions.js @@ -1,4 +1,6 @@ var express = require('express'); +var multer = require('multer'); +var upload = multer({ storage: multer.diskStorage({}) }); module.exports = function (missionsManager) { var router = express.Router(); @@ -13,13 +15,18 @@ module.exports = function (missionsManager) { }); }); - router.post('/', function (req, res) { - var missionFile = req.files.mission; +router.post('/', upload.single('mission'), function (req, res) { + var missionFile = req.file; + + if (!missionFile) { + return res.status(400).send('No mission file uploaded');; + } + missionsManager.handleUpload(missionFile, function (err) { if (err) { res.status(500).send(err); } else { - res.status(204); + res.status(200).json({success: true}); } }); });