Implement download functionality
- directories are served as a zip-file
This commit is contained in:
parent
74ae51f65c
commit
b0137b2db6
32
interface.py
32
interface.py
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
from flask import Flask, render_template, request, redirect
|
from flask import Flask, render_template, request, redirect, send_file
|
||||||
import variables as var
|
import variables as var
|
||||||
import util
|
import util
|
||||||
import os.path
|
import os.path
|
||||||
@ -149,6 +149,36 @@ def upload():
|
|||||||
else:
|
else:
|
||||||
return redirect("./", code=409)
|
return redirect("./", code=409)
|
||||||
|
|
||||||
|
@web.route('/download', methods=["GET"])
|
||||||
|
def download():
|
||||||
|
if 'file' in request.args:
|
||||||
|
requested_file = request.args['file']
|
||||||
|
if '../' not in requested_file:
|
||||||
|
folder_path = var.music_folder
|
||||||
|
files = util.get_recursive_filelist_sorted(var.music_folder)
|
||||||
|
|
||||||
|
if requested_file in files:
|
||||||
|
filepath = os.path.join(folder_path, requested_file)
|
||||||
|
try:
|
||||||
|
return send_file(filepath, as_attachment=True)
|
||||||
|
except Exception as e:
|
||||||
|
self.log.exception(e)
|
||||||
|
self.Error(400)
|
||||||
|
elif 'directory' in request.args:
|
||||||
|
requested_dir = request.args['directory']
|
||||||
|
folder_path = var.music_folder
|
||||||
|
requested_dir_fullpath = os.path.abspath(os.path.join(folder_path, requested_dir)) + '/'
|
||||||
|
if requested_dir_fullpath.startswith(folder_path):
|
||||||
|
prefix = secure_filename(os.path.relpath(requested_dir_fullpath, folder_path))
|
||||||
|
zipfile = util.zipdir(requested_dir_fullpath, requested_dir)
|
||||||
|
try:
|
||||||
|
return send_file(zipfile, as_attachment=True)
|
||||||
|
except Exception as e:
|
||||||
|
self.log.exception(e)
|
||||||
|
self.Error(400)
|
||||||
|
|
||||||
|
return redirect("./", code=400)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
web.run(port=8181, host="127.0.0.1")
|
web.run(port=8181, host="127.0.0.1")
|
||||||
|
@ -29,11 +29,11 @@
|
|||||||
{% for file in files %}
|
{% for file in files %}
|
||||||
<li class="file">
|
<li class="file">
|
||||||
<form method="post" class="file file_add">
|
<form method="post" class="file file_add">
|
||||||
<input type="text" value="{{ subdirpath }}/{{ file }}" name="add_file" hidden>
|
<input type="text" value="{{ subdirpath }}{{ file }}" name="add_file" hidden>
|
||||||
<input type="submit" value="Add">
|
<input type="submit" value="Add">
|
||||||
</form>
|
</form>
|
||||||
<form action="./download" method="get" class="file file_download">
|
<form action="./download" method="get" class="file file_download">
|
||||||
<input type="text" value="{{ subdirpath }}/{{ file }}" name="file" hidden>
|
<input type="text" value="{{ subdirpath }}{{ file }}" name="file" hidden>
|
||||||
<input type="submit" value="Download">
|
<input type="submit" value="Download">
|
||||||
{{ file }}
|
{{ file }}
|
||||||
</form>
|
</form>
|
||||||
|
32
util.py
32
util.py
@ -1,8 +1,10 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import configparser
|
import configparser
|
||||||
|
import hashlib
|
||||||
import os
|
import os
|
||||||
import variables as var
|
import variables as var
|
||||||
|
import zipfile
|
||||||
|
|
||||||
__CONFIG = configparser.ConfigParser(interpolation=None)
|
__CONFIG = configparser.ConfigParser(interpolation=None)
|
||||||
__CONFIG.read("configuration.ini", encoding='latin-1')
|
__CONFIG.read("configuration.ini", encoding='latin-1')
|
||||||
@ -23,6 +25,36 @@ def get_recursive_filelist_sorted(path):
|
|||||||
filelist.sort()
|
filelist.sort()
|
||||||
return filelist
|
return filelist
|
||||||
|
|
||||||
|
# - zips all files of the given zippath (must be a directory)
|
||||||
|
# - returns the absolute path of the created zip file
|
||||||
|
# - zip file will be in the applications tmp folder (according to configuration)
|
||||||
|
# - format of the filename itself = prefix_hash.zip
|
||||||
|
# - prefix can be controlled by the caller
|
||||||
|
# - hash is a sha1 of the string representation of the directories' contents (which are
|
||||||
|
# zipped)
|
||||||
|
def zipdir(zippath, zipname_prefix=None):
|
||||||
|
zipname = __CONFIG.get('bot', 'tmp_folder')
|
||||||
|
if zipname_prefix and '../' not in zipname_prefix:
|
||||||
|
zipname += zipname_prefix.strip().replace('/', '_') + '_'
|
||||||
|
|
||||||
|
files = get_recursive_filelist_sorted(zippath)
|
||||||
|
hash = hashlib.sha1((str(files).encode())).hexdigest()
|
||||||
|
zipname += hash + '.zip'
|
||||||
|
|
||||||
|
if os.path.exists(zipname):
|
||||||
|
return zipname
|
||||||
|
|
||||||
|
zipf = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)
|
||||||
|
|
||||||
|
for file in files:
|
||||||
|
filepath = os.path.dirname(file)
|
||||||
|
file_to_add = os.path.join(zippath, file)
|
||||||
|
add_file_as = os.path.relpath(os.path.join(zippath, file), os.path.join(zippath, '..'))
|
||||||
|
zipf.write(file_to_add, add_file_as)
|
||||||
|
|
||||||
|
zipf.close()
|
||||||
|
return zipname
|
||||||
|
|
||||||
class Dir(object):
|
class Dir(object):
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
Loading…
x
Reference in New Issue
Block a user