"use strict"; // modules const fs = require('fs'); const mkdirp = require("mkdirp"); const {exec} = require('child_process'); const express = require('express'); const multer = require('multer'); const storage = multer.memoryStorage(); const upload = multer({storage: storage}); const logger = require('debug')('cc:squads'); // HTTP status codes by name const codes = require('./http-codes'); const apiAuthenticationMiddleware = require('../middleware/auth-middleware'); // const checkMT = require('../middleware/permission-check').checkMT(); const routerHandling = require('../middleware/router-handling'); // Mongoose Model using mongoDB const WarModel = require('../models/war'); const wars = express.Router(); // routes ********************** wars.route('/') // .get((req, res, next) => { // const filter = {}; // WarModel.find(filter, {}, {sort: {date: 'asc'}}, (err, items) => { // if (err) { // err.status = codes.servererror; // return next(err); // } // if (items) { // res.locals.items = items; // } else { // res.locals.items = []; // } // res.locals.processed = true; // next(); // }); // }) .post(upload.single('log'), (req, res, next) => { const war = new WarModel(req.body); // timestamp and default are set automatically by Mongoose Schema Validation if (req.file) { const timestamp = new Date(); const uploadDate = timestamp.toISOString().slice(0, 10) + '_' + timestamp.toTimeString().slice(0, 8) const folderName = __dirname + '/../resource/logs/' + uploadDate; mkdirp(folderName, function (err) { if (err) { return next(err); } fs.appendFile(folderName + '/war.log', new Buffer(req.file.buffer), (err) => { if (err) { next(err); } exec(__dirname + '/../war-parser/run.sh ' + folderName + ' | tee resource/logs/' + uploadDate + '/score.log' , (error, stdout, stderr) => { console.log("log") if (error) { return next(error); } console.log(`stdout: ${stdout}`); console.log(`stderr: ${stderr}`); res.locals.items={}; return next(); }); // exec(__dirname + '/../war-parser/clean.sh ' + folderName + ' | tee resource/logs/' + uploadDate + '/clean.log' , (error) => { // if (error) { // return next(error); // } // // }); }); }); // war.save((err) => { // if (err) { // err.status = codes.wrongrequest; // err.message += ' in fields: ' + Object.getOwnPropertyNames(err.errors); // return next(err); // } // res.status(codes.created); // res.locals.items = war; // fs.appendFile(__dirname + '/../resource/squad/' + war.id + '.png', new Buffer(req.file.buffer), (err) => { // if (err) next(err); // }); // next(); // }) } else { const err = new Error('no Logfile provided'); err.status = codes.wrongmediasend; next(err); } }) .all( routerHandling.httpMethodNotAllowed ); // // wars.route('/:id') // .get((req, res, next) => { // WarModel.findById(req.params.id, (err, item) => { // if (err) { // err.status = codes.servererror; // return next(err); // } // else if (!item) { // err = new Error("item not found"); // err.status = codes.notfound; // return next(err); // } // res.locals.items = item; // next(); // }); // }) // // .delete(apiAuthenticationMiddleware, checkHl, (req, res, next) => { // WarModel.findByIdAndRemove(req.params.id, (err, item) => { // if (err) { // err.status = codes.wrongrequest; // } // else if (!item) { // err = new Error("item not found"); // err.status = codes.notfound; // } // // // delete graphic // fs.unlink(__dirname + '/../resource/squad/' + req.params.id + '.png', (err) => { // if (err) next(err); // }); // // // we don't set res.locals.items and thus it will send a 204 (no content) at the end. see last handler user.use(..) // res.locals.processed = true; // next(err); // this works because err is in normal case undefined and that is the same as no parameter // }); // }) // // .all( // routerHandling.httpMethodNotAllowed // ); // this middleware function can be used, if you like or remove it // it looks for object(s) in res.locals.items and if they exist, they are send to the client as json wars.use(routerHandling.emptyResponse); module.exports = wars;