This commit is contained in:
Lartza
2020-03-10 22:22:20 +02:00
parent 44c7adae1f
commit b2ced96ba4
15 changed files with 166 additions and 121 deletions

View File

@ -57,6 +57,7 @@ web = Flask(__name__)
log = logging.getLogger("bot")
user = 'Remote Control'
def init_proxy():
global web
if var.is_proxified:
@ -64,19 +65,21 @@ def init_proxy():
# https://stackoverflow.com/questions/29725217/password-protect-one-webpage-in-flask-app
def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
return username == var.config.get("webinterface", "user") and password == var.config.get("webinterface", "password")
def authenticate():
"""Sends a 401 response that enables basic auth"""
global log
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
return Response('Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
@ -90,6 +93,7 @@ def requires_auth(f):
return f(*args, **kwargs)
return decorated
def tag_color(tag):
num = hash(tag) % 8
if num == 0:
@ -109,14 +113,15 @@ def tag_color(tag):
elif num == 7:
return "dark"
def build_tags_color_lookup():
color_lookup = {}
for tag in var.music_db.query_all_tags():
color_lookup[tag] = tag_color(tag)
return color_lookup
def build_path_tags_lookup():
path_tags_lookup = {}
ids = list(var.cache.file_id_lookup.values())
@ -128,11 +133,13 @@ def build_path_tags_lookup():
return path_tags_lookup
def recur_dir(dirobj):
for name, dir in dirobj.get_subdirs().items():
print(dirobj.fullpath + "/" + name)
recur_dir(dir)
@web.route("/", methods=['GET'])
@requires_auth
def index():
@ -153,21 +160,22 @@ def index():
paused=var.bot.is_pause,
)
@web.route("/playlist", methods=['GET'])
@requires_auth
def playlist():
if len(var.playlist) == 0:
return jsonify({'items': [render_template('playlist.html',
m=False,
index=-1
)]
m=False,
index=-1
)]
})
tags_color_lookup = build_tags_color_lookup()
items = []
for index, item_wrapper in enumerate(var.playlist):
items.append(render_template('playlist.html',
items.append(render_template('playlist.html',
index=index,
tags_color_lookup=tags_color_lookup,
m=item_wrapper.item(),
@ -175,7 +183,8 @@ def playlist():
)
)
return jsonify({ 'items': items })
return jsonify({'items': items})
def status():
if len(var.playlist) > 0:
@ -197,7 +206,7 @@ def post():
if request.method == 'POST':
if request.form:
log.debug("web: Post request from %s: %s" % ( request.remote_addr, str(request.form)))
log.debug("web: Post request from %s: %s" % (request.remote_addr, str(request.form)))
if 'add_file_bottom' in request.form and ".." not in request.form['add_file_bottom']:
path = var.music_folder + request.form['add_file_bottom']
if os.path.isfile(path):
@ -231,15 +240,13 @@ def post():
music_wrappers = list(map(
lambda file:
get_cached_wrapper_by_id(var.bot, var.cache.file_id_lookup[folder + file], user),
files))
get_cached_wrapper_by_id(var.bot, var.cache.file_id_lookup[folder + file], user), files))
var.playlist.extend(music_wrappers)
for music_wrapper in music_wrappers:
log.info('web: add to playlist: ' + music_wrapper.format_debug_string())
elif 'add_url' in request.form:
music_wrapper = get_cached_wrapper_from_scrap(var.bot, type='url', url=request.form['add_url'], user=user)
var.playlist.append(music_wrapper)
@ -279,7 +286,6 @@ def post():
else:
var.playlist.remove(index)
elif 'play_music' in request.form:
music_wrapper = var.playlist[int(request.form['play_music'])]
log.info("web: jump to: " + music_wrapper.format_debug_string())
@ -358,6 +364,7 @@ def post():
return status()
@web.route('/upload', methods=["POST"])
def upload():
global log
@ -366,7 +373,7 @@ def upload():
if not files:
return redirect("./", code=406)
#filename = secure_filename(file.filename).strip()
# filename = secure_filename(file.filename).strip()
for file in files:
filename = file.filename
if filename == '':
@ -385,7 +392,7 @@ def upload():
if "audio" in file.mimetype:
storagepath = os.path.abspath(os.path.join(var.music_folder, targetdir))
print('storagepath:',storagepath)
print('storagepath:', storagepath)
if not storagepath.startswith(os.path.abspath(var.music_folder)):
return redirect("./", code=406)