3 from functools import wraps
4 from flask import Flask, render_template, request, redirect, send_file, Response, jsonify, abort
5 import variables as var
7 from datetime import datetime
12 from werkzeug.utils import secure_filename
15 from media.playlist import PlaylistItemWrapper
16 from media.file import FileItem
17 from media.url_from_playlist import PlaylistURLItem, get_playlist_info
18 from media.url import URLItem
19 from media.radio import RadioItem
25 class ReverseProxied(object):
26 '''Wrap the application in this middleware and configure the
27 front-end server to add these headers, to let you quietly bind
28 this to a URL other than / and to an HTTP scheme that is
29 different than what is used locally.
33 proxy_pass http://192.168.0.1:5001;
34 proxy_set_header Host $host;
35 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
36 proxy_set_header X-Scheme $scheme;
37 proxy_set_header X-Script-Name /myprefix;
40 :param app: the WSGI application
43 def __init__(self, app):
46 def __call__(self, environ, start_response):
47 script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
49 environ['SCRIPT_NAME'] = script_name
50 path_info = environ['PATH_INFO']
51 if path_info.startswith(script_name):
52 environ['PATH_INFO'] = path_info[len(script_name):]
54 scheme = environ.get('HTTP_X_SCHEME', '')
56 environ['wsgi.url_scheme'] = scheme
57 real_ip = environ.get('HTTP_X_REAL_IP', '')
59 environ['REMOTE_ADDR'] = real_ip
60 return self.app(environ, start_response)
64 log = logging.getLogger("bot")
65 user = 'Remote Control'
70 web.wsgi_app = ReverseProxied(web.wsgi_app)
72 # https://stackoverflow.com/questions/29725217/password-protect-one-webpage-in-flask-app
74 def check_auth(username, password):
75 """This function is called to check if a username /
76 password combination is valid.
78 return username == var.config.get("webinterface", "user") and password == var.config.get("webinterface", "password")
81 """Sends a 401 response that enables basic auth"""
84 'Could not verify your access level for that URL.\n'
85 'You have to login with proper credentials', 401,
86 {'WWW-Authenticate': 'Basic realm="Login Required"'})
90 def decorated(*args, **kwargs):
92 auth = request.authorization
93 if var.config.getboolean("webinterface", "require_auth") and (not auth or not check_auth(auth.username, auth.password)):
95 log.info("web: Failed login attempt, user: %s" % auth.username)
97 return f(*args, **kwargs)
101 @web.route("/", methods=['GET'])
104 folder_path = var.music_folder
105 files = util.get_recursive_file_list_sorted(var.music_folder)
106 music_library = util.Dir(folder_path)
108 music_library.add_file(file)
111 return render_template('index.html',
113 music_library=music_library,
115 playlist=var.playlist,
117 paused=var.bot.is_pause
120 @web.route("/playlist", methods=['GET'])
123 if len(var.playlist) == 0:
124 return jsonify({'items': [render_template('playlist.html',
132 for index, item_wrapper in enumerate(var.playlist):
133 items.append(render_template('playlist.html',
136 playlist=var.playlist
140 return jsonify({ 'items': items })
143 if len(var.playlist) > 0:
144 return jsonify({'ver': var.playlist.version,
146 'play': not var.bot.is_pause,
147 'mode': var.playlist.mode})
149 return jsonify({'ver': var.playlist.version,
152 'mode': var.playlist.mode})
155 @web.route("/post", methods=['POST'])
160 folder_path = var.music_folder
161 if request.method == 'POST':
163 log.debug("web: Post request from %s: %s" % ( request.remote_addr, str(request.form)))
164 if 'add_file_bottom' in request.form and ".." not in request.form['add_file_bottom']:
165 path = var.music_folder + request.form['add_file_bottom']
166 if os.path.isfile(path):
167 music_wrapper = PlaylistItemWrapper(FileItem(var.bot, request.form['add_file_bottom']), user)
168 var.playlist.append(music_wrapper)
169 log.info('web: add to playlist(bottom): ' + music_wrapper.format_debug_string())
171 elif 'add_file_next' in request.form and ".." not in request.form['add_file_next']:
172 path = var.music_folder + request.form['add_file_next']
173 if os.path.isfile(path):
174 music_wrapper = PlaylistItemWrapper(FileItem(var.bot, request.form['add_file_next']), user)
175 var.playlist.insert(var.playlist.current_index + 1, music_wrapper)
176 log.info('web: add to playlist(next): ' + music_wrapper.format_debug_string())
178 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']):
180 folder = request.form['add_folder']
182 folder = request.form['add_folder_recursively']
184 if not folder.endswith('/'):
187 if os.path.isdir(var.music_folder + folder):
189 files = util.get_recursive_file_list_sorted(var.music_folder)
190 music_library = util.Dir(folder_path)
192 music_library.add_file(file)
194 if 'add_folder_recursively' in request.form:
195 files = music_library.get_files_recursively(folder)
197 files = music_library.get_files(folder)
199 music_wrappers = list(map(
200 lambda file: PlaylistItemWrapper(FileItem(var.bot, folder + file), user),
203 var.playlist.extend(music_wrappers)
205 for music_wrapper in music_wrappers:
206 log.info('web: add to playlist: ' + music_wrapper.format_debug_string())
209 elif 'add_url' in request.form:
210 music_wrapper = PlaylistItemWrapper(URLItem(var.bot, request.form['add_url']), user)
211 var.playlist.append(music_wrapper)
213 log.info("web: add to playlist: " + music_wrapper.format_debug_string())
214 if len(var.playlist) == 2:
215 # If I am the second item on the playlist. (I am the next one!)
216 var.bot.async_download_next()
218 elif 'add_radio' in request.form:
219 url = request.form['add_radio']
220 music_wrapper = PlaylistItemWrapper(RadioItem(var.bot, url), user)
221 var.playlist.append(music_wrapper)
223 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
225 elif 'delete_music' in request.form:
226 music_wrapper = var.playlist[int(request.form['delete_music'])]
227 log.info("web: delete from playlist: " + music_wrapper.format_debug_string())
229 if len(var.playlist) >= int(request.form['delete_music']):
230 index = int(request.form['delete_music'])
232 if index == var.playlist.current_index:
233 var.playlist.remove(index)
235 if index < len(var.playlist):
236 if not var.bot.is_pause:
238 var.playlist.current_index -= 1
239 # then the bot will move to next item
241 else: # if item deleted is the last item of the queue
242 var.playlist.current_index -= 1
243 if not var.bot.is_pause:
246 var.playlist.remove(index)
249 elif 'play_music' in request.form:
250 music_wrapper = var.playlist[int(request.form['play_music'])]
251 log.info("web: jump to: " + music_wrapper.format_debug_string())
253 if len(var.playlist) >= int(request.form['play_music']):
254 var.playlist.point_to(int(request.form['play_music']) - 1)
258 elif 'delete_music_file' in request.form and ".." not in request.form['delete_music_file']:
259 path = var.music_folder + request.form['delete_music_file']
260 if os.path.isfile(path):
261 log.info("web: delete file " + path)
264 elif 'delete_folder' in request.form and ".." not in request.form['delete_folder']:
265 path = var.music_folder + request.form['delete_folder']
266 if os.path.isdir(path):
267 log.info("web: delete folder " + path)
271 elif 'action' in request.form:
272 action = request.form['action']
273 if action == "randomize":
274 var.playlist = media.playlist.get_playlist("random", var.playlist)
276 var.db.set('playlist', 'playback_mode', "random")
277 log.info("web: playback mode changed to random.")
278 if action == "one-shot":
279 var.playlist = media.playlist.get_playlist("one-shot", var.playlist)
280 var.db.set('playlist', 'playback_mode', "one-shot")
281 log.info("web: playback mode changed to one-shot.")
282 if action == "repeat":
283 var.playlist = media.playlist.get_playlist("epeat", var.playlist)
284 var.db.set('playlist', 'playback_mode', "repeat")
285 log.info("web: playback mode changed to repeat.")
286 elif action == "stop":
288 elif action == "pause":
290 elif action == "resume":
292 elif action == "clear":
294 elif action == "volume_up":
295 if var.bot.volume_set + 0.03 < 1.0:
296 var.bot.volume_set = var.bot.volume_set + 0.03
298 var.bot.volume_set = 1.0
299 var.db.set('bot', 'volume', str(var.bot.volume_set))
300 log.info("web: volume up to %d" % (var.bot.volume_set * 100))
301 elif action == "volume_down":
302 if var.bot.volume_set - 0.03 > 0:
303 var.bot.volume_set = var.bot.volume_set - 0.03
305 var.bot.volume_set = 0
306 var.db.set('bot', 'volume', str(var.bot.volume_set))
307 log.info("web: volume up to %d" % (var.bot.volume_set * 100))
311 @web.route('/upload', methods=["POST"])
315 files = request.files.getlist("file[]")
317 return redirect("./", code=406)
319 #filename = secure_filename(file.filename).strip()
321 filename = file.filename
323 return redirect("./", code=406)
325 targetdir = request.form['targetdir'].strip()
327 targetdir = 'uploads/'
328 elif '../' in targetdir:
329 return redirect("./", code=406)
331 log.info('web: Uploading file from %s:' % request.remote_addr)
332 log.info('web: - filename: ' + filename)
333 log.info('web: - targetdir: ' + targetdir)
334 log.info('web: - mimetype: ' + file.mimetype)
336 if "audio" in file.mimetype:
337 storagepath = os.path.abspath(os.path.join(var.music_folder, targetdir))
338 print('storagepath:',storagepath)
339 if not storagepath.startswith(os.path.abspath(var.music_folder)):
340 return redirect("./", code=406)
343 os.makedirs(storagepath)
344 except OSError as ee:
345 if ee.errno != errno.EEXIST:
346 return redirect("./", code=500)
348 filepath = os.path.join(storagepath, filename)
349 log.info(' - filepath: ' + filepath)
350 if os.path.exists(filepath):
351 return redirect("./", code=406)
355 return redirect("./", code=409)
357 return redirect("./", code=302)
360 @web.route('/download', methods=["GET"])
364 if 'file' in request.args:
365 requested_file = request.args['file']
366 log.info('web: Download of file %s requested from %s:' % (requested_file, request.remote_addr))
367 if '../' not in requested_file:
368 folder_path = var.music_folder
369 files = util.get_recursive_file_list_sorted(var.music_folder)
371 if requested_file in files:
372 filepath = os.path.join(folder_path, requested_file)
374 return send_file(filepath, as_attachment=True)
375 except Exception as e:
378 elif 'directory' in request.args:
379 requested_dir = request.args['directory']
380 folder_path = var.music_folder
381 requested_dir_fullpath = os.path.abspath(os.path.join(folder_path, requested_dir)) + '/'
382 if requested_dir_fullpath.startswith(folder_path):
383 if os.path.samefile(requested_dir_fullpath, folder_path):
386 prefix = secure_filename(os.path.relpath(requested_dir_fullpath, folder_path))
387 zipfile = util.zipdir(requested_dir_fullpath, prefix)
389 return send_file(zipfile, as_attachment=True)
390 except Exception as e:
394 return redirect("./", code=400)
397 if __name__ == '__main__':
398 web.run(port=8181, host="127.0.0.1")