webserv.ino 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Web Server */
  2. const String webroot = "/web/"; //Require tailing slash
  3. void sendNotFound() {
  4. server.send(404, "text/plain", "404 - Not found");
  5. }
  6. // Function to handle file uploads
  7. void handleFileUpload() {
  8. HTTPUpload& upload = server.upload();
  9. static File uploadFile;
  10. if (upload.status == UPLOAD_FILE_START) {
  11. String path = webroot + "uploads/" + upload.filename;
  12. Serial.printf("UploadStart: %s\n", path.c_str());
  13. uploadFile = SD.open(path, FILE_WRITE);
  14. if (!uploadFile) {
  15. Serial.println("Failed to open file for writing");
  16. return;
  17. }
  18. } else if (upload.status == UPLOAD_FILE_WRITE) {
  19. // Write the received data to the file
  20. if (uploadFile) {
  21. uploadFile.write(upload.buf, upload.currentSize);
  22. }
  23. } else if (upload.status == UPLOAD_FILE_END) {
  24. // Close the file at the final chunk
  25. if (uploadFile) {
  26. uploadFile.close();
  27. Serial.printf("UploadEnd: %s (%u)\n", upload.filename.c_str(), upload.totalSize);
  28. server.send(200, "text/plain", "File Uploaded");
  29. } else {
  30. server.send(500, "text/plain", "File Upload Failed");
  31. }
  32. } else {
  33. sendNotFound();
  34. }
  35. }
  36. // Function to serve files
  37. void handleFileServe() {
  38. String path = server.uri();
  39. String filepath = path;
  40. filepath.remove(0, 1); //Trim the prefix slash in uri
  41. filepath = webroot + filepath;
  42. Serial.println(filepath);
  43. if (SD.exists(filepath)) {
  44. File file = SD.open(filepath);
  45. if (file) {
  46. server.streamFile(file, getMime(path));
  47. file.close();
  48. } else {
  49. server.send(500, "text/plain", "500 - Internal Server Error");
  50. }
  51. } else {
  52. handleRootRedirect();
  53. }
  54. }
  55. //Redirect request to index.html
  56. void handleRootRedirect() {
  57. server.sendHeader("Location", "/index.html", true);
  58. server.send(302, "text/plain", "Redirecting to index.html");
  59. }
  60. void registerAPIEndpoints() {
  61. server.on("/", HTTP_GET, handleRootRedirect);
  62. server.onNotFound(handleFileServe);
  63. server.on("/upload", HTTP_POST, []() {
  64. server.send(200, "text/plain", ""); // Send empty response for the upload URL
  65. }, handleFileUpload);
  66. }
  67. /* Utilities */
  68. String getMime(const String& path) {
  69. String _contentType = "text/plain";
  70. if (path.endsWith(".html")) _contentType = "text/html";
  71. else if (path.endsWith(".htm")) _contentType = "text/html";
  72. else if (path.endsWith(".css")) _contentType = "text/css";
  73. else if (path.endsWith(".json")) _contentType = "text/json";
  74. else if (path.endsWith(".js")) _contentType = "application/javascript";
  75. else if (path.endsWith(".png")) _contentType = "image/png";
  76. else if (path.endsWith(".gif")) _contentType = "image/gif";
  77. else if (path.endsWith(".jpg")) _contentType = "image/jpeg";
  78. else if (path.endsWith(".ico")) _contentType = "image/x-icon";
  79. else if (path.endsWith(".svg")) _contentType = "image/svg+xml";
  80. else if (path.endsWith(".eot")) _contentType = "font/eot";
  81. else if (path.endsWith(".woff")) _contentType = "font/woff";
  82. else if (path.endsWith(".woff2")) _contentType = "font/woff2";
  83. else if (path.endsWith(".ttf")) _contentType = "font/ttf";
  84. else if (path.endsWith(".xml")) _contentType = "text/xml";
  85. else if (path.endsWith(".pdf")) _contentType = "application/pdf";
  86. else if (path.endsWith(".zip")) _contentType = "application/zip";
  87. else if (path.endsWith(".gz")) _contentType = "application/x-gzip";
  88. else if (path.endsWith(".mp3")) _contentType = "audio/mpeg";
  89. else if (path.endsWith(".mp4")) _contentType = "video/mp4";
  90. else if (path.endsWith(".aac")) _contentType = "audio/aac";
  91. else if (path.endsWith(".ogg")) _contentType = "audio/ogg";
  92. else if (path.endsWith(".wav")) _contentType = "audio/wav";
  93. else if (path.endsWith(".m4v")) _contentType = "video/x-m4v";
  94. else if (path.endsWith(".webm")) _contentType = "video/webm";
  95. else _contentType = "text/plain";
  96. return _contentType;
  97. }