Extended http server to list files in SPIFFS and allow you to upload and delete files.
This commit is contained in:
parent
e52d7c750d
commit
f1424d0277
@ -4,6 +4,7 @@
|
||||
#ifdef HTTP_SERVER_ENABLE
|
||||
|
||||
#include "my_wifi.h"
|
||||
#include <FS.h>
|
||||
|
||||
#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
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include "effects.h"
|
||||
#include "my_wifi.h"
|
||||
#include "prototypes.h"
|
||||
#include <FS.h>
|
||||
|
||||
#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 = "<html><head><title>Pitrix</title></head><body><h1>Pitrix</h1><p>Known animations:</p>";
|
||||
if (!SPIFFS.begin()) {
|
||||
message += "<strong>No SPIFFS file system found.</strong>";
|
||||
} else {
|
||||
message += "<ul>";
|
||||
Dir dir = SPIFFS.openDir("/");
|
||||
while (dir.next()) {
|
||||
message += "<li>" + dir.fileName() + " (<a href='/delete?" + dir.fileName() + "'>delete</a>)</li>";
|
||||
}
|
||||
message += "</ul>";
|
||||
message += "<form action='/upload' method='POST'><input type='file' name='file' /><input type='submit' value='Upload file' /></form>";
|
||||
}
|
||||
message += "</body></html>";
|
||||
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; i<effects->size(); i++) {
|
||||
EffectEntry e = effects->get(i);
|
||||
if (val.compareTo(e.name)==0) {
|
||||
current_effect->stop();
|
||||
current_effect = e.effect;
|
||||
current_effect->start();
|
||||
if (change_current_effect(val)) {
|
||||
http_server.send(200, "text/plain", "OK");
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
http_server.send_P(400, text_plain, PSTR("Unknown effect."));
|
||||
}
|
||||
});
|
||||
http_server.begin();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user