| 1086 | } |
| 1087 | |
| 1088 | void handleFileList(AsyncWebServerRequest* request) |
| 1089 | { |
| 1090 | if(!httpIsAuthenticated(request, F("filelist"))) return; |
| 1091 | |
| 1092 | if(!request->hasArg(F("dir"))) { |
| 1093 | request->send(500, PSTR("text/plain"), PSTR("BAD ARGS")); |
| 1094 | return; |
| 1095 | } |
| 1096 | |
| 1097 | String path = request->arg(F("dir")); |
| 1098 | // LOG_TRACE(TAG_HTTP, F("handleFileList: %s"), path.c_str()); |
| 1099 | path.clear(); |
| 1100 | |
| 1101 | #if defined(ARDUINO_ARCH_ESP32) |
| 1102 | File root = HASP_FS.open("/", FILE_READ); |
| 1103 | File file = root.openNextFile(); |
| 1104 | String output((char*)0); |
| 1105 | output.reserve(HTTP_PAGE_SIZE); |
| 1106 | output = "["; |
| 1107 | |
| 1108 | while(file) { |
| 1109 | if(output != "[") { |
| 1110 | output += ','; |
| 1111 | } |
| 1112 | bool isDir = false; |
| 1113 | output += F("{\"type\":\""); |
| 1114 | output += (isDir) ? F("dir") : F("file"); |
| 1115 | output += F("\",\"name\":\""); |
| 1116 | if(file.name()[0] == '/') { |
| 1117 | output += &(file.name()[1]); |
| 1118 | } else { |
| 1119 | output += file.name(); |
| 1120 | } |
| 1121 | output += F("\"}"); |
| 1122 | |
| 1123 | // file.close(); |
| 1124 | file = root.openNextFile(); |
| 1125 | } |
| 1126 | output += "]"; |
| 1127 | request->send(200, PSTR("text/json"), output); |
| 1128 | #elif defined(ARDUINO_ARCH_ESP8266) |
| 1129 | Dir dir = HASP_FS.openDir(path); |
| 1130 | String output((char*)0); |
| 1131 | output.reserve(HTTP_PAGE_SIZE); |
| 1132 | output = "["; |
| 1133 | |
| 1134 | while(dir.next()) { |
| 1135 | File entry = dir.openFile("r"); |
| 1136 | if(output != "[") { |
| 1137 | output += ','; |
| 1138 | } |
| 1139 | bool isDir = false; |
| 1140 | output += F("{\"type\":\""); |
| 1141 | output += (isDir) ? F("dir") : F("file"); |
| 1142 | output += F("\",\"name\":\""); |
| 1143 | if(entry.name()[0] == '/') { |
| 1144 | output += &(entry.name()[1]); |
| 1145 | } else { |
nothing calls this directly
no test coverage detected