Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
3.0 kB
4
Indexable
Never
const fileHandlers = require('../filehandlers/fileHandlers')

exports.executeCode = async (req, res, next) => {
 
    const {language, code, input} = req.body;
    // console.log(req.body)
    
    if(language != 'c++' && language !='java' && language != 'py') {
        
        if(req.body.isSubmission == true) {
            return null;
        }

        return res.status(404).json({
            status: "fail",
            error: "invalid language"
        })
    }
    
    const result = fileHandlers.saveFile(language, code,input)
    if(result == undefined) {
        if(req.body.isSubmission == true) {
            return null;
        }

        return res.status(404).json({
            status: "fail",
            error: "failed while saving file"
        })
    }
    if(language == 'java') {
        fileHandlers.editJavaFile(result.filePath)
        if(req.body.isSubmission == true) {
            return null;
        }

        return res.status(404).json({
            status: "fail"
        })
    }

    await fileHandlers.compileFile(language, result.filePath, res) 
        .then((exePath) => {
            fileHandlers.executeFile(exePath, language, result.inputPath)
                .then((output) => {
                    const files = [exePath, result.inputPath, result.filePath]

                    if(req.body.isSubmission == true) {
                        //check against test cases
                        const userOutput = output.replace(/\s/g, '');
                        const requiredOutput = req.body.output.replace(/\s/g, '');
                        console.log(userOutput.length)
                        console.log(requiredOutput.length)
                        if(userOutput.localeCompare(requiredOutput) == 0) {
                            console.log("Returned true")
                            fileHandlers.deleteFiles(files)
                            return true;
                        } 
                        else  {
                            console.log("Returned false")
                            fileHandlers.deleteFiles(files)
                            return false;
                        }
                    }
                    return res.status(200).json({
                        status: "success",
                        result: output
                    })
                })
                .catch((message) => {
                    return res.status(200).json({
                        status: "success",
                        result: message
                    })
                })
        })
        .catch((message) => {
            // console.log(message)
            if(req.body.isSubmission == true) {
                return null;
            }
            return res.status(200).json({
                status: "success",
                error: "compilation error",
                message: message 
            })
        })
}