Add wishId as optional parameter to requests

This commit is contained in:
sqozz 2018-04-07 21:57:34 +02:00
parent ae3ca5b0dc
commit d02c458e2a
1 changed files with 6 additions and 9 deletions

View File

@ -35,18 +35,15 @@ def short(shortLink=""):
else: else:
return render_template("index.html", name=shortLink) # Landing page return render_template("index.html", name=shortLink) # Landing page
elif request.method == "POST": # Someone submitted a new link to short elif request.method == "POST": # Someone submitted a new link to short
wishId = request.form["wishId"] longUrl = request.form["url"] # required, accept the exception if the key does not exist
longUrl = request.form["url"] wishId = request.form.get("wishId")
if wishId: databaseId = insertIdUnique(longUrl, idToCheck=wishId)
databaseId = insertIdUnique(wishId, longUrl)
else:
databaseId = insertIdUnique("", longUrl)
return request.url_root + databaseId # Short link in plain text return request.url_root + databaseId # Short link in plain text
def insertIdUnique(idToCheck, longUrl): def insertIdUnique(longUrl, idToCheck=None):
hashUrl = hashlib.sha256(longUrl.encode()).digest() hashUrl = hashlib.sha256(longUrl.encode()).digest()
base64Url = base64.urlsafe_b64encode(hashUrl).decode() base64Url = base64.urlsafe_b64encode(hashUrl).decode()
if len(idToCheck) == 0: if idToCheck == None:
idToCheck = base64Url[:4] idToCheck = base64Url[:4]
conn = sqlite3.connect("data/links.sqlite") conn = sqlite3.connect("data/links.sqlite")
@ -67,7 +64,7 @@ def insertIdUnique(idToCheck, longUrl):
conn.commit() conn.commit()
conn.close() conn.close()
if len(base64Url) - 1 >= len(idToCheck) + 1: if len(base64Url) - 1 >= len(idToCheck) + 1:
databaseId = insertIdUnique(base64Url[:len(idToCheck)+1], longUrl) databaseId = insertIdUnique(longUrl, idToCheck=base64Url[:len(idToCheck)+1])
else: else:
print("Can't produce a long enough hash from the new link to be unique. This should never happen") print("Can't produce a long enough hash from the new link to be unique. This should never happen")
print("Bailing out, you are on your own. Good luck.") print("Bailing out, you are on your own. Good luck.")