From c739cf9923184e929b46242559b93fb121baf521 Mon Sep 17 00:00:00 2001 From: Matt Strapp Date: Thu, 24 Feb 2022 15:04:48 -0600 Subject: Fix bug with windows not uploading the right mimetype Signed-off-by: Matt Strapp --- src/public/js/form.js | 20 ++++++++++---------- src/routes/api.ts | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/public/js/form.js b/src/public/js/form.js index 3fbeb50..724fb05 100644 --- a/src/public/js/form.js +++ b/src/public/js/form.js @@ -3,7 +3,7 @@ window.onload = function () { document.getElementById('nojs').hidden = true; document.getElementById('block').hidden = false; -}; +}; // File submit AJAX request // After successful upload, actuate the file by calling actuate() on the successfully uploaded file @@ -54,24 +54,24 @@ function actuate(file) { xhr.onreadystatechange = function () { if (xhr.readyState === 4) { let response = JSON.parse(xhr.responseText); - if (xhr.status === 200) { - console.log(response); + if (xhr.status === 200 || xhr.status === 500) { createDownload(response.file); - } else { - // Display upload error message to the user - document.getElementById('actuate-err').innerText = response.error; - // DEBUG: Print full error if unknown error occurs - if (xhr.status === 500) - console.error(response.error_msg); } - return; + // Display upload error message to the user + document.getElementById('actuate-err').innerText = response.error; + // DEBUG: Print full error if unknown error occurs + if (xhr.status === 500) + console.error(response.error_msg); } + return; }; } // Creates the download element function createDownload(response) { + if (!response) + return; const tempName = response.filename; const downloadName = response.name.split('.')[ 0 ]; const downloadLink = document.createElement('a'); diff --git a/src/routes/api.ts b/src/routes/api.ts index f17b179..fd1ee38 100644 --- a/src/routes/api.ts +++ b/src/routes/api.ts @@ -41,7 +41,7 @@ api.use(fileUpload({ } 400 when there is no file 413 when the file is too large - 415 when the file's MIME type is not text/x-python + 415 when the file's MIME type is not text/x-python or text/plain (WINDOWS WHY) 500 for any other errors */ api.route('/upload') @@ -58,7 +58,7 @@ api.route('/upload') return res.status(413).json({ error: 'File uploaded was too large.' }); // Check if the file is a python file - if (file.mimetype !== 'text/x-python') + if (file.mimetype !== 'text/x-python' && file.mimetype !== 'text/plain') return res.status(415).json({ error: 'File uploaded was not a Python file.' }); res.status(201).json({ file: { file: file.name, path: file.tempFilePath }, msg: 'File uploaded successfully.', csrf: req.csrfToken() }); @@ -84,7 +84,6 @@ api.route('/upload') Returns: 200: { - "stdout": "Hello from Python!\n", "file": { "name": "file.py", "filename": "file-538126", @@ -124,20 +123,21 @@ api.route('/actuate') - Make this more secure - HOW? */ - let output = ''; + // let output = ''; + let stderr = ''; const actuation = spawn('python', escaped.split(' ')); - actuation.stdout.on('data', (data: Buffer) => { - output += data.toString(); - }); + // actuation.stdout.on('data', (data: Buffer) => { + // output += data.toString(); + // }); actuation.stderr.on('data', (data: Buffer) => { - output += `STDERR: ${data.toString()}`; + stderr += `STDERR: ${data.toString()}`; }); actuation.on('close', (code: number) => { // Make sure the program exited with a code of 0 (success) if (code !== 0) - return res.status(500).json({ error: `Program exited with exit code ${code}`, error_msg: output }); + return res.status(500).json({ error: `Program exited with exit code ${code}`, error_msg: stderr }); const filename: string = (req.body.file.path as string).split('/').pop() as string; - return res.status(200).json({ stdout: output, file: { name: req.body.file.file, filename: filename } }); + return res.status(200).json({ file: { name: req.body.file.file, filename: filename } }); }); // Kill the process if it takes too long // Default timeout is 120 seconds (2 minutes) -- cgit v1.2.3