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

moved delete back to routes

and introduced the get_and_check method so i dont have to use the 404 error to mark expired shares.
parent 8dc0c492
......@@ -18,7 +18,6 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['MAX_MEDIA_SIZE'] = 1000 * 1024 * 1024 # 1000 MB
db = SQLAlchemy(app)
# task_queue = Queue(connection=Redis(host='chiefsend-redis', port=6379))
task_queue = Queue(connection=Redis.from_url('redis://chiefsend-redis'))
from app.models import *
......
......@@ -2,7 +2,6 @@ import os
from flask import current_app
from app import db, app
class Share(db.Model):
__tablename__ = 'Share'
......@@ -17,7 +16,6 @@ class Share(db.Model):
return f'<Share {self.id}>'
def __init__(self, name, expires, download_limit):
# with app.app_context():
# create id
import uuid
share_id = str(uuid.uuid4())
......@@ -33,24 +31,6 @@ class Share(db.Model):
os.makedirs(os.path.join(current_app.config['MEDIA_LOCATION'], share_id))
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
# wrap it up
# db.session.add(self)
# db.session.commit()
def delete(self):
with app.app_context():
# delete files
try:
from shutil import rmtree
rmtree(os.path.join(current_app.config['MEDIA_LOCATION'], self.id))
os.remove(os.path.join(current_app.config['MEDIA_LOCATION'], self.id + '.zip'))
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
# delete from database
for att in self.files:
db.session.delete(att)
db.session.delete(self)
db.session.commit()
class Attachment(db.Model):
......@@ -64,14 +44,9 @@ class Attachment(db.Model):
return f'<Attachment {self.id}, {self.filename} for Share {self.share_id}>'
def __init__(self, share_id, file):
#with app.app_context():
# set attributes
self.share_id = share_id
self.filename = file.filename
# save file
from werkzeug.utils import secure_filename
fn = secure_filename(filename=file.filename)
file.save(os.path.join(current_app.config['MEDIA_LOCATION'], share_id, fn))
# wrap it up
# db.session.add(self)
# db.session.commit()
......@@ -6,23 +6,31 @@ 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 make_archive
from app import task_queue as que
def delete_share(share_id):
with app.app_context():
share = Share.query.get_or_404(share_id)
# delete files
try:
from shutil import rmtree
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()
from app import task_queue
def test_rq():
return 'Hello weld'
@app.route('/secret')
def secret():
from datetime import timedelta
import time
job = task_queue.enqueue_in(timedelta(seconds=5), test_rq)
for _ in range(15):
print(job.result)
time.sleep(1)
return str(job)
@app.route('/delete/<string:share_id>')
def secret(share_id):
sh = Share.query.get_or_404(share_id)
job = que.enqueue_in(timedelta(seconds=8), delete_share, sh.id)
return str(job.id)
@app.route('/privacy')
......@@ -46,7 +54,6 @@ def http_error(e):
@app.route('/expired')
@app.errorhandler(404)
def expired(e=None):
return render_template('Expired.html')
......@@ -66,13 +73,14 @@ def upload():
# files
for file in form.files.data:
db.session.add(Attachment(share_id=share.id, file=file))
db.session.commit()
# create zip
make_archive(
os.path.join(current_app.config['MEDIA_LOCATION'], share.id),
'zip',
os.path.join(current_app.config['MEDIA_LOCATION'], share.id)
)
# put timer into scheduler queue
# que.enqueue_at(share.expires, Share.delete, share)
# wrap it up
db.session.commit()
return url_for('shared', share_id=share.id)
......@@ -80,39 +88,45 @@ def upload():
return render_template('Upload.html', form=form)
def get_and_check_share(share_id):
share = Share.query.get(share_id)
if share is None:
return None
if share.download_limit <= 0:
que.enqueue(delete_share, share.id)
return None
return share
@app.route('/shared/<string:share_id>')
def shared(share_id):
share = Share.query.get_or_404(share_id)
share = get_and_check_share(share_id)
if share is None: return redirect(url_for('expired'))
return render_template('Shared.html', url=url_for('download', share_id=share.id))
@app.route('/d/<string:share_id>', methods=['GET'])
def download(share_id):
share = Share.query.get_or_404(share_id)
share = get_and_check_share(share_id)
if share is None: return redirect(url_for('expired'))
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)
return send_from_directory(os.path.join(current_app.config['MEDIA_LOCATION'], share_id), filename=filename,
as_attachment=True)
share = get_and_check_share(share_id)
if share is None: return redirect(url_for('expired'))
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)
@app.route('/zip/<string:share_id>')
def zip(share_id):
share = Share.query.get_or_404(share_id)
share = get_and_check_share(share_id)
if share is None: return redirect(url_for('expired'))
return send_from_directory(os.path.join(current_app.config['MEDIA_LOCATION']),
filename=str(share.id) + '.zip', as_attachment=True)
@app.route("/upload-test", methods=["GET", "POST"])
def upload_test():
form = UploadForm()
print(form.data.items())
if form.validate_on_submit():
print(form.files.data[0])
return url_for('expired')
return render_template("Upload.html", form=form)
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