Commit 65ff9e3c authored by Lukas Böhm's avatar Lukas Böhm
Browse files

add some comments

parent ee5a13be
......@@ -6,20 +6,23 @@ from app import app, db
from flask import redirect, url_for, render_template, send_from_directory, current_app, abort
from app.models import Share, Attachment
from app.forms import UploadForm
from shutil import rmtree
def check_expired(share):
if share.download_limit <= 0 or datetime.now() > share.expires:
if share.download_limit < 0 or datetime.now() > share.expires:
# delete files
try:
os.removedirs(os.path.join(current_app.config['MEDIA_LOCATION'], share.id))
rmtree(os.path.join(current_app.config['MEDIA_LOCATION'], share.id))
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
# delete from database
for att in share.files:
db.session.delete(att)
db.session.delete(share)
db.session.commit()
return redirect('expired')
return True
return False
@app.route('/')
......@@ -32,24 +35,26 @@ def upload():
form = UploadForm()
if form.validate_on_submit():
# generate unique uuid
share_id = str(uuid.uuid4())
while Share.query.get(share_id) is not None:
share_id = str(uuid.uuid4())
# create share object
up = Share(id=share_id, name=form.name.data, expires=datetime.now() + timedelta(minutes=int(form.timer.data)),
download_limit=form.download_limit.data)
db.session.add(up)
# create share folder
try:
os.makedirs(os.path.join(current_app.config['MEDIA_LOCATION'], share_id))
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
# create attachments
for file in form.files.data:
fn = secure_filename(filename=file.filename)
file.save(os.path.join(current_app.config['MEDIA_LOCATION'], share_id, fn))
att = Attachment(filename=fn, share_id=share_id)
db.session.add(att)
# wrap it up
db.session.commit()
return redirect(url_for('download', share_id=share_id))
......@@ -61,23 +66,23 @@ def download(share_id):
share = Share.query.get_or_404(share_id)
share.download_limit -= 1
db.session.commit()
check_expired(share)
return render_template('Download.html', up=share)
if check_expired(share):
return redirect(url_for('expired'))
else:
return render_template('Download.html', up=share)
@app.route('/media/<string:share_id>/<string:filename>', methods=['GET'])
def media(share_id, filename):
share = Share.query.get_or_404(share_id)
check_expired(share)
if check_expired(share):
return redirect(url_for('expired'))
else:
return send_from_directory(os.path.join(current_app.config['MEDIA_LOCATION'], share_id), filename=filename,
as_attachment=True)
# if filename not in [a.filename for a in share.files]:
# return abort(404)
return send_from_directory(os.path.join(current_app.config['MEDIA_LOCATION'], share_id), filename=filename,
as_attachment=True)
@app.errorhandler(404)
@app.route('/expired')
def expired():
@app.errorhandler(404)
def expired(e=None):
return render_template('Expired.html')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment