]> git.0d.be Git - botaradio.git/blob - interface.py
also accept files according to their extension
[botaradio.git] / interface.py
1 #!/usr/bin/python3
2
3 from functools import wraps
4 from flask import Flask, render_template, request, redirect, send_file, Response, jsonify, abort
5 import variables as var
6 import util
7 import os
8 import os.path
9 import shutil
10 from werkzeug.utils import secure_filename
11 import errno
12 import media
13 from media.cache import get_cached_wrapper_from_scrap, get_cached_wrapper_by_id, get_cached_wrappers_by_tags
14 import logging
15 import time
16
17
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.
23
24     In nginx:
25     location /myprefix {
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;
31         }
32
33     :param app: the WSGI application
34     """
35
36     def __init__(self, app):
37         self.app = app
38
39     def __call__(self, environ, start_response):
40         script_name = environ.get('HTTP_X_SCRIPT_NAME', '')
41         if 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):]
46
47         scheme = environ.get('HTTP_X_SCHEME', '')
48         if scheme:
49             environ['wsgi.url_scheme'] = scheme
50         real_ip = environ.get('HTTP_X_REAL_IP', '')
51         if real_ip:
52             environ['REMOTE_ADDR'] = real_ip
53         return self.app(environ, start_response)
54
55
56 web = Flask(__name__)
57 log = logging.getLogger("bot")
58 user = 'Remote Control'
59
60
61 def init_proxy():
62     global web
63     if var.is_proxified:
64         web.wsgi_app = ReverseProxied(web.wsgi_app)
65
66 # https://stackoverflow.com/questions/29725217/password-protect-one-webpage-in-flask-app
67
68
69 def check_auth(username, password):
70     """This function is called to check if a username /
71     password combination is valid.
72     """
73     return username == var.config.get("webinterface", "user") and password == var.config.get("webinterface", "password")
74
75
76 def authenticate():
77     """Sends a 401 response that enables basic auth"""
78     global log
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"'})
82
83
84 def requires_auth(f):
85     @wraps(f)
86     def decorated(*args, **kwargs):
87         global log
88         auth = request.authorization
89         if var.config.getboolean("webinterface", "require_auth") and (not auth or not check_auth(auth.username, auth.password)):
90             if auth:
91                 log.info("web: Failed login attempt, user: %s" % auth.username)
92             return authenticate()
93         return f(*args, **kwargs)
94     return decorated
95
96
97 def tag_color(tag):
98     num = hash(tag) % 8
99     if num == 0:
100         return "primary"
101     elif num == 1:
102         return "secondary"
103     elif num == 2:
104         return "success"
105     elif num == 3:
106         return "danger"
107     elif num == 4:
108         return "warning"
109     elif num == 5:
110         return "info"
111     elif num == 6:
112         return "light"
113     elif num == 7:
114         return "dark"
115
116
117 def build_tags_color_lookup():
118     color_lookup = {}
119     for tag in var.music_db.query_all_tags():
120         color_lookup[tag] = tag_color(tag)
121
122     return color_lookup
123
124
125 def build_path_tags_lookup():
126     path_tags_lookup = {}
127     ids = list(var.cache.file_id_lookup.values())
128     if len(ids) > 0:
129         id_tags_lookup = var.music_db.query_tags_by_ids(ids)
130
131         for path, id in var.cache.file_id_lookup.items():
132             path_tags_lookup[path] = id_tags_lookup[id]
133
134     return path_tags_lookup
135
136
137 def recur_dir(dirobj):
138     for name, dir in dirobj.get_subdirs().items():
139         print(dirobj.fullpath + "/" + name)
140         recur_dir(dir)
141
142
143 @web.route("/", methods=['GET'])
144 @requires_auth
145 def index():
146     while var.cache.dir_lock.locked():
147         time.sleep(0.1)
148
149     tags_color_lookup = build_tags_color_lookup()
150     path_tags_lookup = build_path_tags_lookup()
151
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,
157                            os=os,
158                            playlist=var.playlist,
159                            user=var.user,
160                            paused=var.bot.is_pause,
161                            )
162
163
164 @web.route("/playlist", methods=['GET'])
165 @requires_auth
166 def playlist():
167     if len(var.playlist) == 0:
168         return jsonify({'items': [render_template('playlist.html',
169                                                   m=False,
170                                                   index=-1
171                                                   )]
172                         })
173
174     tags_color_lookup = build_tags_color_lookup()
175     items = []
176
177     for index, item_wrapper in enumerate(var.playlist):
178         items.append(render_template('playlist.html',
179                                      index=index,
180                                      tags_color_lookup=tags_color_lookup,
181                                      m=item_wrapper.item(),
182                                      playlist=var.playlist
183                                      )
184                      )
185
186     return jsonify({'items': items})
187
188
189 def status():
190     if len(var.playlist) > 0:
191         return jsonify({'ver': var.playlist.version,
192                         'empty': False,
193                         'play': not var.bot.is_pause,
194                         'playhead': var.bot.playhead,
195                         'mode': var.playlist.mode})
196     else:
197         return jsonify({'ver': var.playlist.version,
198                         'empty': True,
199                         'play': False,
200                         'playhead': -1,
201                         'mode': var.playlist.mode})
202
203
204 @web.route("/post", methods=['POST'])
205 @requires_auth
206 def post():
207     global log
208
209     if request.method == 'POST':
210         if request.form:
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)
216
217                 var.playlist.append(music_wrapper)
218                 log.info('web: add to playlist(bottom): ' + music_wrapper.format_debug_string())
219
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())
226
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']):
228             try:
229                 folder = request.form['add_folder']
230             except:
231                 folder = request.form['add_folder_recursively']
232
233             if not folder.endswith('/'):
234                 folder += '/'
235
236             if os.path.isdir(var.music_folder + folder):
237                 dir = var.cache.dir
238                 if 'add_folder_recursively' in request.form:
239                     files = dir.get_files_recursively(folder)
240                 else:
241                     files = dir.get_files(folder)
242
243                 music_wrappers = list(map(
244                     lambda file:
245                     get_cached_wrapper_by_id(var.bot, var.cache.file_id_lookup[folder + file], user), files))
246
247                 var.playlist.extend(music_wrappers)
248
249                 for music_wrapper in music_wrappers:
250                     log.info('web: add to playlist: ' + music_wrapper.format_debug_string())
251
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)
255
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()
260
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)
265
266             log.info("cmd: add to playlist: " + music_wrapper.format_debug_string())
267
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())
271
272             if len(var.playlist) >= int(request.form['delete_music']):
273                 index = int(request.form['delete_music'])
274
275                 if index == var.playlist.current_index:
276                     var.playlist.remove(index)
277
278                     if index < len(var.playlist):
279                         if not var.bot.is_pause:
280                             var.bot.interrupt()
281                             var.playlist.current_index -= 1
282                             # then the bot will move to next item
283
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:
287                             var.bot.interrupt()
288                 else:
289                     var.playlist.remove(index)
290
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())
294
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:
298                     var.bot.interrupt()
299                 else:
300                     var.bot.is_pause = False
301                 time.sleep(0.1)
302
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)
307                 os.remove(path)
308
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)
313                 shutil.rmtree(path)
314                 time.sleep(0.1)
315
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)
321
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)
327                 else:
328                     var.playlist.randomize()
329                 var.bot.interrupt()
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":
348                 var.bot.stop()
349             elif action == "pause":
350                 var.bot.pause()
351             elif action == "resume":
352                 var.bot.resume()
353             elif action == "clear":
354                 var.bot.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
358                 else:
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
365                 else:
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))
369
370     return status()
371
372
373 @web.route('/upload', methods=["POST"])
374 def upload():
375     global log
376
377     files = request.files.getlist("file[]")
378     if not files:
379         return redirect("./", code=406)
380
381     # filename = secure_filename(file.filename).strip()
382     for file in files:
383         filename = file.filename
384         if filename == '':
385             return redirect("./", code=406)
386
387         targetdir = request.form['targetdir'].strip()
388         if targetdir == '':
389             targetdir = 'uploads/'
390         elif '../' in targetdir:
391             return redirect("./", code=406)
392
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)
397
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)
403
404             try:
405                 os.makedirs(storagepath)
406             except OSError as ee:
407                 if ee.errno != errno.EEXIST:
408                     return redirect("./", code=500)
409
410             filepath = os.path.join(storagepath, filename)
411             log.info(' - filepath: ' + filepath)
412             if os.path.exists(filepath):
413                 continue
414
415             file.save(filepath)
416         else:
417             continue
418
419     var.cache.build_dir_cache(var.bot)
420     log.info("web: Local file cache refreshed.")
421
422     return redirect("./", code=302)
423
424
425 @web.route('/download', methods=["GET"])
426 def download():
427     global log
428
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
435
436             if requested_file in files:
437                 filepath = os.path.join(folder_path, requested_file)
438                 try:
439                     return send_file(filepath, as_attachment=True)
440                 except Exception as e:
441                     log.exception(e)
442                     abort(404)
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):
449                 prefix = 'all'
450             else:
451                 prefix = secure_filename(os.path.relpath(requested_dir_fullpath, folder_path))
452             zipfile = util.zipdir(requested_dir_fullpath, prefix)
453             try:
454                 return send_file(zipfile, as_attachment=True)
455             except Exception as e:
456                 log.exception(e)
457                 abort(404)
458
459     return redirect("./", code=400)
460
461
462 if __name__ == '__main__':
463     web.run(port=8181, host="127.0.0.1")