mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 20:09:14 +03:00
FSEditor
This commit is contained in:
106
src/FSEditor.cpp
106
src/FSEditor.cpp
@@ -174,81 +174,125 @@ void FSEditor::handleRequest(AsyncWebServerRequest *request) {
|
|||||||
if (request->method() == HTTP_GET) {
|
if (request->method() == HTTP_GET) {
|
||||||
if (request->hasParam("list")) {
|
if (request->hasParam("list")) {
|
||||||
String path = request->getParam("list")->value();
|
String path = request->getParam("list")->value();
|
||||||
#if ESP32
|
#ifdef ESP32
|
||||||
auto dir = _fs.open(path);
|
File dir = _fs.open(path);
|
||||||
#else
|
#else
|
||||||
auto dir = _fs.openDir(path);
|
fs::Dir dir = _fs.openDir(path);
|
||||||
#endif
|
#endif
|
||||||
path = String();
|
path = String();
|
||||||
String output = "[";
|
String output = "[";
|
||||||
#if ESP32
|
#ifdef ESP32
|
||||||
while (dir.openNextFile()) {
|
File entry = dir.openNextFile();
|
||||||
|
while (entry) {
|
||||||
#else
|
#else
|
||||||
while (dir.next()) {
|
while (dir.next()) {
|
||||||
|
fs::File entry = dir.openFile("r");
|
||||||
#endif
|
#endif
|
||||||
#if ESP32
|
String fname = entry.fullName();
|
||||||
if (isExcluded(_fs, dir.name())) {
|
if (fname.charAt(0) != '/') fname = "/" + fname;
|
||||||
#else
|
|
||||||
if (isExcluded(_fs, dir.fileName().c_str())) {
|
if (isExcluded(_fs, fname.c_str())) {
|
||||||
|
#ifdef ESP32
|
||||||
|
entry = dir.openNextFile();
|
||||||
#endif
|
#endif
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (output != "[") output += ',';
|
if (output != "[") output += ',';
|
||||||
output += "{\"type\":\"";
|
output += "{\"type\":\"";
|
||||||
#if ESP32
|
output += "file";
|
||||||
output += dir.isDirectory() ? "file" : "dir";
|
|
||||||
#else
|
|
||||||
output += dir.isFile() ? "file" : "dir";
|
|
||||||
#endif
|
|
||||||
|
|
||||||
output += "\",\"name\":\"";
|
output += "\",\"name\":\"";
|
||||||
#if ESP32
|
output += String(fname);
|
||||||
output += dir.name();
|
|
||||||
#else
|
|
||||||
output += dir.fileName();
|
|
||||||
#endif
|
|
||||||
output += "\",\"size\":";
|
output += "\",\"size\":";
|
||||||
#if ESP32
|
output += String(entry.size());
|
||||||
output += String(dir.size());
|
|
||||||
#else
|
|
||||||
|
|
||||||
output += String(dir.fileSize());
|
|
||||||
#endif
|
|
||||||
output += "}";
|
output += "}";
|
||||||
|
#ifdef ESP32
|
||||||
|
entry = dir.openNextFile();
|
||||||
|
#else
|
||||||
|
entry.close();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
#ifdef ESP32
|
||||||
|
dir.close();
|
||||||
|
#endif
|
||||||
output += "]";
|
output += "]";
|
||||||
request->send(200, "application/json", output);
|
request->send(200, "application/json", output);
|
||||||
output = String();
|
output = String();
|
||||||
} else if (request->hasParam("edit") || request->hasParam("download")) {
|
} else if (request->hasParam("edit") || request->hasParam("download")) {
|
||||||
request->send(request->_tempFile, request->_tempFile.name(), String(), request->hasParam("download"));
|
request->send(request->_tempFile, request->_tempFile.fullName(), String(), request->hasParam("download"));
|
||||||
} else {
|
} else {
|
||||||
const char *buildTime = __DATE__ " " __TIME__ " GMT";
|
const char *buildTime = __DATE__ " " __TIME__ " GMT";
|
||||||
if (request->header("If-Modified-Since").equals(buildTime)) {
|
if (request->header("If-Modified-Since").equals(buildTime)) {
|
||||||
request->send(304);
|
request->send(304);
|
||||||
} else {
|
} else {
|
||||||
AsyncWebServerResponse *response = request->beginResponse(_fs, "/edit.htm");
|
AsyncWebServerResponse *response = request->beginResponse(LittleFS, "/edit.htm", "text/html");
|
||||||
|
// response->addHeader("Content-Encoding", "gzip");
|
||||||
response->addHeader("Last-Modified", buildTime);
|
response->addHeader("Last-Modified", buildTime);
|
||||||
request->send(response);
|
request->send(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (request->method() == HTTP_DELETE) {
|
} else if (request->method() == HTTP_DELETE) {
|
||||||
if (request->hasParam("path", true)) {
|
if (request->hasParam("path", true)) {
|
||||||
_fs.remove(request->getParam("path", true)->value());
|
if (!(_fs.remove(request->getParam("path", true)->value()))) {
|
||||||
|
#ifdef ESP32
|
||||||
|
_fs.rmdir(request->getParam("path", true)->value()); // try rmdir for littlefs
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
request->send(200, "", "DELETE: " + request->getParam("path", true)->value());
|
request->send(200, "", "DELETE: " + request->getParam("path", true)->value());
|
||||||
} else
|
} else
|
||||||
request->send(404);
|
request->send(404);
|
||||||
} else if (request->method() == HTTP_POST) {
|
} else if (request->method() == HTTP_POST) {
|
||||||
if (request->hasParam("data", true, true) && _fs.exists(request->getParam("data", true, true)->value()))
|
if (request->hasParam("data", true, true) && _fs.exists(request->getParam("data", true, true)->value()))
|
||||||
request->send(200, "", "UPLOADED: " + request->getParam("data", true, true)->value());
|
request->send(200, "", "UPLOADED: " + request->getParam("data", true, true)->value());
|
||||||
else
|
|
||||||
|
else if (request->hasParam("rawname", true) && request->hasParam("raw0", true)) {
|
||||||
|
String rawnam = request->getParam("rawname", true)->value();
|
||||||
|
|
||||||
|
if (_fs.exists(rawnam)) _fs.remove(rawnam); // delete it to allow a mode
|
||||||
|
|
||||||
|
int k = 0;
|
||||||
|
uint16_t i = 0;
|
||||||
|
fs::File f = _fs.open(rawnam, "a");
|
||||||
|
|
||||||
|
while (request->hasParam("raw" + String(k), true)) { //raw0 .. raw1
|
||||||
|
if (f) {
|
||||||
|
i += f.print(request->getParam("raw" + String(k), true)->value());
|
||||||
|
}
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
f.close();
|
||||||
|
request->send(200, "", "IPADWRITE: " + rawnam + ":" + String(i));
|
||||||
|
|
||||||
|
} else {
|
||||||
request->send(500);
|
request->send(500);
|
||||||
|
}
|
||||||
|
|
||||||
} else if (request->method() == HTTP_PUT) {
|
} else if (request->method() == HTTP_PUT) {
|
||||||
if (request->hasParam("path", true)) {
|
if (request->hasParam("path", true)) {
|
||||||
String filename = request->getParam("path", true)->value();
|
String filename = request->getParam("path", true)->value();
|
||||||
if (_fs.exists(filename)) {
|
if (_fs.exists(filename)) {
|
||||||
request->send(200);
|
request->send(200);
|
||||||
} else {
|
} else {
|
||||||
|
/*******************************************************/
|
||||||
|
#ifdef ESP32
|
||||||
|
if (strchr(filename.c_str(), '/')) {
|
||||||
|
// For file creation, silently make subdirs as needed. If any fail,
|
||||||
|
// it will be caught by the real file open later on
|
||||||
|
char *pathStr = strdup(filename.c_str());
|
||||||
|
if (pathStr) {
|
||||||
|
// Make dirs up to the final fnamepart
|
||||||
|
char *ptr = strchr(pathStr, '/');
|
||||||
|
while (ptr) {
|
||||||
|
*ptr = 0;
|
||||||
|
_fs.mkdir(pathStr);
|
||||||
|
*ptr = '/';
|
||||||
|
ptr = strchr(ptr + 1, '/');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(pathStr);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
/*******************************************************/
|
||||||
fs::File f = _fs.open(filename, "w");
|
fs::File f = _fs.open(filename, "w");
|
||||||
if (f) {
|
if (f) {
|
||||||
f.write((uint8_t)0x00);
|
f.write((uint8_t)0x00);
|
||||||
|
|||||||
Reference in New Issue
Block a user