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.playlist import get_item_wrapper, get_item_wrapper_by_id
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'
63 web.wsgi_app = ReverseProxied(web.wsgi_app)
65 # https://stackoverflow.com/questions/29725217/password-protect-one-webpage-in-flask-app
67 def check_auth(username, password):
68 """This function is called to check if a username /
69 password combination is valid.
71 return username == var.config.get("webinterface", "user") and password == var.config.get("webinterface", "password")
74 """Sends a 401 response that enables basic auth"""
77 'Could not verify your access level for that URL.\n'
78 'You have to login with proper credentials', 401,
79 {'WWW-Authenticate': 'Basic realm="Login Required"'})
83 def decorated(*args, **kwargs):
85 auth = request.authorization
86 if var.config.getboolean("webinterface", "require_auth") and (not auth or not check_auth(auth.username, auth.password)):
88 log.info("web: Failed login attempt, user: %s" % auth.username)
90 return f(*args, **kwargs)
94 @web.route("/", methods=['GET'])
97 return render_template('index.html',
98 all_files=var.library.files,
99 music_library=var.library.dir,
101 playlist=var.playlist,
103 paused=var.bot.is_pause
106 @web.route("/playlist", methods=['GET'])
109 if len(var.playlist) == 0:
110 return jsonify({'items': [render_template('playlist.html',
118 for index, item_wrapper in enumerate(var.playlist):
119 items.append(render_template('playlist.html',
121 m=item_wrapper.item(),
122 playlist=var.playlist
126 return jsonify({ 'items': items })
129 if len(var.playlist) > 0:
130 return jsonify({'ver': var.playlist.version,
132 'play': not var.bot.is_pause,
133 'mode': var.playlist.mode})
135 return jsonify({'ver': var.playlist.version,
138 'mode': var.playlist.mode})
141 @web.route("/post", methods=['POST'])
146 if request.method == 'POST':
148 log.debug("web: Post request from %s: %s" % ( request.remote_addr, str(request.form)))
149 if 'add_file_bottom' in request.form and ".." not in request.form['add_file_bottom']:
150 path = var.music_folder + request.form['add_file_bottom']
151 if os.path.isfile(path):
152 music_wrapper = get_item_wrapper_by_id(var.bot, var.library.file_id_lookup[request.form['add_file_bottom']], user)
154 var.playlist.append(music_wrapper)
155 log.info('web: add to playlist(bottom): ' + music_wrapper.format_debug_string())
157 elif 'add_file_next' in request.form and ".." not in request.form['add_file_next']:
158 path = var.music_folder + request.form['add_file_next']
159 if os.path.isfile(path):
160 music_wrapper = get_item_wrapper_by_id(var.bot, var.library.file_id_lookup[request.form['add_file_next']], user)
161 var.playlist.insert(var.playlist.current_index + 1, music_wrapper)
162 log.info('web: add to playlist(next): ' + music_wrapper.format_debug_string())
164 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']):
166 folder = request.form['add_folder']
168 folder = request.form['add_folder_recursively']
170 if not folder.endswith('/'):
173 if os.path.isdir(var.music_folder + folder):
174 dir = var.library.dir
175 if 'add_folder_recursively' in request.form:
176 files = dir.get_files_recursively(folder)
178 files = dir.get_files(folder)
180 music_wrappers = list(map(
182 get_item_wrapper_by_id(var.bot, var.library.file_id_lookup[folder + file], user),
185 var.playlist.extend(music_wrappers)
187 for music_wrapper in music_wrappers:
188 log.info('web: add to playlist: ' + music_wrapper.format_debug_string())
191 elif 'add_url' in request.form:
192 music_wrapper = get_item_wrapper(var.bot, type='url', url=request.form['url'])
193 var.playlist.append(music_wrapper)
195 log.info("web: add to playlist: " + music_wrapper.format_debug_string())
196 if len(var.playlist) == 2:
197 # If I am the second item on the playlist. (I am the next one!)
198 var.bot.async_download_next()
200 elif 'add_radio' in request.form:
201 url = request.form['add_radio']
202 music_wrapper = get_item_wrapper(var.bot, type='radio', url=url)
203 var.playlist.append(music_wrapper)
205 log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
207 elif 'delete_music' in request.form:
208 music_wrapper = var.playlist[int(request.form['delete_music'])]
209 log.info("web: delete from playlist: " + music_wrapper.format_debug_string())
211 if len(var.playlist) >= int(request.form['delete_music']):
212 index = int(request.form['delete_music'])
214 if index == var.playlist.current_index:
215 var.playlist.remove(index)
217 if index < len(var.playlist):
218 if not var.bot.is_pause:
220 var.playlist.current_index -= 1
221 # then the bot will move to next item
223 else: # if item deleted is the last item of the queue
224 var.playlist.current_index -= 1
225 if not var.bot.is_pause:
228 var.playlist.remove(index)
231 elif 'play_music' in request.form:
232 music_wrapper = var.playlist[int(request.form['play_music'])]
233 log.info("web: jump to: " + music_wrapper.format_debug_string())
235 if len(var.playlist) >= int(request.form['play_music']):
236 var.playlist.point_to(int(request.form['play_music']) - 1)
240 elif 'delete_music_file' in request.form and ".." not in request.form['delete_music_file']:
241 path = var.music_folder + request.form['delete_music_file']
242 if os.path.isfile(path):
243 log.info("web: delete file " + path)
246 elif 'delete_folder' in request.form and ".." not in request.form['delete_folder']:
247 path = var.music_folder + request.form['delete_folder']
248 if os.path.isdir(path):
249 log.info("web: delete folder " + path)
253 elif 'action' in request.form:
254 action = request.form['action']
255 if action == "randomize":
256 if var.playlist.mode != "random":
257 var.playlist = media.playlist.get_playlist("random", var.playlist)
259 var.playlist.randomize()
261 var.db.set('playlist', 'playback_mode', "random")
262 log.info("web: playback mode changed to random.")
263 if action == "one-shot":
264 var.playlist = media.playlist.get_playlist("one-shot", var.playlist)
265 var.db.set('playlist', 'playback_mode', "one-shot")
266 log.info("web: playback mode changed to one-shot.")
267 if action == "repeat":
268 var.playlist = media.playlist.get_playlist("repeat", var.playlist)
269 var.db.set('playlist', 'playback_mode', "repeat")
270 log.info("web: playback mode changed to repeat.")
271 if action == "autoplay":
272 var.playlist = media.playlist.get_playlist("autoplay", var.playlist)
273 var.db.set('playlist', 'playback_mode', "autoplay")
274 log.info("web: playback mode changed to autoplay.")
275 elif action == "stop":
277 elif action == "pause":
279 elif action == "resume":
281 elif action == "clear":
283 elif action == "volume_up":
284 if var.bot.volume_set + 0.03 < 1.0:
285 var.bot.volume_set = var.bot.volume_set + 0.03
287 var.bot.volume_set = 1.0
288 var.db.set('bot', 'volume', str(var.bot.volume_set))
289 log.info("web: volume up to %d" % (var.bot.volume_set * 100))
290 elif action == "volume_down":
291 if var.bot.volume_set - 0.03 > 0:
292 var.bot.volume_set = var.bot.volume_set - 0.03
294 var.bot.volume_set = 0
295 var.db.set('bot', 'volume', str(var.bot.volume_set))
296 log.info("web: volume up to %d" % (var.bot.volume_set * 100))
300 @web.route('/upload', methods=["POST"])
304 files = request.files.getlist("file[]")
306 return redirect("./", code=406)
308 #filename = secure_filename(file.filename).strip()
310 filename = file.filename
312 return redirect("./", code=406)
314 targetdir = request.form['targetdir'].strip()
316 targetdir = 'uploads/'
317 elif '../' in targetdir:
318 return redirect("./", code=406)
320 log.info('web: Uploading file from %s:' % request.remote_addr)
321 log.info('web: - filename: ' + filename)
322 log.info('web: - targetdir: ' + targetdir)
323 log.info('web: - mimetype: ' + file.mimetype)
325 if "audio" in file.mimetype:
326 storagepath = os.path.abspath(os.path.join(var.music_folder, targetdir))
327 print('storagepath:',storagepath)
328 if not storagepath.startswith(os.path.abspath(var.music_folder)):
329 return redirect("./", code=406)
332 os.makedirs(storagepath)
333 except OSError as ee:
334 if ee.errno != errno.EEXIST:
335 return redirect("./", code=500)
337 filepath = os.path.join(storagepath, filename)
338 log.info(' - filepath: ' + filepath)
339 if os.path.exists(filepath):
340 return redirect("./", code=406)
344 return redirect("./", code=409)
346 return redirect("./", code=302)
349 @web.route('/download', methods=["GET"])
353 if 'file' in request.args:
354 requested_file = request.args['file']
355 log.info('web: Download of file %s requested from %s:' % (requested_file, request.remote_addr))
356 if '../' not in requested_file:
357 folder_path = var.music_folder
358 files = var.library.files
360 if requested_file in files:
361 filepath = os.path.join(folder_path, requested_file)
363 return send_file(filepath, as_attachment=True)
364 except Exception as e:
367 elif 'directory' in request.args:
368 requested_dir = request.args['directory']
369 folder_path = var.music_folder
370 requested_dir_fullpath = os.path.abspath(os.path.join(folder_path, requested_dir)) + '/'
371 if requested_dir_fullpath.startswith(folder_path):
372 if os.path.samefile(requested_dir_fullpath, folder_path):
375 prefix = secure_filename(os.path.relpath(requested_dir_fullpath, folder_path))
376 zipfile = util.zipdir(requested_dir_fullpath, prefix)
378 return send_file(zipfile, as_attachment=True)
379 except Exception as e:
383 return redirect("./", code=400)
386 if __name__ == '__main__':
387 web.run(port=8181, host="127.0.0.1")