Allow uploading files to existing/new folders
This commit is contained in:
		
							
								
								
									
										47
									
								
								interface.py
									
									
									
									
									
								
							
							
						
						
									
										47
									
								
								interface.py
									
									
									
									
									
								
							@@ -7,6 +7,7 @@ import os.path
 | 
				
			|||||||
from os import listdir
 | 
					from os import listdir
 | 
				
			||||||
import random
 | 
					import random
 | 
				
			||||||
from werkzeug.utils import secure_filename
 | 
					from werkzeug.utils import secure_filename
 | 
				
			||||||
 | 
					import errno
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ReverseProxied(object):
 | 
					class ReverseProxied(object):
 | 
				
			||||||
    '''Wrap the application in this middleware and configure the
 | 
					    '''Wrap the application in this middleware and configure the
 | 
				
			||||||
@@ -107,23 +108,43 @@ def index():
 | 
				
			|||||||
                           music_library=music_library)
 | 
					                           music_library=music_library)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@web.route('/download', methods=["POST"])
 | 
					@web.route('/upload', methods=["POST"])
 | 
				
			||||||
def download():
 | 
					def upload():
 | 
				
			||||||
    print(request.form)
 | 
					    file = request.files['file']
 | 
				
			||||||
 | 
					 | 
				
			||||||
    file = request.files['music_file']
 | 
					 | 
				
			||||||
    if not file:
 | 
					    if not file:
 | 
				
			||||||
        return redirect("./", code=406)
 | 
					        return redirect("./", code=406)
 | 
				
			||||||
    elif file.filename == '':
 | 
					
 | 
				
			||||||
        return redirect("./", code=406)
 | 
					    filename = secure_filename(file.filename).strip()
 | 
				
			||||||
    elif '..' in request.form['directory']:
 | 
					    if filename == '':
 | 
				
			||||||
        return redirect("./", code=406)
 | 
					        return redirect("./", code=406)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if file.name == "music_file" and "audio" in file.headers.to_list()[1][1]:
 | 
					    targetdir = request.form['targetdir'].strip()
 | 
				
			||||||
        web.config['UPLOAD_FOLDER'] = var.music_folder + request.form['directory']
 | 
					    if targetdir == '':
 | 
				
			||||||
        filename = secure_filename(file.filename)
 | 
					        targetdir = 'uploads/'
 | 
				
			||||||
        print(filename)
 | 
					    elif '..' in targetdir:
 | 
				
			||||||
        file.save(os.path.join(web.config['UPLOAD_FOLDER'], filename))
 | 
					        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)
 | 
					        return redirect("./", code=302)
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        return redirect("./", code=409)
 | 
					        return redirect("./", code=409)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -34,19 +34,23 @@
 | 
				
			|||||||
<body>
 | 
					<body>
 | 
				
			||||||
<a href="."><h5>Refresh</h5></a>
 | 
					<a href="."><h5>Refresh</h5></a>
 | 
				
			||||||
<br>
 | 
					<br>
 | 
				
			||||||
{% if False %}
 | 
					
 | 
				
			||||||
<div id="download">
 | 
					<div id="upload">
 | 
				
			||||||
    <form action="./download" method="post" enctype="multipart/form-data">
 | 
					    <form action="./upload" method="post" enctype="multipart/form-data">
 | 
				
			||||||
        <input type="file" name="music_file" value="Browse Music file"/>
 | 
					        <input type="file" name="file" value="Browse Music file"/>
 | 
				
			||||||
        <select name="directory">
 | 
					        Upload into
 | 
				
			||||||
            {% for dir in all_files %}
 | 
					        <input list="targetdirs" id="targetdir" name="targetdir" />
 | 
				
			||||||
            <option value={{ dir }}>{{ dir }}</option>
 | 
					        <datalist id="targetdirs">
 | 
				
			||||||
 | 
					            {% for dir in music_library.get_subdirs_recursively() %}
 | 
				
			||||||
 | 
					            <option value="{{ dir }}">
 | 
				
			||||||
            {% endfor %}
 | 
					            {% endfor %}
 | 
				
			||||||
        </select>
 | 
					        </datalist>
 | 
				
			||||||
        <input type="submit" value="Upload"/>
 | 
					        <input type="submit" value="Upload"/>
 | 
				
			||||||
    </form>
 | 
					    </form>
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
{% endif %}
 | 
					
 | 
				
			||||||
 | 
					<br />
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<div id="playlist">
 | 
					<div id="playlist">
 | 
				
			||||||
    Current Playing :
 | 
					    Current Playing :
 | 
				
			||||||
    {% if current_music %}
 | 
					    {% if current_music %}
 | 
				
			||||||
@@ -54,7 +58,7 @@
 | 
				
			|||||||
    {% else %}
 | 
					    {% else %}
 | 
				
			||||||
    No music
 | 
					    No music
 | 
				
			||||||
    {% endif %}
 | 
					    {% endif %}
 | 
				
			||||||
    <br>
 | 
					    <br />
 | 
				
			||||||
    Playlist :
 | 
					    Playlist :
 | 
				
			||||||
    <form method="post"><input type="text" value="randomize" name="action" hidden><input type="submit" value="Randomize playlist"></form>
 | 
					    <form method="post"><input type="text" value="randomize" name="action" hidden><input type="submit" value="Randomize playlist"></form>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										16
									
								
								util.py
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								util.py
									
									
									
									
									
								
							@@ -57,6 +57,22 @@ class Dir(object):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return subdirs
 | 
					        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):
 | 
					    def get_files(self, path=None):
 | 
				
			||||||
        files = []
 | 
					        files = []
 | 
				
			||||||
        if path and path != '':
 | 
					        if path and path != '':
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user