Commit 5fca45f2 authored by Lukas Böhm's avatar Lukas Böhm
Browse files

make download limit optional

this time in logic
parent 978a04c0
......@@ -30,7 +30,7 @@ class Share(db.Model):
self.id = share_id
self.name = name
self.expires = expires
self.download_limit = download_limit if download_limit > 0 else 100000000
self.download_limit = download_limit if download_limit > 0 else None
self.is_public = is_public
self.password = generate_password_hash(password) if password else None
# create folder
......
......@@ -10,11 +10,15 @@ from app import task_queue as que
from shutil import make_archive, rmtree
def get_and_check_share(share_id) -> Share:
def get_and_check_share(share_id, decrease_limit=False) -> Share:
share = Share.query.get(share_id)
if share is None:
return None
if share.download_limit <= 0 or (share.expires is not None and datetime.now() > share.expires):
if share.download_limit and decrease_limit:
share.download_limit -= 1
db.session.commit()
if (share.download_limit and share.download_limit < 0) or\
(share.expires is not None and datetime.now() > share.expires):
que.enqueue(delete_share, share.id)
return None
return share
......@@ -118,13 +122,11 @@ def download(share_id):
@app.route('/d/<string:share_id>/<string:filename>', methods=['GET'])
def media(share_id, filename):
share = get_and_check_share(share_id)
share = get_and_check_share(share_id, decrease_limit=True)
if share is None: return redirect(url_for('expired'))
if share.password:
if share_id not in session:
return redirect(url_for('share_auth', share_id=share_id))
share.download_limit -= 1
db.session.commit()
return send_from_directory(os.path.join(current_app.config['MEDIA_LOCATION'], share.id),
filename=filename, 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