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 <matthias@blankertz.org>
This commit is contained in:
2025-12-21 14:22:33 +01:00
parent 25ac3f0687
commit eec3703b7e
2 changed files with 15 additions and 10 deletions

View File

@@ -565,6 +565,7 @@
function selectTag(tag) { function selectTag(tag) {
const container = document.getElementById('playlist-exist-list'); const container = document.getElementById('playlist-exist-list');
if (tag.tag === null) return;
const tagtext = bytesToHex(tag.tag); const tagtext = bytesToHex(tag.tag);
document.getElementById('playlist-exist-list') document.getElementById('playlist-exist-list')
.querySelectorAll("li") .querySelectorAll("li")
@@ -797,17 +798,16 @@
const rootnode = document.createElement('ul'); const rootnode = document.createElement('ul');
tree.appendChild(rootnode); 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) { for (const file of files) {
const path = file.split('/').slice(1); const path = file.name.split('/').slice(1);
let treeIterator = rootnode; let treeIterator = rootnode;
let curPath = '' let curPath = ''
for (const elem of path) { for (const elem of path) {
curPath += '/' + elem; curPath += '/' + elem;
let node = findChildByName(treeIterator, elem); let node = findChildByName(treeIterator, elem);
if (node === null) { if (node === null) {
const isFile = path.slice(-1)[0] === elem; node = addTreeNode(treeIterator, elem, file.type, curPath);
node = addTreeNode(treeIterator, elem, isFile ? 'file':'directory', curPath);
} }
treeIterator = node; treeIterator = node;
} }

View File

@@ -162,6 +162,15 @@ async def audiofiles_get(request):
def directory_iterator(): def directory_iterator():
yield '[' yield '['
first = True first = True
def make_json_str(obj):
nonlocal first
jsonpath = json.dumps(obj)
if not first:
jsonpath = ',' + jsonpath
first = False
return jsonpath
dirstack = [fsroot] dirstack = [fsroot]
while dirstack: while dirstack:
current_dir = dirstack.pop() current_dir = dirstack.pop()
@@ -170,15 +179,11 @@ async def audiofiles_get(request):
type_ = entry[1] type_ = entry[1]
current_path = current_dir + b'/' + name current_path = current_dir + b'/' + name
if type_ == 0x4000: if type_ == 0x4000:
yield make_json_str({'name': current_path[len(fsroot):], 'type': 'directory'})
dirstack.append(current_path) dirstack.append(current_path)
elif type_ == 0x8000: elif type_ == 0x8000:
if name.lower().endswith('.mp3'): if name.lower().endswith('.mp3'):
jsonpath = json.dumps(current_path[len(fsroot):]) yield make_json_str({'name': current_path[len(fsroot):], 'type': 'file'})
if not first:
yield ','+jsonpath
else:
yield jsonpath
first = False
yield ']' yield ']'
return directory_iterator(), {'Content-Type': 'application/json; charset=UTF-8'} return directory_iterator(), {'Content-Type': 'application/json; charset=UTF-8'}