3 from functools import wraps
4 from flask import Flask, render_template, request, redirect, send_file, Response, jsonify, abort
5 import variables as var
10 from werkzeug.utils import secure_filename
13 from media.cache import get_cached_wrapper_from_scrap, get_cached_wrapper_by_id, get_cached_wrappers_by_tags
18 class ReverseProxied(object):
19 """Wrap the application in this middleware and configure the
20 front-end server to add these headers, to let you quietly bind
21 this to a URL other than / and to an HTTP scheme that is
22 different than what is used locally.
26 proxy_pass http://192.168.0.1:5001;
27 proxy_set_header Host $host;
28 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
29 proxy_set_header X-Scheme $scheme;
30 proxy_set_header X-Script-Name /myprefix;
33 :param app: the WSGI application
36 def __init__(self, app):
39 def __call__(self, environ, start_response):
40 script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
42 environ['SCRIPT_NAME'] = script_name
43 path_info = environ['PATH_INFO']
44 if path_info.startswith(script_name):
45 environ['PATH_INFO'] = path_info[len(script_name):]
47 scheme = environ.get('HTTP_X_SCHEME', '')
49 environ['wsgi.url_scheme'] = scheme
50 real_ip = environ.get('HTTP_X_REAL_IP', '')
52 environ['REMOTE_ADDR'] = real_ip
53 return self.app(environ, start_response)
57 log = logging.getLogger("bot")
58 user = 'Remote Control'
64 web.wsgi_app = ReverseProxied(web.wsgi_app)
66 # https://stackoverflow.com/questions/29725217/password-protect-one-webpage-in-flask-app
69 def check_auth(username, password):
70 """This function is called to check if a username /
71 password combination is valid.
73 return username == var.config.get("webinterface", "user") and password == var.config.get("webinterface", "password")
77 """Sends a 401 response that enables basic auth"""
79 return Response('Could not verify your access level for that URL.\n'
80 'You have to login with proper credentials', 401,
81 {'WWW-Authenticate': 'Basic realm="Login Required"'})
86 def decorated(*args, **kwargs):
88 auth = request.authorization
89 if var.config.getboolean("webinterface", "require_auth") and (not auth or not check_auth(auth.username, auth.password)):
91 log.info("web: Failed login attempt, user: %s" % auth.username)
93 return f(*args, **kwargs)
117 def build_tags_color_lookup():
119 for tag in var.music_db.query_all_tags():
120 color_lookup[tag] = tag_color(tag)
125 def build_path_tags_lookup():
126 path_tags_lookup = {}
127 ids = list(var.cache.file_id_lookup.values())
129 id_tags_lookup = var.music_db.query_tags_by_ids(ids)
131 for path, id in var.cache.file_id_lookup.items():
132 path_tags_lookup[path] = id_tags_lookup[id]
134 return path_tags_lookup
137 def recur_dir(dirobj):
138 for name, dir in dirobj.get_subdirs().items():
139 print(dirobj.fullpath + "/" + name)
143 @web.route("/", methods=['GET'])
146 while var.cache.dir_lock.locked():
149 tags_color_lookup = build_tags_color_lookup()
150 path_tags_lookup = build_path_tags_lookup()
152 return render_template('index.html',
153 all_files=var.cache.files,
154 tags_lookup=path_tags_lookup,
155 tags_color_lookup=tags_color_lookup,
156 music_library=var.cache.dir,
158 playlist=var.playlist,
160 paused=var.bot.is_pause,
164 @web.route("/playlist", methods=['GET'])
167 if len(var.playlist) == 0:
168 return jsonify({'items': [render_template('playlist.html',
174 tags_color_lookup = build_tags_color_lookup()
177 for index, item_wrapper in enumerate(var.playlist):
178 items.append(render_template('playlist.html',
180 tags_color_lookup=tags_color_lookup,
181 m=item_wrapper.item(),
182 playlist=var.playlist
186 return jsonify({'items': items})
190 if len(var.playlist) > 0:
191 return jsonify({'ver': var.playlist.version,
193 'play': not var.bot.is_pause,
194 'playhead': var.bot.playhead,
195 'mode': var.playlist.mode})
197 return jsonify({'ver': var.playlist.version,
201 'mode': var.playlist.mode})
204 @web.route("/post", methods=['POST'])
209 if request.method == 'POST':
211 log.debug("web: Post request from %s: %s" % (request.remote_addr, str(request.form)))
212 if 'add_file_bottom' in request.form and ".." not in request.form['add_file_bottom']:
213 path = var.music_folder + request.form['add_file_bottom']
214 if os.path.isfile(path):
215 music_wrapper = get_cached_wrapper_by_id(var.bot, var.cache.file_id_lookup[request.form['add_file_bottom']], user)
217 var.playlist.append(music_wrapper)
218 log.info('web: add to playlist(bottom): ' + music_wrapper.format_debug_string())
220 elif 'add_file_next' in request.form and ".." not in request.form['add_file_next']:
221 path = var.music_folder + request.form['add_file_next']
222 if os.path.isfile(path):
223 music_wrapper = get_cached_wrapper_by_id(var.bot, var.cache.file_id_lookup[request.form['add_file_next']], user)
224 var.playlist.insert(var.playlist.current_index + 1, music_wrapper)
225 log.info('web: add to playlist(next): ' + music_wrapper.format_debug_string())
227 elif ('add_folder' in request.form and ".." not in request.form['add_folder']) or ('add_folder_recursively' in request.form and ".." not in request.form['add_folder_recursively']):
229 folder = request.form['add_folder']
231 folder = request.form['add_folder_recursively']
233 if not folder.endswith('/'):
236 if os.path.isdir(var.music_folder + folder):
238 if 'add_folder_recursively' in request.form:
239 files = dir.get_files_recursively(folder)
241 files = dir.get_files(folder)
243 music_wrappers = list(map(
245 get_cached_wrapper_by_id(var.bot, var.cache.file_id_lookup[folder + file], user), files))
247 var.playlist.extend(music_wrappers)
249 for music_wrapper in music_wrappers:
250 log.info('web: add to playlist: ' + music_wrapper.format_debug_string())
252 elif 'add_url' in request.form:
253 music_wrapper = get_cached_wrapper_from_scrap(var.bot, type='url', url=request.form['add_url'], user=user)
254 var.playlist.append(music_wrapper)
256 log.info("web: add to playlist: " + music_wrapper.format_debug_string())
257 if len(var.playlist) == 2:
258 # If I am the second item on the playlist. (I am the next one!)
259 var.bot.async_download_next()
261 elif 'add_radio' in request.form:
262 url = request.form['add_radio']
263 music_wrapper = get_cached_wrapper_from_scrap(var.bot, type='radio', url=url, user=user)
264 var.playlist.append(music_wrapper)
266 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
268 elif 'delete_music' in request.form:
269 music_wrapper = var.playlist[int(request.form['delete_music'])]
270 log.info("web: delete from playlist: " + music_wrapper.format_debug_string())
272 if len(var.playlist) >= int(request.form['delete_music']):
273 index = int(request.form['delete_music'])
275 if index == var.playlist.current_index:
276 var.playlist.remove(index)
278 if index < len(var.playlist):
279 if not var.bot.is_pause:
281 var.playlist.current_index -= 1
282 # then the bot will move to next item
284 else: # if item deleted is the last item of the queue
285 var.playlist.current_index -= 1
286 if not var.bot.is_pause:
289 var.playlist.remove(index)
291 elif 'play_music' in request.form:
292 music_wrapper = var.playlist[int(request.form['play_music'])]
293 log.info("web: jump to: " + music_wrapper.format_debug_string())
295 if len(var.playlist) >= int(request.form['play_music']):
296 var.playlist.point_to(int(request.form['play_music']) - 1)
297 if not var.bot.is_pause:
300 var.bot.is_pause = False
303 elif 'delete_music_file' in request.form and ".." not in request.form['delete_music_file']:
304 path = var.music_folder + request.form['delete_music_file']
305 if os.path.isfile(path):
306 log.info("web: delete file " + path)
309 elif 'delete_folder' in request.form and ".." not in request.form['delete_folder']:
310 path = var.music_folder + request.form['delete_folder']
311 if os.path.isdir(path):
312 log.info("web: delete folder " + path)
316 elif 'add_tag' in request.form:
317 music_wrappers = get_cached_wrappers_by_tags(var.bot, [request.form['add_tag']], user)
318 for music_wrapper in music_wrappers:
319 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
320 var.playlist.extend(music_wrappers)
322 elif 'action' in request.form:
323 action = request.form['action']
324 if action == "randomize":
325 if var.playlist.mode != "random":
326 var.playlist = media.playlist.get_playlist("random", var.playlist)
328 var.playlist.randomize()
330 var.db.set('playlist', 'playback_mode', "random")
331 log.info("web: playback mode changed to random.")
332 if action == "one-shot":
333 var.playlist = media.playlist.get_playlist("one-shot", var.playlist)
334 var.db.set('playlist', 'playback_mode', "one-shot")
335 log.info("web: playback mode changed to one-shot.")
336 if action == "repeat":
337 var.playlist = media.playlist.get_playlist("repeat", var.playlist)
338 var.db.set('playlist', 'playback_mode', "repeat")
339 log.info("web: playback mode changed to repeat.")
340 if action == "autoplay":
341 var.playlist = media.playlist.get_playlist("autoplay", var.playlist)
342 var.db.set('playlist', 'playback_mode', "autoplay")
343 log.info("web: playback mode changed to autoplay.")
344 if action == "rescan":
345 var.cache.build_dir_cache(var.bot)
346 log.info("web: Local file cache refreshed.")
347 elif action == "stop":
349 elif action == "pause":
351 elif action == "resume":
353 elif action == "clear":
355 elif action == "volume_up":
356 if var.bot.volume_set + 0.03 < 1.0:
357 var.bot.volume_set = var.bot.volume_set + 0.03
359 var.bot.volume_set = 1.0
360 var.db.set('bot', 'volume', str(var.bot.volume_set))
361 log.info("web: volume up to %d" % (var.bot.volume_set * 100))
362 elif action == "volume_down":
363 if var.bot.volume_set - 0.03 > 0:
364 var.bot.volume_set = var.bot.volume_set - 0.03
366 var.bot.volume_set = 0
367 var.db.set('bot', 'volume', str(var.bot.volume_set))
368 log.info("web: volume up to %d" % (var.bot.volume_set * 100))
373 @web.route('/upload', methods=["POST"])
377 files = request.files.getlist("file[]")
379 return redirect("./", code=406)
381 # filename = secure_filename(file.filename).strip()
383 filename = file.filename
385 return redirect("./", code=406)
387 targetdir = request.form['targetdir'].strip()
389 targetdir = 'uploads/'
390 elif '../' in targetdir:
391 return redirect("./", code=406)
393 log.info('web: Uploading file from %s:' % request.remote_addr)
394 log.info('web: - filename: ' + filename)
395 log.info('web: - targetdir: ' + targetdir)
396 log.info('web: - mimetype: ' + file.mimetype)
398 if "audio" in file.mimetype or os.path.splitext(filename)[-1] in ('.ogg', '.opus', '.mp3', '.flac', '.wav'):
399 storagepath = os.path.abspath(os.path.join(var.music_folder, targetdir))
400 print('storagepath:', storagepath)
401 if not storagepath.startswith(os.path.abspath(var.music_folder)):
402 return redirect("./", code=406)
405 os.makedirs(storagepath)
406 except OSError as ee:
407 if ee.errno != errno.EEXIST:
408 return redirect("./", code=500)
410 filepath = os.path.join(storagepath, filename)
411 log.info(' - filepath: ' + filepath)
412 if os.path.exists(filepath):
419 var.cache.build_dir_cache(var.bot)
420 log.info("web: Local file cache refreshed.")
422 return redirect("./", code=302)
425 @web.route('/download', methods=["GET"])
429 if 'file' in request.args:
430 requested_file = request.args['file']
431 log.info('web: Download of file %s requested from %s:' % (requested_file, request.remote_addr))
432 if '../' not in requested_file:
433 folder_path = var.music_folder
434 files = var.cache.files
436 if requested_file in files:
437 filepath = os.path.join(folder_path, requested_file)
439 return send_file(filepath, as_attachment=True)
440 except Exception as e:
443 elif 'directory' in request.args:
444 requested_dir = request.args['directory']
445 folder_path = var.music_folder
446 requested_dir_fullpath = os.path.abspath(os.path.join(folder_path, requested_dir)) + '/'
447 if requested_dir_fullpath.startswith(folder_path):
448 if os.path.samefile(requested_dir_fullpath, folder_path):
451 prefix = secure_filename(os.path.relpath(requested_dir_fullpath, folder_path))
452 zipfile = util.zipdir(requested_dir_fullpath, prefix)
454 return send_file(zipfile, as_attachment=True)
455 except Exception as e:
459 return redirect("./", code=400)
462 if __name__ == '__main__':
463 web.run(port=8181, host="127.0.0.1")