`;
res.writeHead(200, {'Content-type': 'text/html; charset=utf-8'});
res.end(html, 'utf-8');
}
function sendFileJSON(res, deckName) {
fs.readFile(deckName + '.json', (error, content) => {
console.log(JSON.parse(content));
res.writeHead(200, {'Content-type': 'application/json; charset=utf-8'});
res.end(JSON.stringify(JSON.parse(content).ObjectStates[0]), 'utf-8');
if (error) {
console.error(error);
}
});
}
function handleUpload(res, req) {
let body = '';
req.on('data', data => {
body += data;
// check for file > 100MB
if (body.length > 1e8) {
req.connection.destroy();
console.log('upload too big');
}
});
req.on('end', () => {
const json = JSON.parse(body);
const deckJSON = json.json;
const cardTemplate = fs.readFileSync('template/card.json');
let deckOut = JSON.parse(fs.readFileSync('template/deck.json'));
deckOut.ObjectStates[0].Nickname = deckJSON.name;
Object.assign(deckOut.ObjectStates[0].CustomDeck['1'],
{NumWidth: Math.ceil(Math.sqrt(deckJSON.deck.length)),
NumHeight: Math.ceil(Math.sqrt(deckJSON.deck.length)),
FaceURL: "deck.png",
BackURL: "http://cloud-3.steamusercontent.com/ugc/156906385556221451/CE2C3AFE1759790CB0B532FFD636D05A99EC91F4/"});
deckOut.ObjectStates[0].CustomDeck['1'].ContainedObjects =
deckJSON.deck.map((cardIn, index) => {
let cardOut = JSON.parse(cardTemplate);
Object.assign(cardOut, {Nickname: cardIn.name,
Description: cardIn.keywords,
CardID: 100 + index});
for (let ii=0; ii<(cardIn.count || 1); ii++) {
deckOut.ObjectStates[0].DeckIDs.push(100 + index);
}
return cardOut;
});
fs.writeFileSync(deckJSON.name + '.json', JSON.stringify(deckOut));
fs.writeFileSync(deckJSON.name + '.input.json', JSON.stringify(deckJSON));
console.log("making page");
phantom.create().then(
ph => ph.createPage().then(
page => {
page.on('onLoadFinished', status => {
if (status !== 'success') {
console.log('Failed to load page');
ph.exit(1);
}
else {
page.render(deckJSON.name + ".png");
page.close().then(() => ph.exit());
}
});
page.property('zoomFactor', 2); // pretty arbitrary
page.property('content', json.body);
}));
decks.push(deckJSON.name);
});
}
function send404(res, uri) {
res.writeHead(404, {'Content-type': "text/html; charset=utf-8"});
const html = `
404 Not Found
Error 404: Path ${uri.pathname} not found
You seem to have gone to the wrong place, would you like to go
back to the main page?
`;
res.end(html, 'utf-8');
}
function sendFile(res, filename, contentType='text/html; charset=utf-8') {
fs.readFile(filename, (error, content) => {
res.writeHead(200, {'Content-type': contentType});
res.end(content, 'utf-8');
if (error) {
console.error(error);
}
});
}