arma-server-web-admin/routes/missions.js

59 lines
1.3 KiB
JavaScript
Raw Normal View History

2014-04-04 08:37:43 +00:00
var fs = require('fs');
var path = require('path');
2014-04-04 08:37:43 +00:00
var config = require('./../config');
function missionsPath() {
return path.join(config.path, 'mpmissions');
}
2014-04-04 08:37:43 +00:00
exports.index = function(req, res){
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) {
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){
var filename = req.params.mission;
2014-04-05 19:27:54 +00:00
if (req.params.format) {
filename += '.' + req.params.format;
2014-04-05 19:27:54 +00:00
}
res.download(path.join(missionsPath(), encodeURI(filename)), decodeURI(filename));
2014-04-04 08:37:43 +00:00
};
exports.destroy = function(req, res){
var filename = req.params.mission;
2014-04-05 17:02:17 +00:00
if (req.params.format) {
filename += '.' + req.params.format;
2014-04-05 17:02:17 +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});
}
});
};