diff options
author | Matt Strapp <matt@mattstrapp.net> | 2022-02-24 15:04:48 -0600 |
---|---|---|
committer | Matt Strapp <matt@mattstrapp.net> | 2022-02-24 15:04:48 -0600 |
commit | c739cf9923184e929b46242559b93fb121baf521 (patch) | |
tree | b018d93795333134ff44449fc1b34ce4a8f89988 | |
parent | Bump @types/node from 17.0.19 to 17.0.20 (#12) (diff) | |
download | ee4511w-web-c739cf9923184e929b46242559b93fb121baf521.tar ee4511w-web-c739cf9923184e929b46242559b93fb121baf521.tar.gz ee4511w-web-c739cf9923184e929b46242559b93fb121baf521.tar.bz2 ee4511w-web-c739cf9923184e929b46242559b93fb121baf521.tar.lz ee4511w-web-c739cf9923184e929b46242559b93fb121baf521.tar.xz ee4511w-web-c739cf9923184e929b46242559b93fb121baf521.tar.zst ee4511w-web-c739cf9923184e929b46242559b93fb121baf521.zip |
Fix bug with windows not uploading the right mimetype
Signed-off-by: Matt Strapp <matt@mattstrapp.net>
-rw-r--r-- | src/public/js/form.js | 20 | ||||
-rw-r--r-- | src/routes/api.ts | 20 |
2 files changed, 20 insertions, 20 deletions
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) |