zip download, and proper config
This commit is contained in:
82
code/app.py
82
code/app.py
@@ -1,23 +1,26 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os,sys
|
||||
import os,sys,time
|
||||
import json
|
||||
from datetime import datetime
|
||||
from flask import Flask, render_template, jsonify, \
|
||||
redirect, url_for, request, g, session, send_from_directory
|
||||
redirect, url_for, request, g, session, send_file, send_from_directory
|
||||
from werkzeug.utils import secure_filename
|
||||
import hashlib
|
||||
import zipfile
|
||||
from revprox import ReverseProxied
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(__name__)
|
||||
# Read config from json !
|
||||
app.config['UPLOAD_FOLDER'] = 'data/'
|
||||
app.config['SHARES_FILE'] = 'data/shares.json'
|
||||
app.config['DATE_FORMAT'] = "%Y-%m-%d %H:%M"
|
||||
app.config['UID'] = 1000
|
||||
app.config['DEBUG'] = False
|
||||
config_values = json.load(open(os.getenv('FLEES_CONFIG'),'rt'))
|
||||
app.config['UPLOAD_FOLDER'] = config_values['data_folder']
|
||||
app.config['SHARES_FILE'] = config_values['shares_file']
|
||||
app.config['ZIP_FOLDER'] = config_values['zip_folder']
|
||||
app.config['DATE_FORMAT'] = config_values['date_format']
|
||||
app.config['UID'] = config_values['uid']
|
||||
app.config['DEBUG'] = config_values['debug']
|
||||
|
||||
app.secret_key = 'Cz2dw5NiRt3PSMFBSLTAJJi7U2CdW7iPQqEeOaU6'
|
||||
app.wsgi_app = ReverseProxied(app.wsgi_app)
|
||||
@@ -151,6 +154,8 @@ def download_direct(name,password,filename):
|
||||
@app.route('/download/<name>/<password>/<filename>', methods=['GET'])
|
||||
@app.route('/download/<name>/<filename>', methods=['GET'])
|
||||
def download_file(name,filename,password = None):
|
||||
if password != None:
|
||||
session[name] = password
|
||||
(ok,share) = get_share(name)
|
||||
if not ok:
|
||||
return share
|
||||
@@ -159,14 +164,33 @@ def download_file(name,filename,password = None):
|
||||
return 'no such file', 404
|
||||
return send_from_directory(directory=share['path'], filename=filename)
|
||||
|
||||
|
||||
@app.route('/zip/<name>/<password>', methods=['GET'])
|
||||
@app.route('/zip/<name>', methods=['GET'])
|
||||
def download_zip(name,password = None):
|
||||
if password != None:
|
||||
session[name] = password
|
||||
(ok,share) = get_share(name)
|
||||
if not ok:
|
||||
return share
|
||||
zip_clean()
|
||||
zip_path = zip_share(share)
|
||||
return send_file(
|
||||
zip_path,
|
||||
as_attachment = True,
|
||||
attachment_filename = name + ".zip"
|
||||
)
|
||||
|
||||
|
||||
def file_stat(filename):
|
||||
s = os.stat(filename)
|
||||
return {
|
||||
'size': file_size_human(s.st_size),
|
||||
'size': file_size_MB(s.st_size),
|
||||
'mtime': file_date_human(s.st_mtime),
|
||||
'name': os.path.basename(filename)
|
||||
}
|
||||
|
||||
|
||||
def file_size_human(num):
|
||||
for x in [' B','KB','MB','GB','TB']:
|
||||
if num < 1024.0:
|
||||
@@ -175,6 +199,10 @@ def file_size_human(num):
|
||||
return "%3.1f %s" % (num, x)
|
||||
num /= 1024.0
|
||||
|
||||
def file_size_MB(num):
|
||||
return "%0.2f"%(num/(1024*1024),)
|
||||
|
||||
|
||||
def file_date_human(num):
|
||||
return datetime.fromtimestamp(
|
||||
num
|
||||
@@ -244,6 +272,44 @@ def print_debug(s):
|
||||
sys.stderr.write(str(s)+"\n")
|
||||
sys.stderr.flush()
|
||||
|
||||
|
||||
def zip_share(share):
|
||||
|
||||
if not os.path.exists(app.config['ZIP_FOLDER']):
|
||||
os.makedirs(app.config['ZIP_FOLDER'])
|
||||
os.chown(app.config['ZIP_FOLDER'], app.config['UID'], -1)
|
||||
|
||||
zip_path = os.path.join(
|
||||
app.config['ZIP_FOLDER'],
|
||||
"%s-%d.zip"%(
|
||||
share['name'],
|
||||
time.time()
|
||||
)
|
||||
)
|
||||
zf = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED)
|
||||
for file in sorted(os.listdir(share['path'])):
|
||||
print_debug(os.path.join(share['path'],file))
|
||||
zf.write(
|
||||
os.path.join(share['path'],file),
|
||||
arcname = os.path.join(share['name'],file)
|
||||
)
|
||||
zf.close()
|
||||
os.chown(zip_path, app.config['UID'], -1)
|
||||
return zip_path
|
||||
|
||||
def zip_clean():
|
||||
""" delete zip files older than 1 hour """
|
||||
if not os.path.exists(app.config['ZIP_FOLDER']):
|
||||
return
|
||||
for file in os.listdir(app.config['ZIP_FOLDER']):
|
||||
if not file.endswith("zip"):
|
||||
continue
|
||||
mtime = os.stat(
|
||||
os.path.join(app.config['ZIP_FOLDER'],file)
|
||||
).st_mtime
|
||||
if mtime + 3600 < time.time():
|
||||
os.remove(os.path.join(app.config['ZIP_FOLDER'],file))
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user