diff --git a/app/newurl.py b/app/newurl.py index e0f4c212fbec08b57b0957e22cf80f3341d9b9f9..1d34a72677b3b553e62f704b77b1e1b85bcff4d6 100644 --- a/app/newurl.py +++ b/app/newurl.py @@ -2,6 +2,9 @@ from grecaptcha_verify import grecaptcha_verify from flask import render_template from sqlite3 import connect +import string +import random + 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')) @@ -9,6 +12,7 @@ def newurl(request, skipCaptcha, recaptchaPrivateKey, recaptchaPublicKey, buildd except: userID = "null" loginbar = '<a href="/user/login" >login</a>' + short = request.form.get('short') if not grecaptcha_verify(request, skipCaptcha, recaptchaPrivateKey): return render_template('home.html', builddate=builddate, domain=domain_prepared, snackbar="There was an error validating, that you are a human, please try again.", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')], recaptchaPublicKey=recaptchaPublicKey, showDomainSelect=showDomainSelect, loginEnabled=loginEnabled, loginbar=loginbar, cookieNotice=cookieNotice, passwordProtected=passwordProtected) #return the user the prefilled form with an error message, because recaptcha failed if passwordProtected and (request.form.get("password") != password): @@ -16,8 +20,8 @@ def newurl(request, skipCaptcha, recaptchaPrivateKey, recaptchaPublicKey, buildd if (request.form.get('url').replace(" ", "") == ""): return render_template('home.html', builddate=builddate, domain=domain_prepared, snackbar="Please enter a url to short, before submitting this form", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')], recaptchaPublicKey=recaptchaPublicKey, showDomainSelect=showDomainSelect, loginEnabled=loginEnabled, loginbar=loginbar, cookieNotice=cookieNotice, passwordProtected=passwordProtected) #return the user the prefilled form with an error message, because no url to short was provided if (request.form.get('short').replace(" ", "") == ""): - return render_template('home.html', builddate=builddate, domain=domain_prepared, snackbar="Please enter a short name, before submitting this form", long_url_prefilled=request.form.get('url'), short_url_prefilled=request.form.get('short').lower(), domain_prefilled=domain_to_index[request.form.get('domain')], recaptchaPublicKey=recaptchaPublicKey, showDomainSelect=showDomainSelect, loginEnabled=loginEnabled, loginbar=loginbar, cookieNotice=cookieNotice, passwordProtected=passwordProtected) #return the user the prefilled form with an error message, because no short link was provided - shorturl = (request.form.get('domain') + "/" + request.form.get('short').replace(" ", "_").replace("/", "").replace("?","")).lower() + short = gen_short(request.form.get('domain')) + shorturl = (request.form.get('domain') + "/" + short.replace(" ", "_").replace("/", "").replace("?","")).lower() url = request.form.get('url') with connect('db/urls.db') as conn: #Check if another user already used the short link @@ -40,6 +44,24 @@ def newurl(request, skipCaptcha, recaptchaPrivateKey, recaptchaPublicKey, buildd 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 +def gen_short(domain): + """ + This function generates a random. + """ + short = ''.join(random.choices(string.ascii_uppercase + + string.digits, k = 5)) + with connect('db/urls.db') as conn: #Check if another user already used the short link + cursor = conn.cursor() + res = cursor.execute('SELECT LONG_URL FROM WEB_URL WHERE SHORT_URL=?', [domain + "/" + short]) + try: + dbResponse = res.fetchone() + if dbResponse is not None: + return gen_short(domain) + return short + except: + return gen_short(domain) + + if (__name__ == "__main__"): print("This file is not made for direct call, please run the main.py") exit() \ No newline at end of file