diff --git a/app/main.py b/app/main.py index 2cec98d202ff1ae9ffff722679f0798483a9e66e..e3c79e0812cb248df6b6b12bfca6a9c71ae7a932 100644 --- a/app/main.py +++ b/app/main.py @@ -1,7 +1,8 @@ #!/usr/bin/env python3 #Import of Libraries +from numpy import imag from waitress import serve #Used as webserver (Production) -from flask import Flask, request, redirect, make_response, abort #Used to prepare the dynamic pages +from flask import Flask, request, redirect, make_response, abort, Response #Used to prepare the dynamic pages from os import environ #Used for getting the enviorement variables from itsdangerous import URLSafeSerializer #used for signing the cookies from random import choice#used for signing the cookies @@ -198,10 +199,11 @@ def delete(): if(loginEnabled): return deleteLink(request, s) else: abort(404) -@app.route('/user/makeqr', methods=['POST']) +@app.route('/makeqr', methods=['GET']) def makeQrCode(): - link = request.form.get('link') - return "data:image/jpeg;base64," + makeQR(url_scheme + "://" + link) + link = request.args.get('link') + image = makeQR(url_scheme + "://" + link) + return Response(image, mimetype='image/jpg') @app.route('/user/api', methods=['POST']) def api(): diff --git a/app/makeqr.py b/app/makeqr.py index d05bf7fadca325527c092c5441eebce116acf958..4a1c8141ed2dc148368b45d4b023d042a39e630c 100644 --- a/app/makeqr.py +++ b/app/makeqr.py @@ -1,23 +1,23 @@ #!/usr/bin/env python3 +from statistics import mode from qrcode import QRCode, constants #Used to generate the QR -from base64 import b64encode #Used to encode the generated QR as base64, to directly insert it into the HTML from io import BytesIO #Needed for base64 encoding of the image from PIL import Image #Needed for QR generation def makeQR(text): #This function is used to create a QR code and encode it base64, if you make a new shortlink - qr = QRCode( #QR generation variables - version=1, - error_correction=constants.ERROR_CORRECT_L, - box_size=10, - border=1, - ) - qr.add_data(text) #The URL is in the text variable - qr.make(fit=True) #Generate the QR + qr = QRCode( + version=1, + error_correction=constants.ERROR_CORRECT_L, + box_size=20, + border=4, + ) + qr.add_data(text) + qr.make(fit=True) img = qr.make_image(fill_color="black", back_color="white") #Encode the WR as base 64 with BytesIO() as buffer: img.save(buffer, 'jpeg') - return b64encode(buffer.getvalue()).decode() + return buffer.getvalue() if (__name__ == "__main__"): print("This file is not made for direct call, please run the main.py") diff --git a/app/newurl.py b/app/newurl.py index 7a7baff47f1278ca6439564a2e74abad91d76bcf..e0f4c212fbec08b57b0957e22cf80f3341d9b9f9 100644 --- a/app/newurl.py +++ b/app/newurl.py @@ -2,8 +2,6 @@ from grecaptcha_verify import grecaptcha_verify from flask import render_template from sqlite3 import connect -from makeqr import makeQR - def newurl(request, skipCaptcha, recaptchaPrivateKey, recaptchaPublicKey, builddate, domain_prepared, domain_to_index, showDomainSelect, cookieNotice, s, url_scheme, loginEnabled, passwordProtected, password): try: userID = s.loads(request.cookies.get('userID')) @@ -38,7 +36,7 @@ def newurl(request, skipCaptcha, recaptchaPrivateKey, recaptchaPublicKey, buildd 'INSERT INTO WEB_URL (LONG_URL, SHORT_URL, USERNAME) VALUES (?, ?, ?)', [url, shorturl, userID] ) - return render_template('home.html', short_url=shorturl, recaptchaPublicKey=recaptchaPublicKey, builddate=builddate, domain=domain_prepared, qrcode=makeQR(url_scheme + "://" + shorturl), loginbar=loginbar, cookieNotice=cookieNotice, loginEnabled=loginEnabled) #return the shorten link to the user + return render_template('home.html', short_url=shorturl, recaptchaPublicKey=recaptchaPublicKey, builddate=builddate, domain=domain_prepared, loginbar=loginbar, cookieNotice=cookieNotice, loginEnabled=loginEnabled) #return the shorten link to the user else: return render_template('home.html', builddate=builddate, recaptchaPublicKey=recaptchaPublicKey, domain=domain_prepared, snackbar="URL already used, please try another one", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')], showDomainSelect=showDomainSelect, loginbar=loginbar, cookieNotice=cookieNotice, loginEnabled=loginEnabled) #return the user the prefilled form with an error message, because the url was already used diff --git a/app/templates/home.html b/app/templates/home.html index d963aa807ed4879803439f7c1c3f2693350b851e..9f3cce16a67963bf3a573cda93dc452a8b1b8f26 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -49,7 +49,7 @@ </form> {% else %} <h3>Your shortened URL is: <a href="http://{{short_url}}"> {{short_url}}</a></h3> - <img src="data:image/jpeg;base64,{{qrcode}}"> + <img src="/makeqr?link={{short_url}}"> <p><a href="/">create another link</a></p> {% endif %} <p class="message">© Made by <a href="https://jonasled.de">jonasled</a><br>