Improved file handling of mission files

Fixes #19
This commit is contained in:
Björn Dahlgren 2015-11-21 12:41:32 +01:00
parent d01f8f572d
commit 9aa3853bcf
2 changed files with 15 additions and 13 deletions

View File

@ -1,5 +1,5 @@
<td style="width: 100%;">
<a href='/api/missions/<%-name%>'><%-name%></a>
<a href='/api/missions/<%-encodeURI(name)%>'><%-name%></a>
</td>
<td>
<button type="button" class="btn btn-danger btn-xs delete">

View File

@ -1,10 +1,14 @@
var fs = require('fs');
var path = require('path');
var config = require('./../config');
function missionsPath() {
return path.join(config.path, 'mpmissions');
}
exports.index = function(req, res){
var path = config.path + '/mpmissions';
fs.readdir(path, function (err, files) {
fs.readdir(missionsPath(), function (err, files) {
if (err) {
res.send(err);
} else {
@ -22,31 +26,29 @@ exports.create = function(req, res){
var missionFile = req.files.mission;
fs.readFile(missionFile.path, function (err, data) {
var newPath = config.path + '/mpmissions/' + missionFile.name.toLowerCase();
fs.writeFile(newPath, data, function (err) {
var filename = decodeURI(missionFile.name.toLowerCase());
fs.writeFile(path.join(missionsPath(), filename), data, function (err) {
res.json(missionFile);
});
});
};
exports.show = function(req, res){
var path = config.path + '/mpmissions/' + req.params.mission;
var filename = req.params.mission;
if (req.params.format) {
path += '.' + req.params.format;
filename += '.' + req.params.format;
}
res.download(path);
res.download(path.join(missionsPath(), encodeURI(filename)), decodeURI(filename));
};
exports.destroy = function(req, res){
var path = config.path + '/mpmissions/' + req.params.mission;
var filename = req.params.mission;
if (req.params.format) {
path += '.' + req.params.format;
filename += '.' + req.params.format;
}
fs.unlink(path, function (err) {
fs.unlink(path.join(missionsPath(), filename), function (err) {
if (err) {
res.json(500, {success: false});
} else {