Включаем технический редактор файлов

This commit is contained in:
2022-05-19 00:14:53 +03:00
parent f4a1ff70ca
commit 85289144bc
4 changed files with 43 additions and 24 deletions

0
data_svelte/dev_conf.txt Normal file
View File

BIN
data_svelte/edit.htm.gz Normal file

Binary file not shown.

View File

@@ -5,10 +5,10 @@
extern void standWebServerInit(); extern void standWebServerInit();
extern bool handleFileRead(String path); extern bool handleFileRead(String path);
extern String getContentType(String filename); extern String getContentType(String filename);
#ifdef REST_FILE_OPERATIONS //#ifdef REST_FILE_OPERATIONS
extern void handleFileUpload(); extern void handleFileUpload();
extern void handleFileDelete(); extern void handleFileDelete();
extern void handleFileCreate(); extern void handleFileCreate();
extern void handleFileList(); extern void handleFileList();
#endif //#endif
#endif #endif

View File

@@ -48,36 +48,53 @@ void standWebServerInit() {
// Запускаем HTTP сервер // Запускаем HTTP сервер
HTTP.begin(); HTTP.begin();
#ifdef REST_FILE_OPERATIONS //#ifdef REST_FILE_OPERATIONS
SPIFFS.begin(); // SPIFFS.begin();
{ // {
Dir dir = SPIFFS.openDir("/"); // Dir dir = SPIFFS.openDir("/");
while (dir.next()) { // while (dir.next()) {
String fileName = dir.fileName(); // String fileName = dir.fileName();
size_t fileSize = dir.fileSize(); // size_t fileSize = dir.fileSize();
} // }
} // }
// HTTP страницы для работы с FFS // HTTP страницы для работы с FFS
// list directory // list directory
HTTP.on("/list", HTTP_GET, handleFileList); HTTP.on("/list", HTTP_GET, handleFileList);
//загрузка редактора editor //загрузка редактора editor
HTTP.on("/edit", HTTP_GET, []() { HTTP.on("/edit", HTTP_GET, []() {
if (!handleFileRead("/edit.htm")) HTTP.send(404, "text/plain", "FileNotFound"); if (!HTTP.args()) {
if (!handleFileRead("/edit.htm")) HTTP.send(404, "text/plain", "FileNotFound");
}
if (HTTP.hasArg("list")) {
handleFileList();
}
if (HTTP.hasArg("edit")) {
if (!handleFileRead(HTTP.arg("edit"))) HTTP.send(404, "text/plain", "FileNotFound");
}
if (HTTP.hasArg("download")) {
if (!handleFileRead(HTTP.arg("download"))) HTTP.send(404, "text/plain", "FileNotFound");
}
}); });
//Создание файла //Создание файла
HTTP.on("/edit", HTTP_PUT, handleFileCreate); HTTP.on("/edit", HTTP_PUT, handleFileCreate);
//Удаление файла //Удаление файла
HTTP.on("/edit", HTTP_DELETE, handleFileDelete); HTTP.on("/edit", HTTP_DELETE, handleFileDelete);
// first callback is called after the request has ended with all parsed arguments
// second callback handles file uploads at that location //Изменение файла
HTTP.on( HTTP.on("/edit", HTTP_POST, []() {
"/edit", HTTP_POST, []() {
HTTP.send(200, "text/plain", ""); HTTP.send(200, "text/plain", "");
}, },
handleFileUpload); handleFileUpload);
#endif //#endif
// called when the url is not defined here // called when the url is not defined here
// use it to load content from SPIFFS
HTTP.onNotFound([]() { HTTP.onNotFound([]() {
if (!handleFileRead(HTTP.uri())) if (!handleFileRead(HTTP.uri()))
HTTP.send(404, "text/plain", "FileNotFound"); HTTP.send(404, "text/plain", "FileNotFound");
@@ -92,6 +109,8 @@ bool handleFileRead(String path) {
if (FileFS.exists(pathWithGz)) if (FileFS.exists(pathWithGz))
path += ".gz"; path += ".gz";
File file = FileFS.open(path, "r"); File file = FileFS.open(path, "r");
if (contentType == "application/octet-stream")
HTTP.sendHeader("Content-Disposition", "attachment;filename=" + (String)file.name());
HTTP.streamFile(file, contentType); HTTP.streamFile(file, contentType);
file.close(); file.close();
return true; return true;
@@ -131,7 +150,7 @@ String getContentType(String filename) {
return "text/plain"; return "text/plain";
} }
#ifdef REST_FILE_OPERATIONS //#ifdef REST_FILE_OPERATIONS
// Здесь функции для работы с файловой системой // Здесь функции для работы с файловой системой
void handleFileUpload() { void handleFileUpload() {
if (HTTP.uri() != "/edit") return; if (HTTP.uri() != "/edit") return;
@@ -181,27 +200,27 @@ void handleFileCreate() {
} }
void handleFileList() { void handleFileList() {
if (!HTTP.hasArg("dir")) { if (!HTTP.hasArg("list")) {
HTTP.send(500, "text/plain", "BAD ARGS"); HTTP.send(500, "text/plain", "BAD ARGS");
return; return;
} }
String path = HTTP.arg("dir"); String path = HTTP.arg("list");
Dir dir = FileFS.openDir(path); Dir dir = FileFS.openDir(path);
path = String(); path = String();
String output = "["; String output = "[";
while (dir.next()) { while (dir.next()) {
File entry = dir.openFile("r"); File entry = dir.openFile("r");
if (output != "[") output += ','; if (output != "[") output += ',';
bool isDir = false; bool isDir = dir.isDirectory();
output += "{\"type\":\""; output += "{\"type\":\"";
output += (isDir) ? "dir" : "file"; output += (isDir) ? "dir" : "file";
output += "\",\"name\":\""; output += "\",\"name\":\"";
output += String(entry.name()).substring(1); output += String(entry.name());
output += "\"}"; output += "\"}";
entry.close(); entry.close();
} }
output += "]"; output += "]";
HTTP.send(200, "text/json", output); HTTP.send(200, "text/json", output);
} }
#endif //#endif
#endif #endif