Make links case-insensitive

This commit is contained in:
Parker M. 2024-11-03 16:31:15 -06:00
parent de4dc0bc5b
commit db1857a18f
Signed by: parker
GPG Key ID: 505ED36FC12B5D5E
2 changed files with 18 additions and 6 deletions

View File

@ -37,19 +37,21 @@ async def create_link(
# Check if the URL is valid
if not validators.url(url.url):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Invalid URL"
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Invalid URL",
)
# Create the new link and add it to the database
while True:
try:
link_path = "".join(
random.choices(string.ascii_uppercase + "1234567890", k=5)
)
).upper()
new_link = Link(
link=link_path,
owner=api_key["owner"],
redirect_link=url.url,
expire_date=datetime.datetime.now() + datetime.timedelta(days=30),
expire_date=datetime.datetime.now()
+ datetime.timedelta(days=30),
)
db.add(new_link)
db.commit()
@ -70,6 +72,7 @@ async def delete_link(
db=Depends(get_db),
api_key: str = Security(check_api_key),
):
link = link.upper()
# Get the link and check the owner
link = db.query(Link).filter(Link.link == link).first()
if not link:
@ -93,12 +96,16 @@ async def delete_link(
return {"response": "Link successfully deleted", "link": link.link}
@router.get("/{link}/records", summary="Get all of the IP log records associated with a link")
@router.get(
"/{link}/records",
summary="Get all of the IP log records associated with a link",
)
async def get_link_records(
link: Annotated[str, Path(title="Link to get records for")],
db=Depends(get_db),
api_key: str = Security(check_api_key),
):
link = link.upper()
# Get the link and check the owner
link = db.query(Link).filter(Link.link == link).first()
if not link:
@ -116,12 +123,16 @@ async def get_link_records(
return records
@router.delete("/{link}/records", summary="Delete all of the IP log records associated with a link")
@router.delete(
"/{link}/records",
summary="Delete all of the IP log records associated with a link",
)
async def delete_link_records(
link: Annotated[str, Path(title="Link to delete records for")],
db=Depends(get_db),
api_key: str = Security(check_api_key),
):
link = link.upper()
# Get the link and check the owner
link = db.query(Link).filter(Link.link == link).first()
if not link:

View File

@ -154,7 +154,8 @@ Log all records for visits to shortened links
@app.route("/<link>", methods=["GET"])
def log_redirect(link):
def log_redirect(link: str):
link = link.upper()
# If `link` is not exactly 5 characters, return redirect to base url
if len(link) != 5:
return redirect(BASE_URL)