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:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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'}
|
||||
|
||||
Reference in New Issue
Block a user