2014-04-04 08:37:43 +00:00
|
|
|
var fs = require('fs');
|
2015-11-21 11:41:32 +00:00
|
|
|
var path = require('path');
|
2014-04-04 08:37:43 +00:00
|
|
|
|
|
|
|
var config = require('./../config');
|
|
|
|
|
2015-11-21 11:41:32 +00:00
|
|
|
function missionsPath() {
|
|
|
|
return path.join(config.path, 'mpmissions');
|
|
|
|
}
|
|
|
|
|
2014-04-04 08:37:43 +00:00
|
|
|
exports.index = function(req, res){
|
2015-11-21 11:41:32 +00:00
|
|
|
fs.readdir(missionsPath(), function (err, files) {
|
2014-04-04 08:37:43 +00:00
|
|
|
if (err) {
|
|
|
|
res.send(err);
|
|
|
|
} else {
|
2014-04-04 09:28:33 +00:00
|
|
|
var missions = files.map(function (filename) {
|
2015-08-15 15:12:50 +00:00
|
|
|
return {
|
|
|
|
name: filename,
|
|
|
|
};
|
2014-04-04 09:28:33 +00:00
|
|
|
});
|
|
|
|
res.send(missions);
|
2014-04-04 08:37:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.create = function(req, res){
|
2014-04-04 09:28:33 +00:00
|
|
|
var missionFile = req.files.mission;
|
2014-04-05 17:02:17 +00:00
|
|
|
|
2014-04-04 09:28:33 +00:00
|
|
|
fs.readFile(missionFile.path, function (err, data) {
|
2015-11-21 11:41:32 +00:00
|
|
|
var filename = decodeURI(missionFile.name.toLowerCase());
|
|
|
|
fs.writeFile(path.join(missionsPath(), filename), data, function (err) {
|
2014-04-04 09:28:33 +00:00
|
|
|
res.json(missionFile);
|
|
|
|
});
|
|
|
|
});
|
2014-04-04 08:37:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.show = function(req, res){
|
2015-11-21 11:41:32 +00:00
|
|
|
var filename = req.params.mission;
|
2014-04-05 19:27:54 +00:00
|
|
|
if (req.params.format) {
|
2015-11-21 11:41:32 +00:00
|
|
|
filename += '.' + req.params.format;
|
2014-04-05 19:27:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-21 11:41:32 +00:00
|
|
|
res.download(path.join(missionsPath(), encodeURI(filename)), decodeURI(filename));
|
2014-04-04 08:37:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.destroy = function(req, res){
|
2015-11-21 11:41:32 +00:00
|
|
|
var filename = req.params.mission;
|
2014-04-05 17:02:17 +00:00
|
|
|
if (req.params.format) {
|
2015-11-21 11:41:32 +00:00
|
|
|
filename += '.' + req.params.format;
|
2014-04-05 17:02:17 +00:00
|
|
|
}
|
|
|
|
|
2015-11-21 11:41:32 +00:00
|
|
|
fs.unlink(path.join(missionsPath(), filename), function (err) {
|
2014-04-05 17:02:17 +00:00
|
|
|
if (err) {
|
|
|
|
res.json(500, {success: false});
|
|
|
|
} else {
|
|
|
|
res.json({success: true});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|