2021-12-23 22:42:19 +01:00
|
|
|
|
#include "StandWebServer.h"
|
|
|
|
|
|
#ifdef STANDARD_WEB_SERVER
|
2021-12-24 22:49:06 +01:00
|
|
|
|
|
|
|
|
|
|
File fsUploadFile;
|
|
|
|
|
|
|
2021-12-23 22:42:19 +01:00
|
|
|
|
void standWebServerInit() {
|
|
|
|
|
|
// Кэшировать файлы для быстрой работы
|
2022-09-19 13:56:23 +02:00
|
|
|
|
HTTP.serveStatic("/bundle.js", FileFS, "/", "max-age=31536000"); // кеширование на 1 год
|
|
|
|
|
|
HTTP.serveStatic("/bundle.css", FileFS, "/", "max-age=31536000"); // кеширование на 1 год
|
|
|
|
|
|
HTTP.serveStatic("/bundle.js.gz", FileFS, "/", "max-age=31536000"); // кеширование на 1 год
|
|
|
|
|
|
HTTP.serveStatic("/bundle.css.gz", FileFS, "/", "max-age=31536000"); // кеширование на 1 год
|
|
|
|
|
|
HTTP.serveStatic("/favicon.png", FileFS, "/", "max-age=31536000"); // кеширование на 1 год
|
2021-12-23 22:42:19 +01:00
|
|
|
|
|
2022-02-14 00:12:58 +01:00
|
|
|
|
HTTP.on("/devicelist.json", HTTP_GET, []() {
|
|
|
|
|
|
HTTP.send(200, "application/json", devListHeapJson);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
HTTP.on("/settings.h.json", HTTP_GET, []() {
|
2021-12-23 22:42:19 +01:00
|
|
|
|
HTTP.send(200, "application/json", settingsFlashJson);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-02-14 00:12:58 +01:00
|
|
|
|
HTTP.on("/settings.f.json", HTTP_GET, []() {
|
2022-02-12 03:09:57 +01:00
|
|
|
|
HTTP.send(200, "application/json", readFile(F("settings.json"), 20000));
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-02-14 00:12:58 +01:00
|
|
|
|
HTTP.on("/params.json", HTTP_GET, []() {
|
2022-03-01 01:43:30 +01:00
|
|
|
|
String json = getParamsJson();
|
|
|
|
|
|
HTTP.send(200, "application/json", json);
|
2022-02-08 00:20:49 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2022-02-13 00:40:15 +01:00
|
|
|
|
HTTP.on("/errors.json", HTTP_GET, []() {
|
|
|
|
|
|
HTTP.send(200, "application/json", errorsHeapJson);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-01-22 00:15:49 +01:00
|
|
|
|
HTTP.on("/config.json", HTTP_GET, []() {
|
2022-01-28 00:47:13 +01:00
|
|
|
|
HTTP.send(200, "application/json", readFile(F("config.json"), 20000));
|
2022-01-22 00:15:49 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2022-01-26 00:03:43 +01:00
|
|
|
|
HTTP.on("/layout.json", HTTP_GET, []() {
|
2022-01-28 00:47:13 +01:00
|
|
|
|
HTTP.send(200, "application/json", readFile(F("layout.json"), 20000));
|
2022-01-26 00:03:43 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2021-12-23 22:42:19 +01:00
|
|
|
|
HTTP.on("/restart", HTTP_GET, []() {
|
|
|
|
|
|
// ESP.restart();
|
|
|
|
|
|
HTTP.send(200, "text/plain", "ok");
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Добавляем функцию Update для перезаписи прошивки по WiFi при 1М(256K FileFS) и выше
|
|
|
|
|
|
// httpUpdater.setup(&HTTP);
|
|
|
|
|
|
// Запускаем HTTP сервер
|
|
|
|
|
|
HTTP.begin();
|
|
|
|
|
|
|
2022-08-24 00:21:20 +02:00
|
|
|
|
//#ifdef REST_FILE_OPERATIONS
|
2022-05-19 00:14:53 +03:00
|
|
|
|
// SPIFFS.begin();
|
|
|
|
|
|
// {
|
|
|
|
|
|
// Dir dir = SPIFFS.openDir("/");
|
|
|
|
|
|
// while (dir.next()) {
|
|
|
|
|
|
// String fileName = dir.fileName();
|
|
|
|
|
|
// size_t fileSize = dir.fileSize();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
2021-12-23 22:42:19 +01:00
|
|
|
|
// HTTP страницы для работы с FFS
|
2022-08-24 00:21:20 +02:00
|
|
|
|
|
2021-12-23 22:42:19 +01:00
|
|
|
|
// list directory
|
|
|
|
|
|
HTTP.on("/list", HTTP_GET, handleFileList);
|
2022-08-24 00:21:20 +02:00
|
|
|
|
|
2021-12-23 22:42:19 +01:00
|
|
|
|
//загрузка редактора editor
|
|
|
|
|
|
HTTP.on("/edit", HTTP_GET, []() {
|
2022-05-19 00:14:53 +03:00
|
|
|
|
if (!HTTP.args()) {
|
|
|
|
|
|
if (!handleFileRead("/edit.htm")) HTTP.send(404, "text/plain", "FileNotFound");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (HTTP.hasArg("list")) {
|
2022-08-24 00:21:20 +02:00
|
|
|
|
handleFileList();
|
2022-05-19 00:14:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (HTTP.hasArg("edit")) {
|
2022-08-24 00:21:20 +02:00
|
|
|
|
if (!handleFileRead(HTTP.arg("edit"))) HTTP.send(404, "text/plain", "FileNotFound");
|
2022-05-19 00:14:53 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (HTTP.hasArg("download")) {
|
2022-08-24 00:21:20 +02:00
|
|
|
|
if (!handleFileRead(HTTP.arg("download"))) HTTP.send(404, "text/plain", "FileNotFound");
|
2022-05-19 00:14:53 +03:00
|
|
|
|
}
|
2021-12-23 22:42:19 +01:00
|
|
|
|
});
|
2022-08-24 00:21:20 +02:00
|
|
|
|
|
2021-12-23 22:42:19 +01:00
|
|
|
|
//Создание файла
|
|
|
|
|
|
HTTP.on("/edit", HTTP_PUT, handleFileCreate);
|
2022-08-24 00:21:20 +02:00
|
|
|
|
|
2021-12-23 22:42:19 +01:00
|
|
|
|
//Удаление файла
|
|
|
|
|
|
HTTP.on("/edit", HTTP_DELETE, handleFileDelete);
|
2022-08-24 00:21:20 +02:00
|
|
|
|
|
2022-05-19 00:14:53 +03:00
|
|
|
|
//Изменение файла
|
2022-08-24 00:21:20 +02:00
|
|
|
|
HTTP.on(
|
|
|
|
|
|
"/edit", HTTP_POST, []() {
|
2021-12-23 22:42:19 +01:00
|
|
|
|
HTTP.send(200, "text/plain", "");
|
|
|
|
|
|
},
|
|
|
|
|
|
handleFileUpload);
|
2022-08-24 00:21:20 +02:00
|
|
|
|
//#endif
|
|
|
|
|
|
|
2021-12-23 22:42:19 +01:00
|
|
|
|
// called when the url is not defined here
|
|
|
|
|
|
HTTP.onNotFound([]() {
|
|
|
|
|
|
if (!handleFileRead(HTTP.uri()))
|
|
|
|
|
|
HTTP.send(404, "text/plain", "FileNotFound");
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool handleFileRead(String path) {
|
|
|
|
|
|
if (path.endsWith("/")) path += "index.html";
|
|
|
|
|
|
String contentType = getContentType(path);
|
|
|
|
|
|
String pathWithGz = path + ".gz";
|
|
|
|
|
|
if (FileFS.exists(pathWithGz) || FileFS.exists(path)) {
|
|
|
|
|
|
if (FileFS.exists(pathWithGz))
|
|
|
|
|
|
path += ".gz";
|
|
|
|
|
|
File file = FileFS.open(path, "r");
|
2022-08-24 00:21:20 +02:00
|
|
|
|
if (contentType == "application/octet-stream")
|
2022-05-19 00:14:53 +03:00
|
|
|
|
HTTP.sendHeader("Content-Disposition", "attachment;filename=" + (String)file.name());
|
2021-12-23 23:47:13 +01:00
|
|
|
|
HTTP.streamFile(file, contentType);
|
2021-12-23 22:42:19 +01:00
|
|
|
|
file.close();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String getContentType(String filename) {
|
|
|
|
|
|
if (HTTP.hasArg("download"))
|
|
|
|
|
|
return "application/octet-stream";
|
|
|
|
|
|
else if (filename.endsWith(".htm"))
|
|
|
|
|
|
return "text/html";
|
|
|
|
|
|
else if (filename.endsWith(".html"))
|
|
|
|
|
|
return "text/html";
|
2022-08-27 00:57:19 +02:00
|
|
|
|
else if (filename.endsWith(".txt"))
|
|
|
|
|
|
return "text/plain";
|
2021-12-23 22:42:19 +01:00
|
|
|
|
else if (filename.endsWith(".json"))
|
|
|
|
|
|
return "application/json";
|
|
|
|
|
|
else if (filename.endsWith(".css"))
|
|
|
|
|
|
return "text/css";
|
|
|
|
|
|
else if (filename.endsWith(".js"))
|
|
|
|
|
|
return "application/javascript";
|
|
|
|
|
|
else if (filename.endsWith(".png"))
|
|
|
|
|
|
return "image/png";
|
|
|
|
|
|
else if (filename.endsWith(".gif"))
|
|
|
|
|
|
return "image/gif";
|
|
|
|
|
|
else if (filename.endsWith(".jpg"))
|
|
|
|
|
|
return "image/jpeg";
|
|
|
|
|
|
else if (filename.endsWith(".ico"))
|
|
|
|
|
|
return "image/x-icon";
|
|
|
|
|
|
else if (filename.endsWith(".xml"))
|
|
|
|
|
|
return "text/xml";
|
|
|
|
|
|
else if (filename.endsWith(".pdf"))
|
|
|
|
|
|
return "application/x-pdf";
|
|
|
|
|
|
else if (filename.endsWith(".zip"))
|
|
|
|
|
|
return "application/x-zip";
|
|
|
|
|
|
else if (filename.endsWith(".gz"))
|
|
|
|
|
|
return "application/x-gzip";
|
|
|
|
|
|
return "text/plain";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-19 00:14:53 +03:00
|
|
|
|
//#ifdef REST_FILE_OPERATIONS
|
2021-12-23 22:42:19 +01:00
|
|
|
|
// Здесь функции для работы с файловой системой
|
|
|
|
|
|
void handleFileUpload() {
|
|
|
|
|
|
if (HTTP.uri() != "/edit") return;
|
|
|
|
|
|
HTTPUpload& upload = HTTP.upload();
|
|
|
|
|
|
if (upload.status == UPLOAD_FILE_START) {
|
|
|
|
|
|
String filename = upload.filename;
|
|
|
|
|
|
if (!filename.startsWith("/")) filename = "/" + filename;
|
|
|
|
|
|
fsUploadFile = FileFS.open(filename, "w");
|
|
|
|
|
|
filename = String();
|
|
|
|
|
|
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
|
|
|
|
|
// Serial.print("handleFileUpload Data: "); Serial.println(upload.currentSize);
|
|
|
|
|
|
if (fsUploadFile)
|
|
|
|
|
|
fsUploadFile.write(upload.buf, upload.currentSize);
|
|
|
|
|
|
} else if (upload.status == UPLOAD_FILE_END) {
|
|
|
|
|
|
if (fsUploadFile)
|
|
|
|
|
|
fsUploadFile.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void handleFileDelete() {
|
|
|
|
|
|
if (HTTP.args() == 0) return HTTP.send(500, "text/plain", "BAD ARGS");
|
|
|
|
|
|
String path = HTTP.arg(0);
|
|
|
|
|
|
if (path == "/")
|
|
|
|
|
|
return HTTP.send(500, "text/plain", "BAD PATH");
|
|
|
|
|
|
if (!FileFS.exists(path))
|
|
|
|
|
|
return HTTP.send(404, "text/plain", "FileNotFound");
|
|
|
|
|
|
FileFS.remove(path);
|
|
|
|
|
|
HTTP.send(200, "text/plain", "");
|
|
|
|
|
|
path = String();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void handleFileCreate() {
|
|
|
|
|
|
if (HTTP.args() == 0)
|
|
|
|
|
|
return HTTP.send(500, "text/plain", "BAD ARGS");
|
|
|
|
|
|
String path = HTTP.arg(0);
|
|
|
|
|
|
if (path == "/")
|
|
|
|
|
|
return HTTP.send(500, "text/plain", "BAD PATH");
|
|
|
|
|
|
if (FileFS.exists(path))
|
|
|
|
|
|
return HTTP.send(500, "text/plain", "FILE EXISTS");
|
|
|
|
|
|
File file = FileFS.open(path, "w");
|
|
|
|
|
|
if (file)
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
else
|
|
|
|
|
|
return HTTP.send(500, "text/plain", "CREATE FAILED");
|
|
|
|
|
|
HTTP.send(200, "text/plain", "");
|
|
|
|
|
|
path = String();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void handleFileList() {
|
2022-05-19 00:14:53 +03:00
|
|
|
|
if (!HTTP.hasArg("list")) {
|
2021-12-23 22:42:19 +01:00
|
|
|
|
HTTP.send(500, "text/plain", "BAD ARGS");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-09-19 13:56:23 +02:00
|
|
|
|
File dir = FileFS.open("/", "r");
|
2021-12-23 22:42:19 +01:00
|
|
|
|
String output = "[";
|
2022-05-19 09:48:28 +03:00
|
|
|
|
File entry;
|
|
|
|
|
|
while (entry = dir.openNextFile()) {
|
2021-12-23 22:42:19 +01:00
|
|
|
|
if (output != "[") output += ',';
|
2022-05-19 09:48:28 +03:00
|
|
|
|
bool isDir = entry.isDirectory();
|
2021-12-23 22:42:19 +01:00
|
|
|
|
output += "{\"type\":\"";
|
|
|
|
|
|
output += (isDir) ? "dir" : "file";
|
|
|
|
|
|
output += "\",\"name\":\"";
|
2022-05-19 00:14:53 +03:00
|
|
|
|
output += String(entry.name());
|
2021-12-23 22:42:19 +01:00
|
|
|
|
output += "\"}";
|
|
|
|
|
|
entry.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
output += "]";
|
2022-09-19 13:56:23 +02:00
|
|
|
|
Serial.println(output);
|
2021-12-23 22:42:19 +01:00
|
|
|
|
HTTP.send(200, "text/json", output);
|
|
|
|
|
|
}
|
2022-05-19 00:14:53 +03:00
|
|
|
|
//#endif
|
2021-12-23 22:42:19 +01:00
|
|
|
|
#endif
|