2014-04-06 01:10:09 +00:00
|
|
|
var fs = require('fs')
|
|
|
|
, path = require('path')
|
2014-07-28 17:46:01 +00:00
|
|
|
, async = require('async')
|
2014-04-21 13:24:47 +00:00
|
|
|
, playwithsix = require('playwithsix')
|
2014-04-06 01:10:09 +00:00
|
|
|
, when = require('when')
|
|
|
|
, nodefn = require('when/node/function');
|
2014-04-04 08:37:43 +00:00
|
|
|
|
|
|
|
var config = require('./../config');
|
|
|
|
|
2014-04-21 13:24:47 +00:00
|
|
|
function downloadMod(mod, cb) {
|
|
|
|
playwithsix.downloadMod(config.path, mod, cb);
|
|
|
|
}
|
|
|
|
|
2014-04-06 01:10:09 +00:00
|
|
|
function walk (directory) {
|
|
|
|
createFile = function (file, stat) {
|
|
|
|
return {
|
|
|
|
type: "file",
|
|
|
|
name: file,
|
|
|
|
size: stat.size
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
createFolder = function (folder) {
|
|
|
|
return {
|
|
|
|
type: "folder",
|
|
|
|
name: folder,
|
|
|
|
files: []
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
var results = [];
|
|
|
|
|
|
|
|
return when.map(nodefn.call(fs.readdir, directory), function(file) {
|
|
|
|
var absolutePath = path.join(directory, file);
|
|
|
|
return nodefn.call(fs.stat, absolutePath).then(function(stat) {
|
|
|
|
if (stat.isFile()) {
|
|
|
|
return results.push(createFile(file, stat));
|
|
|
|
}
|
|
|
|
|
|
|
|
folder = createFolder(file);
|
|
|
|
return walk(absolutePath).then(function(filesInDir) {
|
|
|
|
folder.files = filesInDir;
|
|
|
|
results.push(folder);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).then(function() {
|
|
|
|
return results;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-07-28 17:46:01 +00:00
|
|
|
function isPlayWithSixMod(modPath, cb) {
|
2014-11-09 23:50:17 +00:00
|
|
|
var pwsFile = path.join(modPath, '.synq.json');
|
2014-07-28 17:46:01 +00:00
|
|
|
fs.exists(pwsFile, function (exists) {
|
|
|
|
if (cb) {
|
|
|
|
cb(exists);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-04-04 08:37:43 +00:00
|
|
|
exports.index = function(req, res){
|
|
|
|
fs.readdir(config.path, function (err, files) {
|
|
|
|
if (err) {
|
|
|
|
res.send(err);
|
|
|
|
} else {
|
|
|
|
var mods = files.filter(function (file) {
|
|
|
|
return file.charAt(0) == "@";
|
|
|
|
});
|
2014-04-21 13:24:47 +00:00
|
|
|
|
2014-05-21 22:13:14 +00:00
|
|
|
playwithsix.checkOutdated(config.path, function (err, outdatedMods) {
|
2014-07-28 17:46:01 +00:00
|
|
|
async.map(mods, function (mod, cb) {
|
|
|
|
var modPath = path.join(config.path, mod);
|
|
|
|
isPlayWithSixMod(modPath, function (isPlayWithSixMod) {
|
2015-01-16 21:50:17 +00:00
|
|
|
cb(null, { name: mod, outdated: outdatedMods && outdatedMods.indexOf(mod) >= 0, playWithSix: isPlayWithSixMod });
|
2014-07-28 17:46:01 +00:00
|
|
|
});
|
|
|
|
}, function (err, mods) {
|
|
|
|
res.send(mods);
|
|
|
|
});
|
2014-04-21 13:24:47 +00:00
|
|
|
});
|
2014-04-04 08:37:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.create = function(req, res){
|
2014-05-21 22:13:14 +00:00
|
|
|
downloadMod(req.body.name, function (err, mods) {
|
2014-05-06 23:35:44 +00:00
|
|
|
if (mods && !err) {
|
|
|
|
res.send(mods);
|
|
|
|
} else {
|
|
|
|
res.send(500, err);
|
|
|
|
}
|
|
|
|
});
|
2014-04-04 08:37:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.show = function(req, res){
|
2014-04-06 01:10:09 +00:00
|
|
|
walk(config.path + "/" + req.params.mod).then(function(files) {
|
|
|
|
res.json(files);
|
|
|
|
}).otherwise(function(error) {
|
|
|
|
console.error(error.stack || error);
|
|
|
|
res.send({success: false});
|
|
|
|
});
|
2014-04-04 08:37:43 +00:00
|
|
|
};
|
|
|
|
|
2014-04-21 13:24:47 +00:00
|
|
|
exports.update = function(req, res){
|
2014-05-21 22:13:14 +00:00
|
|
|
downloadMod(req.params.mod, function (err, mods) {
|
2014-04-21 13:24:47 +00:00
|
|
|
if (mods && !err) {
|
|
|
|
res.send(mods);
|
|
|
|
} else {
|
|
|
|
res.send(500, err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-04-04 08:37:43 +00:00
|
|
|
exports.destroy = function(req, res){
|
|
|
|
res.send('destroy mod ' + req.params.mod);
|
2014-04-06 01:10:09 +00:00
|
|
|
};
|