diff --git a/data/www/script.js b/data/www/script.js
index 7022a75..8d07030 100644
--- a/data/www/script.js
+++ b/data/www/script.js
@@ -9,19 +9,7 @@ function loadAvailableFiles() {
console.log("Available files...");
fetch("/available-files")
.then(response => response.json())
- .then(data => {
- console.log("data", data);
- let dom = "";
- data.files.forEach(element => {
- if (element === data.selectedFile) {
- dom += ``;
- }
- else {
- dom += ``;
- }
- });
- document.getElementById("available-files").innerHTML = dom;
- });
+ .then(handleAvailableFiles);
}
function selectFile(name) {
@@ -29,7 +17,22 @@ function selectFile(name) {
const body = new FormData();
body.set("fileName", name);
fetch("/select-file", {method: "POST", body: body})
- .then(res => loadAvailableFiles());
+ .then(response => response.json())
+ .then(handleAvailableFiles);
+}
+
+function handleAvailableFiles(data) {
+ console.log("data", data);
+ let dom = "";
+ data.files.forEach(element => {
+ if (element === data.selectedFile) {
+ dom += ``;
+ }
+ else {
+ dom += ``;
+ }
+ });
+ document.getElementById("available-files").innerHTML = dom;
}
(() => {
diff --git a/src/main.cpp b/src/main.cpp
index 968b28d..7e21d0c 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -32,7 +32,7 @@ void onAvailableFiles(AsyncWebServerRequest *request)
{
Serial.println("Available files");
AsyncResponseStream *response = request->beginResponseStream("application/json");
-
+
DynamicJsonDocument root(256);
root["selectedFile"] = selectedFile;
JsonArray files = root.createNestedArray("files");
@@ -40,15 +40,14 @@ void onAvailableFiles(AsyncWebServerRequest *request)
File file = music.openNextFile();
while (file)
{
- Serial.print("File: ");
String fileName = file.name();
- Serial.println(fileName);
- files.add(fileName);
+ if (!fileName.startsWith("."))
+ files.add(fileName);
file.close();
file = music.openNextFile();
}
serializeJson(root, *response);
-
+
request->send(response);
}
@@ -61,7 +60,7 @@ void onSelectFile(AsyncWebServerRequest *request)
Serial.print(selectedFile);
}
Serial.println();
- request->send(200);
+ onAvailableFiles(request);
}
void setup()
@@ -79,12 +78,13 @@ void setup()
}
// List existing files
- File root = SPIFFS.open("/");
+ File root = SPIFFS.open("/music");
File file = root.openNextFile();
while (file)
{
- Serial.print("File: ");
- Serial.println(file.path());
+ String fileName = file.name();
+ if (selectedFile == "" && !fileName.startsWith("."))
+ selectedFile = fileName;
file.close();
file = root.openNextFile();
}