diff --git a/include/http_server.h b/include/http_server.h index 85f3ac2..6a17189 100644 --- a/include/http_server.h +++ b/include/http_server.h @@ -4,6 +4,7 @@ #ifdef HTTP_SERVER_ENABLE #include "my_wifi.h" +#include #if defined ( ESP8266 ) extern ESP8266WebServer http_server; @@ -11,6 +12,8 @@ extern ESP8266WebServer http_server; extern ESP32WebServer http_server; #endif +extern File upload_file; + void http_server_setup(); void http_server_loop(); #endif diff --git a/src/http_server.cpp b/src/http_server.cpp index ac1be4b..786cfee 100644 --- a/src/http_server.cpp +++ b/src/http_server.cpp @@ -6,6 +6,7 @@ #include "effects.h" #include "my_wifi.h" #include "prototypes.h" +#include #if defined( ESP8266 ) ESP8266WebServer http_server(HTTP_SERVER_PORT); @@ -13,6 +14,26 @@ ESP8266WebServer http_server(HTTP_SERVER_PORT); ESP32WebServer http_server(HTTP_SERVER_PORT); #endif +File upload_file; + +void http_server_handle_file_upload() { + if (http_server.uri() != "/upload") return; + + HTTPUpload upload = http_server.upload(); + + if (upload.status == UPLOAD_FILE_START) { + String filename = upload.filename; + if (!filename.startsWith("/")) filename = "/" + filename; + LOGln("HTTP * Upload of %s starting...", upload.filename.c_str()); + upload_file = SPIFFS.open(filename, "w"); + } else if (upload.status == UPLOAD_FILE_WRITE) { + if (upload_file) upload_file.write(upload.buf, upload.currentSize); + } else if (upload.status == UPLOAD_FILE_END) { + if (upload_file) upload_file.close(); + LOGln("HTTP * Upload of %s with %d bytes done.", upload.filename.c_str(), upload.totalSize); + } +} + void http_server_400() { http_server.send(400); } @@ -21,8 +42,43 @@ void http_server_setup() { PGM_P text_plain = PSTR("text/plain"); http_server.on("/", HTTP_GET, [&](){ LOGln("HTTP * GET /"); - http_server.send_P(200, text_plain, PSTR("Welcome to pitrix.")); + String message = "Pitrix

Pitrix

Known animations:

"; + if (!SPIFFS.begin()) { + message += "No SPIFFS file system found."; + } else { + message += ""; + message += "
"; + } + message += ""; + http_server.send(200, "text/html", message); }); + http_server.on("/delete", HTTP_GET, [&]() { + LOGln("HTTP * GET /delete"); + if (http_server.args()==0) { + http_server.send_P(400, text_plain, PSTR("No filename given")); + return; + } + String file = http_server.arg(0); + if (file == "/") { + http_server.send_P(400, text_plain, PSTR("Invalid path")); + return; + } + if (!SPIFFS.exists(file)) { + http_server.send_P(400, text_plain, PSTR("File does not exist.")); + return; + } + SPIFFS.remove(file); + http_server.send_P(200, text_plain, PSTR("OK")); + }); + http_server.on("/upload", HTTP_POST, []() { + LOGln("HTTP * POST /upload"); + http_server.send(200, "text/plain", "OK"); + }, http_server_handle_file_upload); http_server.on("/free_heap", HTTP_GET, [&](){ LOGln("HTTP * GET /free_heap"); http_server.send(200, "text/plain", String(ESP.getFreeHeap())); @@ -40,7 +96,7 @@ void http_server_setup() { ESP.restart(); }); http_server.on("/brightness", HTTP_POST, [&](){ - LOG("HTTP * POST /brightness with value "); LOGln(http_server.arg("plain")); + LOGln("HTTP * POST /brightness with value %s", http_server.arg("plain").c_str()); if (!http_server.hasArg("plain")) { http_server.send_P(400, text_plain, PSTR("No brightness given")); return; @@ -54,23 +110,17 @@ void http_server_setup() { http_server.send(200, "text/plain", "OK"); }); http_server.on("/mode", HTTP_POST, [&](){ - LOGln("HTTP * POST /mode with value "); LOGln(http_server.arg("plain")); + LOGln("HTTP * POST /mode with value %s", http_server.arg("plain").c_str()); if (!http_server.hasArg("plain")) { http_server.send_P(400, text_plain, PSTR("No effect given.")); return; } String val = http_server.arg("plain"); - for (int i=0; isize(); i++) { - EffectEntry e = effects->get(i); - if (val.compareTo(e.name)==0) { - current_effect->stop(); - current_effect = e.effect; - current_effect->start(); - http_server.send(200, "text/plain", "OK"); - return; - } + if (change_current_effect(val)) { + http_server.send(200, "text/plain", "OK"); + } else { + http_server.send_P(400, text_plain, PSTR("Unknown effect.")); } - http_server.send_P(400, text_plain, PSTR("Unknown effect.")); }); http_server.begin();