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 var.playlist.length() == 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 (var.playlist.length() > 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 print('folder:', folder)
189 if os.path.isdir(var.music_folder + folder):
191 files = util.get_recursive_file_list_sorted(var.music_folder)
192 music_library = util.Dir(folder_path)
194 music_library.add_file(file)
196 if 'add_folder_recursively' in request.form:
197 files = music_library.get_files_recursively(folder)
199 files = music_library.get_files(folder)
201 music_wrappers = list(map(
202 lambda file: PlaylistItemWrapper(FileItem(var.bot, file), user),
205 var.playlist.extend(files)
207 for music_wrapper in music_wrappers:
208 log.info('web: add to playlist: ' + music_wrapper.format_debug_string())
211 elif 'add_url' in request.form:
212 music_wrapper = PlaylistItemWrapper(URLItem(var.bot, request.form['add_url']), user)
213 var.playlist.append(music_wrapper)
215 log.info("web: add to playlist: " + music_wrapper.format_debug_string())
216 if var.playlist.length() == 2:
217 # If I am the second item on the playlist. (I am the next one!)
218 var.bot.async_download_next()
220 elif 'add_radio' in request.form:
221 url = request.form['add_radio']
222 music_wrapper = PlaylistItemWrapper(RadioItem(var.bot, url), user)
223 var.playlist.append(music_wrapper)
225 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
227 elif 'delete_music' in request.form:
228 music_wrapper = var.playlist[int(request.form['delete_music'])]
229 log.info("web: delete from playlist: " + music_wrapper.format_debug_string())
231 if var.playlist.length() >= int(request.form['delete_music']):
232 index = int(request.form['delete_music'])
234 if index == var.playlist.current_index:
235 var.playlist.remove(index)
237 if index < len(var.playlist):
238 if not var.bot.is_pause:
239 var.bot.interrupt_playing()
240 var.playlist.current_index -= 1
241 # then the bot will move to next item
243 else: # if item deleted is the last item of the queue
244 var.playlist.current_index -= 1
245 if not var.bot.is_pause:
246 var.bot.interrupt_playing()
248 var.playlist.remove(index)
251 elif 'play_music' in request.form:
252 music_wrapper = var.playlist[int(request.form['play_music'])]
253 log.info("web: jump to: " + music_wrapper.format_debug_string())
255 if len(var.playlist) >= int(request.form['play_music']):
256 var.playlist.point_to(int(request.form['play_music']) - 1)
257 var.bot.interrupt_playing()
259 elif 'delete_music_file' in request.form and ".." not in request.form['delete_music_file']:
260 path = var.music_folder + request.form['delete_music_file']
261 if os.path.isfile(path):
262 log.info("web: delete file " + path)
265 elif 'delete_folder' in request.form and ".." not in request.form['delete_folder']:
266 path = var.music_folder + request.form['delete_folder']
267 if os.path.isdir(path):
268 log.info("web: delete folder " + path)
272 elif 'action' in request.form:
273 action = request.form['action']
274 if action == "randomize":
275 var.bot.interrupt_playing()
276 var.playlist.set_mode("random")
277 var.db.set('playlist', 'playback_mode', "random")
278 log.info("web: playback mode changed to random.")
279 if action == "one-shot":
280 var.playlist.set_mode("one-shot")
281 var.db.set('playlist', 'playback_mode', "one-shot")
282 log.info("web: playback mode changed to one-shot.")
283 if action == "repeat":
284 var.playlist.set_mode("repeat")
285 var.db.set('playlist', 'playback_mode', "repeat")
286 log.info("web: playback mode changed to repeat.")
287 elif action == "stop":
289 elif action == "pause":
291 elif action == "resume":
293 elif action == "clear":
295 elif action == "volume_up":
296 if var.bot.volume_set + 0.03 < 1.0:
297 var.bot.volume_set = var.bot.volume_set + 0.03
299 var.bot.volume_set = 1.0
300 var.db.set('bot', 'volume', str(var.bot.volume_set))
301 log.info("web: volume up to %d" % (var.bot.volume_set * 100))
302 elif action == "volume_down":
303 if var.bot.volume_set - 0.03 > 0:
304 var.bot.volume_set = var.bot.volume_set - 0.03
306 var.bot.volume_set = 0
307 var.db.set('bot', 'volume', str(var.bot.volume_set))
308 log.info("web: volume up to %d" % (var.bot.volume_set * 100))
312 @web.route('/upload', methods=["POST"])
316 files = request.files.getlist("file[]")
318 return redirect("./", code=406)
320 #filename = secure_filename(file.filename).strip()
322 filename = file.filename
324 return redirect("./", code=406)
326 targetdir = request.form['targetdir'].strip()
328 targetdir = 'uploads/'
329 elif '../' in targetdir:
330 return redirect("./", code=406)
332 log.info('web: Uploading file from %s:' % request.remote_addr)
333 log.info('web: - filename: ' + filename)
334 log.info('web: - targetdir: ' + targetdir)
335 log.info('web: - mimetype: ' + file.mimetype)
337 if "audio" in file.mimetype:
338 storagepath = os.path.abspath(os.path.join(var.music_folder, targetdir))
339 print('storagepath:',storagepath)
340 if not storagepath.startswith(os.path.abspath(var.music_folder)):
341 return redirect("./", code=406)
344 os.makedirs(storagepath)
345 except OSError as ee:
346 if ee.errno != errno.EEXIST:
347 return redirect("./", code=500)
349 filepath = os.path.join(storagepath, filename)
350 log.info(' - filepath: ' + filepath)
351 if os.path.exists(filepath):
352 return redirect("./", code=406)
356 return redirect("./", code=409)
358 return redirect("./", code=302)
361 @web.route('/download', methods=["GET"])
365 if 'file' in request.args:
366 requested_file = request.args['file']
367 log.info('web: Download of file %s requested from %s:' % (requested_file, request.remote_addr))
368 if '../' not in requested_file:
369 folder_path = var.music_folder
370 files = util.get_recursive_file_list_sorted(var.music_folder)
372 if requested_file in files:
373 filepath = os.path.join(folder_path, requested_file)
375 return send_file(filepath, as_attachment=True)
376 except Exception as e:
379 elif 'directory' in request.args:
380 requested_dir = request.args['directory']
381 folder_path = var.music_folder
382 requested_dir_fullpath = os.path.abspath(os.path.join(folder_path, requested_dir)) + '/'
383 if requested_dir_fullpath.startswith(folder_path):
384 if os.path.samefile(requested_dir_fullpath, folder_path):
387 prefix = secure_filename(os.path.relpath(requested_dir_fullpath, folder_path))
388 zipfile = util.zipdir(requested_dir_fullpath, prefix)
390 return send_file(zipfile, as_attachment=True)
391 except Exception as e:
395 return redirect("./", code=400)
398 if __name__ == '__main__':
399 web.run(port=8181, host="127.0.0.1")