From 24cfc1aa8c81fdb2f3b8b874bddbf645ec03b353 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Mon, 13 Feb 2023 14:54:31 -0800 Subject: [PATCH] setup upload func --- flipbook/helpers.py | 8 ++++- flipbook/routes/manage.py | 44 +++++++++++++++++++++++-- flipbook/templates/includes/header.html | 1 + 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/flipbook/helpers.py b/flipbook/helpers.py index f35f2a4..57a4c65 100644 --- a/flipbook/helpers.py +++ b/flipbook/helpers.py @@ -1,6 +1,8 @@ from web3.auto import w3 from eth_account.messages import encode_defunct +from flipbook import config + def verify_signature(message, signature, public_address): msg = encode_defunct(text=message) @@ -8,4 +10,8 @@ def verify_signature(message, signature, public_address): if recovered.lower() == public_address.lower(): return True else: - return False \ No newline at end of file + return False + +def allowed_file(filename): + return "." in filename and \ + filename.rsplit(".", 1)[1].lower() in config.ALLOWED_EXTENSIONS \ No newline at end of file diff --git a/flipbook/routes/manage.py b/flipbook/routes/manage.py index 21d6974..334ffb8 100644 --- a/flipbook/routes/manage.py +++ b/flipbook/routes/manage.py @@ -1,5 +1,12 @@ -from quart import Blueprint, render_template, request, redirect, url_for, flash -from flask_login import login_required +from secrets import token_urlsafe +from pathlib import Path + +from quart import Blueprint, render_template, request, redirect, url_for, flash, send_from_directory +from flask_login import login_required, current_user +from werkzeug.utils import secure_filename + +from flipbook.helpers import allowed_file +from flipbook import config bp = Blueprint('manage', 'manage', url_prefix='/manage') @@ -12,4 +19,35 @@ async def index(): @bp.route('/upload', methods=['GET', 'POST']) @login_required async def upload(): - return await render_template('upload.html') \ No newline at end of file + if request.method == "POST": + data = await request.get_data() + print(data) + post_title = request.form.get("title") + if "file" not in request.files: + await flash("You didn't upload a caliente meme, bro! You're fuckin up!", "is-danger") + return await redirect(request.url) + file = request.files["file"] + if file.filename == "": + await flash("You didn't upload a caliente meme, bro! You're fuckin up!", "is-danger") + return await redirect(request.url) + if post_title == "": + await flash("You didn't give your meme a spicy title, bro! You're fuckin up!", "is-danger") + return await redirect(request.url) + if file and allowed_file(file.filename): + filename = "{}-{}".format( + token_urlsafe(12), + secure_filename(file.filename) + ) + file.save(Path(config.UPLOADS_PATH, filename)) + upload = Upload( + token_id=0, + title=post_title, + image_name=filename, + text=request.form.get('text', ''), + wallet=current_user, + ) + upload.save() + # upload.save_thumbnail() + await flash("Uploaded successfully!", "is-success") + return redirect(url_for("manage.index")) + return render_template("upload.html") \ No newline at end of file diff --git a/flipbook/templates/includes/header.html b/flipbook/templates/includes/header.html index 998607d..4cf4fef 100644 --- a/flipbook/templates/includes/header.html +++ b/flipbook/templates/includes/header.html @@ -5,6 +5,7 @@
  • Home
  • {% if current_user.is_authenticated %}
  • Manage
  • +
  • upload
  • Logout
  • {% else %}
  • Connect