🗃️ Improves UI filesystem interface
This commit is contained in:
@@ -44,23 +44,28 @@ bool deleteFile(const char *filename) { return ESPFS.remove(filename); }
|
||||
|
||||
String listFiles(const String &directory, bool isRoot) {
|
||||
File root = ESPFS.open(directory.startsWith("/") ? directory : "/" + directory);
|
||||
if (!root.isDirectory()) return "";
|
||||
if (!root.isDirectory()) return "{}";
|
||||
|
||||
File file = root.openNextFile();
|
||||
if (!file) {
|
||||
return isRoot ? "{ \"root\": {} }" : "{}";
|
||||
}
|
||||
|
||||
String output = isRoot ? "{ \"root\": {" : "{";
|
||||
|
||||
while (file) {
|
||||
String name = String(file.name());
|
||||
if (file.isDirectory()) {
|
||||
output += "\"" + String(file.name()) + "\": " + listFiles(file.name(), false) + ", ";
|
||||
output += "\"" + name + "\": " + listFiles(name, false);
|
||||
} else {
|
||||
output += "\"" + String(file.name()) + "\": " + String(file.size()) + ", ";
|
||||
output += "\"" + name + "\": " + String(file.size());
|
||||
}
|
||||
file = root.openNextFile();
|
||||
|
||||
File next = root.openNextFile();
|
||||
if (next) output += ", ";
|
||||
file = next;
|
||||
}
|
||||
|
||||
if (output.endsWith(", ")) {
|
||||
output.remove(output.length() - 2);
|
||||
}
|
||||
output += "}";
|
||||
if (isRoot) output += "}";
|
||||
|
||||
@@ -98,4 +103,10 @@ bool editFile(const char *filename, const char *content) {
|
||||
return true;
|
||||
}
|
||||
|
||||
esp_err_t mkdir(PsychicRequest *request, JsonVariant &json) {
|
||||
const char *path = json["path"].as<const char *>();
|
||||
ESP_LOGI(TAG, "Creating directory: %s", path);
|
||||
return ESPFS.mkdir(path) ? request->reply(200) : request->reply(500);
|
||||
}
|
||||
|
||||
} // namespace FileSystem
|
||||
@@ -26,4 +26,6 @@ esp_err_t uploadFile(PsychicRequest *request, const String &filename, uint64_t i
|
||||
esp_err_t getFiles(PsychicRequest *request);
|
||||
esp_err_t handleDelete(PsychicRequest *request, JsonVariant &json);
|
||||
esp_err_t handleEdit(PsychicRequest *request, JsonVariant &json);
|
||||
|
||||
esp_err_t mkdir(PsychicRequest *request, JsonVariant &json);
|
||||
} // namespace FileSystem
|
||||
Reference in New Issue
Block a user