From bd59c75a15896f21a04d762ef8139232b89a1a92 Mon Sep 17 00:00:00 2001 From: nexy7574 Date: Tue, 2 Jul 2024 01:17:46 +0100 Subject: [PATCH] Include a count as to how large the result URL will be --- src/db.py | 4 +++- src/main.py | 16 +++++++++------- src/static/index.html | 10 +++++++++- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/db.py b/src/db.py index 32cd11e..faaa64c 100644 --- a/src/db.py +++ b/src/db.py @@ -7,7 +7,9 @@ from tortoise.contrib.pydantic import pydantic_model_creator class Redirect(Model): uuid = fields.UUIDField(primary_key=True, default=uuid.uuid4) - slug = fields.CharField(max_length=32779, index=False, default=lambda: secrets.token_urlsafe(1024)) + slug = fields.CharField( + max_length=32779, index=False, default=lambda: secrets.token_urlsafe(1024) + ) destination = fields.CharField(max_length=8192) created_at = fields.DatetimeField(auto_now_add=True) expires = fields.DatetimeField(null=True) diff --git a/src/main.py b/src/main.py index 012393f..4960941 100644 --- a/src/main.py +++ b/src/main.py @@ -94,12 +94,12 @@ async def list_redirects(): @app.post("/api/create", response_model=db.RedirectPydantic) async def create_redirect( - response: JSONResponse, - destination: str = Form(...), - expires: typing.Optional[datetime.datetime] = Form(None), - max_visits: typing.Optional[int] = Form(None), - slug_length: int = Form(2048, gt=0, lte=16389), - slug_type: str = Form("urlsafe", regex="^(urlsafe|hex|words|uuid)$"), + response: JSONResponse, + destination: str = Form(...), + expires: typing.Optional[datetime.datetime] = Form(None), + max_visits: typing.Optional[int] = Form(None), + slug_length: int = Form(2048, gt=0, lte=16389), + slug_type: str = Form("urlsafe", regex="^(urlsafe|hex|words|uuid)$"), ): parsed = urlparse(destination) if not parsed.scheme or not parsed.netloc: @@ -136,7 +136,9 @@ async def create_redirect( while await db.Redirect.get_or_none(slug=slug) is not None: slug = secrets.token_urlsafe(slug_length // 2) - redirect = await db.Redirect.create(destination=destination, expires=expires, max_visits=max_visits, slug=slug) + redirect = await db.Redirect.create( + destination=destination, expires=expires, max_visits=max_visits, slug=slug + ) response.status_code = 201 return await db.RedirectPydantic.from_tortoise_orm(redirect) diff --git a/src/static/index.html b/src/static/index.html index fa44ed9..99e03dd 100644 --- a/src/static/index.html +++ b/src/static/index.html @@ -5,6 +5,12 @@ Link Elongater - Create Redirect @@ -84,7 +91,8 @@
-
+ +

URL length will total to a lot of characters.