Commit 825898cc authored by Lukas Böhm's avatar Lukas Böhm
Browse files

cleanup timer works now

parent 1b16e96d
......@@ -7,6 +7,50 @@ from flask import redirect, url_for, render_template, send_from_directory, curre
from app.models import Share, Attachment
from app.forms import UploadForm
from shutil import rmtree, make_archive
from apscheduler.schedulers.background import BackgroundScheduler
import atexit
def remove_expired():
print('CLEAING UP')
with app.app_context():
expired_shares = Share.query.filter(datetime.now() > Share.expires).all()
for sh in expired_shares:
# delete files
try:
rmtree(os.path.join(current_app.config['MEDIA_LOCATION'], sh.id))
os.remove(os.path.join(current_app.config['MEDIA_LOCATION'], sh.id + '.zip'))
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
# delete from database
for att in sh.files:
db.session.delete(att)
db.session.delete(sh)
db.session.commit()
sched = BackgroundScheduler(daemon=True)
sched.add_job(remove_expired, 'interval', seconds=10)
sched.start()
atexit.register(lambda: sched.shutdown())
def check_limit(share):
if share.download_limit < 0 or share.expires < datetime.now():
# delete files
try:
rmtree(os.path.join(current_app.config['MEDIA_LOCATION'], share.id))
os.remove(os.path.join(current_app.config['MEDIA_LOCATION'], share.id + '.zip'))
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 False
else:
return True
@app.route('/privacy')
......@@ -40,7 +84,7 @@ def upload():
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)),
up = Share(id=share_id, name=form.name.data, expires=datetime.now() + timedelta(minutes=float(form.timer.data)),
download_limit=form.download_limit.data)
db.session.add(up)
# create share folder
......@@ -67,26 +111,10 @@ def upload():
return render_template('Upload.html', form=form)
def check_expired(share):
if share.download_limit < 0 or datetime.now() > share.expires:
# delete files
try:
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 True
return False
@app.route('/shared/<string:share_id>')
def shared(share_id):
share = Share.query.get_or_404(share_id)
check_expired(share)
check_limit(share)
return render_template('Shared.html', url=url_for('download', share_id=share.id))
......@@ -95,16 +123,13 @@ def download(share_id):
share = Share.query.get_or_404(share_id)
share.download_limit -= 1
db.session.commit()
if check_expired(share):
return redirect(url_for('expired'))
else:
return render_template('Download.html', up=share)
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)
check_limit(share)
return send_from_directory(os.path.join(current_app.config['MEDIA_LOCATION'], share_id), filename=filename,
as_attachment=True)
......@@ -112,6 +137,6 @@ def media(share_id, filename):
@app.route('/zip/<string:share_id>')
def zip(share_id):
share = Share.query.get_or_404(share_id)
check_expired(share)
check_limit(share)
return send_from_directory(os.path.join(current_app.config['MEDIA_LOCATION']),
filename=str(share.id) + '.zip', as_attachment=True)
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