Compare commits
2 Commits
e2ca9e5139
...
059b705a38
| Author | SHA1 | Date | |
|---|---|---|---|
| 059b705a38 | |||
| 3213ec8f66 |
@@ -297,6 +297,9 @@ class BTreeDB(IPlaylistDB):
|
||||
key = key.encode()
|
||||
return self.db.get(b'settings/' + key, self.DEFAULT_SETTINGS[key]).decode()
|
||||
|
||||
def deletePlaylistForTag(self, tag: bytes):
|
||||
self._deletePlaylist(tag)
|
||||
|
||||
def validate(self, dump=False):
|
||||
"""
|
||||
Validate the structure of the playlist database.
|
||||
|
||||
@@ -16,6 +16,7 @@ app = None
|
||||
nfc = None
|
||||
playlist_db = None
|
||||
|
||||
|
||||
def start_webserver(config_, app_):
|
||||
global server, config, app, nfc, playlist_db
|
||||
server = asyncio.create_task(webapp.start_server(port=80))
|
||||
@@ -96,6 +97,8 @@ async def playlist_get(request, tag):
|
||||
return 'invalid tag', 400
|
||||
|
||||
playlist = playlist_db.getPlaylistForTag(tag.encode())
|
||||
if playlist is None:
|
||||
return None, 404
|
||||
|
||||
return {
|
||||
'shuffle': playlist.__dict__.get('shuffle'),
|
||||
@@ -104,21 +107,57 @@ async def playlist_get(request, tag):
|
||||
for p in playlist.getPaths()],
|
||||
}
|
||||
|
||||
|
||||
@webapp.route('/api/v1/playlist/<tag>', methods=['PUT'])
|
||||
async def playlist_put(request, tag):
|
||||
if not is_hex(tag):
|
||||
return 'invalid tag', 400
|
||||
|
||||
playlist = request.json
|
||||
if 'persist' in playlist and \
|
||||
playlist['persist'] not in ['no', 'track', 'offset']:
|
||||
return "Invalid 'persist' setting", 400
|
||||
if 'shuffle' in playlist and \
|
||||
playlist['shuffle'] not in ['no', 'yes']:
|
||||
return "Invalid 'shuffle' setting", 400
|
||||
|
||||
playlist_db.createPlaylistForTag(tag.encode(),
|
||||
(fsroot + path.encode() for path in playlist.get('paths', [])),
|
||||
playlist.get('persist', 'track').encode(),
|
||||
playlist.get('shuffle', 'no').encode())
|
||||
return '', 204
|
||||
|
||||
|
||||
@webapp.route('/api/v1/playlist/<tag>', methods=['DELETE'])
|
||||
async def playlist_delete(request, tag):
|
||||
if not is_hex(tag):
|
||||
return 'invalid tag', 400
|
||||
playlist_db.deletePlaylistForTag(tag.encode())
|
||||
return '', 204
|
||||
|
||||
|
||||
@webapp.route('/api/v1/audiofiles', methods=['GET'])
|
||||
async def audiofiles_get(request):
|
||||
audiofiles = set()
|
||||
def directory_iterator():
|
||||
yield '['
|
||||
first = True
|
||||
dirstack = [fsroot]
|
||||
|
||||
while dirstack:
|
||||
current_dir = dirstack.pop()
|
||||
for entry in os.ilistdir(current_dir):
|
||||
name = entry[0]
|
||||
type_ = entry[1]
|
||||
current_path = current_dir + '/' + name
|
||||
current_path = current_dir + b'/' + name
|
||||
if type_ == 0x4000:
|
||||
dirstack.append(current_path)
|
||||
elif type_ == 0x8000:
|
||||
if name.lower().endswith('.mp3'):
|
||||
audiofiles.add(current_path[len(fsroot):])
|
||||
jsonpath = json.dumps(current_path[len(fsroot):])
|
||||
if not first:
|
||||
yield ','+jsonpath
|
||||
else:
|
||||
yield jsonpath
|
||||
first = False
|
||||
yield ']'
|
||||
|
||||
return sorted(audiofiles)
|
||||
return directory_iterator(), {'Content-Type': 'application/json; charset=UTF-8'}
|
||||
|
||||
Reference in New Issue
Block a user