From eec3703b7eaab6a4cf96b0f26b87f65f4c121747 Mon Sep 17 00:00:00 2001 From: Matthias Blankertz Date: Sun, 21 Dec 2025 14:22:33 +0100 Subject: [PATCH] feat: Extend audiofiles API Extend the audiofiles GET API to return both directories and audio files. Also change the JSON format to include the name and type of all entries. Signed-off-by: Matthias Blankertz --- software/frontend/index.html | 8 ++++---- software/src/webserver.py | 17 +++++++++++------ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/software/frontend/index.html b/software/frontend/index.html index d8a9103..29576cf 100644 --- a/software/frontend/index.html +++ b/software/frontend/index.html @@ -565,6 +565,7 @@ function selectTag(tag) { const container = document.getElementById('playlist-exist-list'); + if (tag.tag === null) return; const tagtext = bytesToHex(tag.tag); document.getElementById('playlist-exist-list') .querySelectorAll("li") @@ -797,17 +798,16 @@ const rootnode = document.createElement('ul'); tree.appendChild(rootnode); - files.sort(); + files.sort((a, b) => a.name < b.name ? -1 : (a.name > b.name ? 1 : 0)); for (const file of files) { - const path = file.split('/').slice(1); + const path = file.name.split('/').slice(1); let treeIterator = rootnode; let curPath = '' for (const elem of path) { curPath += '/' + elem; let node = findChildByName(treeIterator, elem); if (node === null) { - const isFile = path.slice(-1)[0] === elem; - node = addTreeNode(treeIterator, elem, isFile ? 'file':'directory', curPath); + node = addTreeNode(treeIterator, elem, file.type, curPath); } treeIterator = node; } diff --git a/software/src/webserver.py b/software/src/webserver.py index 85f4917..c56fad2 100644 --- a/software/src/webserver.py +++ b/software/src/webserver.py @@ -162,6 +162,15 @@ async def audiofiles_get(request): def directory_iterator(): yield '[' first = True + + def make_json_str(obj): + nonlocal first + jsonpath = json.dumps(obj) + if not first: + jsonpath = ',' + jsonpath + first = False + return jsonpath + dirstack = [fsroot] while dirstack: current_dir = dirstack.pop() @@ -170,15 +179,11 @@ async def audiofiles_get(request): type_ = entry[1] current_path = current_dir + b'/' + name if type_ == 0x4000: + yield make_json_str({'name': current_path[len(fsroot):], 'type': 'directory'}) dirstack.append(current_path) elif type_ == 0x8000: if name.lower().endswith('.mp3'): - jsonpath = json.dumps(current_path[len(fsroot):]) - if not first: - yield ','+jsonpath - else: - yield jsonpath - first = False + yield make_json_str({'name': current_path[len(fsroot):], 'type': 'file'}) yield ']' return directory_iterator(), {'Content-Type': 'application/json; charset=UTF-8'}