diff --git a/interface.py b/interface.py index 85878b2..76b9624 100644 --- a/interface.py +++ b/interface.py @@ -7,6 +7,7 @@ import os.path from os import listdir import random from werkzeug.utils import secure_filename +import errno class ReverseProxied(object): '''Wrap the application in this middleware and configure the @@ -107,23 +108,43 @@ def index(): music_library=music_library) -@web.route('/download', methods=["POST"]) -def download(): - print(request.form) - - file = request.files['music_file'] +@web.route('/upload', methods=["POST"]) +def upload(): + file = request.files['file'] if not file: return redirect("./", code=406) - elif file.filename == '': - return redirect("./", code=406) - elif '..' in request.form['directory']: + + filename = secure_filename(file.filename).strip() + if filename == '': return redirect("./", code=406) - if file.name == "music_file" and "audio" in file.headers.to_list()[1][1]: - web.config['UPLOAD_FOLDER'] = var.music_folder + request.form['directory'] - filename = secure_filename(file.filename) - print(filename) - file.save(os.path.join(web.config['UPLOAD_FOLDER'], filename)) + targetdir = request.form['targetdir'].strip() + if targetdir == '': + targetdir = 'uploads/' + elif '..' in targetdir: + return redirect("./", code=406) + + print('Uploading file:') + print('filename:', filename) + print('targetdir:', targetdir) + print('mimetype:', file.mimetype) + + if "audio" in file.mimetype: + storagepath = os.path.abspath(os.path.join(var.music_folder, targetdir)) + if not storagepath.startswith(var.music_folder): + return redirect("./", code=406) + + try: + os.makedirs(storagepath) + except OSError as ee: + if ee.errno != errno.EEXIST: + return redirect("./", code=500) + + filepath = os.path.join(storagepath, filename) + if os.path.exists(filepath): + return redirect("./", code=406) + + file.save(filepath) return redirect("./", code=302) else: return redirect("./", code=409) diff --git a/templates/index.html b/templates/index.html index 09009b2..72858fd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -34,19 +34,23 @@
Refresh

-{% if False %} -
-
- - + Upload into + + + {% for dir in music_library.get_subdirs_recursively() %} +
-{% endif %} + +
+
Current Playing : {% if current_music %} @@ -54,7 +58,7 @@ {% else %} No music {% endif %} -
+
Playlist :
diff --git a/util.py b/util.py index 85a4331..8ed6f37 100644 --- a/util.py +++ b/util.py @@ -57,6 +57,22 @@ class Dir(object): return subdirs + def get_subdirs_recursively(self, path=None): + subdirs = [] + if path and path != '': + subdir = path.split('/')[0] + if subdir in self.subdirs: + searchpath = '/'.join(path.split('/')[1::]) + subdirs = self.subdirs[subdir].get_subdirs_recursively(searchpath) + else: + subdirs = list(self.subdirs.keys()) + + for key, val in self.subdirs.items(): + subdirs.extend(map(lambda subdir: key + '/' + subdir,val.get_subdirs_recursively())) + + subdirs.sort() + return subdirs + def get_files(self, path=None): files = [] if path and path != '':