Implement download functionality

- directories are served as a zip-file
This commit is contained in:
Fabian Würfl
2018-05-20 22:41:35 +02:00
parent 74ae51f65c
commit b0137b2db6
3 changed files with 65 additions and 3 deletions

View File

@ -1,6 +1,6 @@
#!/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 util
import os.path
@ -149,6 +149,36 @@ def upload():
else:
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__':
web.run(port=8181, host="127.0.0.1")