kettek2/acts/old/delete.js

63 lines
2.4 KiB
JavaScript

// TODO: deletions should check to see if they were the last file in a directory, and if so, to delete the parent folder and redirect to the parent view
// TODO: additionally, deletions should work on generic files and even directories!
var fs = require('fs');
var path = require('path');
module.exports = function(qwiki) {
// **** DELETE
qwiki.rule('delete', '@@CONTENT@@', function(req, res, instance, next) {
res.write('<form action="" method="POST"> Deleting this page will also delete all page revisions! Are you sure you wish to do this?<div class="prompt"><input type="submit" name="submit" value="Yes"><input type="submit" name="submit" value="No"></div></form>');
next()
});
qwiki.act('delete', function(req, res) {
var area = (req.url == '' ? 'front' : req.url.substr(1));
// handle POST
if ('submit' in req.fields) {
if (req.fields['submit'] == 'Yes') {
fs.stat('wiki/'+area+'.qwk', function(err, stat) {
if (err == null) {
qwiki.deletePage(area, function() {
qwiki.deleteCache(area, function() {
console.log('redirecting to '+req.url);
res.writeHead(302, {'Location': req.url});
res.end();
});
});
} else if (err.code == 'ENOENT') {
// does not exist as a wiki document, it's a directory or a file
fs.stat('wiki/'+area, function(err, stat) {
if (err == null) {
if (stat.isDirectory()) {
console.log('request to delete directory');
} else {
fs.unlink('wiki/'+area, function(err) {
console.log('deleted file: ' + area);
});
}
} else if (err.code == 'ENOENT') {
console.log('file "' + area + '" does not exist');
} else {
console.log(err);
}
});
res.writeHead(302, {'Location': '../index'});
res.end();
} else {
console.log(err);
res.writeHead(302, {'Location': '../index'});
res.end();
}
});
return;
} else {
res.writeHead(302, {'Location': req.url+'/edit'});
res.end();
}
} else {
res.writeHead(200, "OK", {
"Content-Type": "text/html",
});
}
qwiki.parsePage('delete', 'edit', req, res);
});
}