generator
This commit is contained in:
47
code/app.py
47
code/app.py
@@ -12,8 +12,12 @@ 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.secret_key = 'Cz2dw5NiRt3PSMFBSLTAJJi7U2CdW7iPQqEeOaU6'
|
||||
app.wsgi_app = ReverseProxied(app.wsgi_app)
|
||||
|
||||
@@ -27,7 +31,8 @@ def index():
|
||||
public_shares = []
|
||||
for share in g.shares:
|
||||
public = get_or_none(share,'public')
|
||||
if public:
|
||||
expired = is_expired(share)
|
||||
if public and not expired:
|
||||
public_shares.append({
|
||||
'name': share['name'],
|
||||
'expire': get_or_none(share,'expire'),
|
||||
@@ -61,6 +66,7 @@ def upload():
|
||||
)
|
||||
)
|
||||
file.save(filename)
|
||||
os.chown(filename, app.config['UID'], -1)
|
||||
return redirect(url_for('list_view',name=name))
|
||||
|
||||
@app.route('/send/<name>', methods=['GET'])
|
||||
@@ -78,7 +84,14 @@ def list_view(name):
|
||||
files = []
|
||||
for file in sorted(os.listdir(share['path'])):
|
||||
files.append(file_stat(os.path.join(share['path'],file)))
|
||||
return render_template("list.html",name = share['name'], entries = files)
|
||||
return render_template(
|
||||
"list.html",
|
||||
name = share['name'],
|
||||
entries = files,
|
||||
public = get_or_none(share,'public'),
|
||||
upload = get_or_none(share,'upload'),
|
||||
expire = get_or_none(share,'expire')
|
||||
)
|
||||
#~ return jsonify({"share":share, "files": files})
|
||||
|
||||
@app.route('/download/<name>/<filename>', methods=['GET'])
|
||||
@@ -94,11 +107,27 @@ def download_file(name,filename):
|
||||
def file_stat(filename):
|
||||
s = os.stat(filename)
|
||||
return {
|
||||
'size': s.st_size,
|
||||
'mtime': s.st_mtime,
|
||||
'size': file_size_human(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:
|
||||
if x==' B':
|
||||
return "%d %s" % (num, x)
|
||||
return "%3.1f %s" % (num, x)
|
||||
num /= 1024.0
|
||||
|
||||
def file_date_human(num):
|
||||
return datetime.fromtimestamp(
|
||||
num
|
||||
).strftime(app.config['DATE_FORMAT'])
|
||||
|
||||
|
||||
|
||||
|
||||
def get_or_none(d,key):
|
||||
if key in d:
|
||||
return d[key]
|
||||
@@ -110,6 +139,8 @@ def get_share(name):
|
||||
if len(share) < 1:
|
||||
return (False,'No such share')
|
||||
share = share[0]
|
||||
if is_expired(share):
|
||||
return (False, 'Share has expired')
|
||||
authenticated = True
|
||||
if 'pass_hash' in share:
|
||||
authenticated = False
|
||||
@@ -132,8 +163,16 @@ def get_share(name):
|
||||
})
|
||||
if not os.path.exists(share['path']):
|
||||
os.makedirs(share['path'])
|
||||
os.chown(share['path'], app.config['UID'], -1)
|
||||
return (True,share)
|
||||
|
||||
def is_expired(share):
|
||||
expires = get_or_none(share, 'expire')
|
||||
if expires:
|
||||
if datetime.now() > datetime.strptime(expires, app.config['DATE_FORMAT']):
|
||||
return True
|
||||
return False
|
||||
|
||||
def printerr(s):
|
||||
sys.stderr.write(str(s)+"\n")
|
||||
sys.stderr.flush()
|
||||
|
||||
Reference in New Issue
Block a user