MCPcopy Create free account
hub / github.com/HASwitchPlate/openHASP / handleFileList

Function handleFileList

src/sys/svc/hasp_http.cpp:973–1039  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

971}
972
973static void handleFileList()
974{
975 if(!httpIsAuthenticated(F("filelist"))) return;
976
977 if(!webServer.hasArg(F("dir"))) {
978 webServer.send(500, PSTR("text/plain"), PSTR("BAD ARGS"));
979 return;
980 }
981
982 String path = webServer.arg(F("dir"));
983 // LOG_TRACE(TAG_HTTP, F("handleFileList: %s"), path.c_str());
984 path.clear();
985
986#if defined(ARDUINO_ARCH_ESP32)
987 File root = HASP_FS.open("/", FILE_READ);
988 File file = root.openNextFile();
989 String output((char*)0);
990 output.reserve(HTTP_PAGE_SIZE);
991 output = "[";
992
993 while(file) {
994 if(output != "[") {
995 output += ',';
996 }
997 bool isDir = false;
998 output += F("{\"type\":\"");
999 output += (isDir) ? F("dir") : F("file");
1000 output += F("\",\"name\":\"");
1001 if(file.name()[0] == '/') {
1002 output += &(file.name()[1]);
1003 } else {
1004 output += file.name();
1005 }
1006 output += F("\"}");
1007
1008 // file.close();
1009 file = root.openNextFile();
1010 }
1011 output += "]";
1012 webServer.send(200, PSTR("text/json"), output);
1013#elif defined(ARDUINO_ARCH_ESP8266)
1014 Dir dir = HASP_FS.openDir(path);
1015 String output((char*)0);
1016 output.reserve(HTTP_PAGE_SIZE);
1017 output = "[";
1018
1019 while(dir.next()) {
1020 File entry = dir.openFile("r");
1021 if(output != "[") {
1022 output += ',';
1023 }
1024 bool isDir = false;
1025 output += F("{\"type\":\"");
1026 output += (isDir) ? F("dir") : F("file");
1027 output += F("\",\"name\":\"");
1028 if(entry.name()[0] == '/') {
1029 output += &(entry.name()[1]);
1030 } else {

Callers

nothing calls this directly

Calls 5

clearMethod · 0.80
openDirMethod · 0.80
nextMethod · 0.80
openFileMethod · 0.80
httpIsAuthenticatedFunction · 0.70

Tested by

no test coverage detected