You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

node.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. var express = require('express');
  2. var formidable = require('formidable');
  3. var fs = require('fs');
  4. var spawn = require('child_process').spawn;
  5. var app = express();
  6. var port = 9001;
  7. app.listen(port, () => {
  8. console.log("New app listening on " + port);
  9. });
  10. app.use("/stitchify/stitchimg", express.static(__dirname + "/stitchimg"));
  11. app.post("/stitchify/upload", (req, res) => {
  12. console.log("Request to stitchify/upload");
  13. var form = new formidable.IncomingForm();
  14. form.parse(req, (err, fields, files) => {
  15. console.log(files.infile.path);
  16. var filepath = "/var/www/noelle.codes/node/stitchimg/" + files.infile.name;
  17. fs.rename(files.infile.path, filepath, (err) => {
  18. if (err) {
  19. console.log("Oh no, err.");
  20. res.write("<h2>Heck.</h2>");
  21. res.write("<p>There was a server-side error. Please hit the back button and try again.</p>");
  22. res.end();
  23. } else {
  24. console.log("Uploaded " + files.infile.name);
  25. var filepath = files.infile.path;
  26. var process = spawn('python3', ["./stitchify.py",files.infile.name]);
  27. process.stdout.on("data", (data) => {
  28. console.log("Successfully converted " + data.toString());
  29. res.write("<img src='stitchimg/" + data.toString() + "'>");
  30. res.write("<br><br>");
  31. res.write("<p>(<a href='https://paypal.me/joyeusenoelle' target='_blank'>Did it work? Send me a couple bucks as a thank you!</a>)</p>");
  32. res.write("<p>(Or <a href='https://patreon.com/noelleanthony'>sign up for my Patreon so I can afford to stay alive and keep writing code like this!</a>)</p>");
  33. res.end();
  34. });
  35. }
  36. });
  37. });
  38. });
  39. app.get("/stitchify", (req, res) => {
  40. console.log("Request to stitchify");
  41. res.write("<h2>Stitchify!</h2>");
  42. res.write("<p>Turn your images into cross-stitch patterns!</p>");
  43. res.write("<p>At the moment, this tool supports images with up to 52 colors.More than that and it throws a tantrum.</p>");
  44. res.write("<form action='stitchify/upload' method='post' enctype='multipart/form-data'>");
  45. res.write("<input type='file' name='infile' accept='image/png, image/jpeg, image/gif'>");
  46. res.write("<input type='submit'>");
  47. res.write("</form>");
  48. res.write("<p>(<a href='https://paypal.me/joyeusenoelle' target='_blank'>Consider sending me a couple bucks as a thank you for writing this and keeping it running!</a>)</p>");
  49. res.write("<p>(Or <a href='https://patreon.com/noelleanthony'>sign up for my Patreon so I can afford to stay alive and keep writing code like this!</a>)</p>");
  50. res.end();
  51. });
  52. app.get("/", (req, res) => {
  53. console.log("Generic request");
  54. res.write("Try going to /stitchify!");
  55. res.end();
  56. });