Skip to content

Commit

Permalink
Simplify handling of missing extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Aug 7, 2022
1 parent 6779391 commit 134d28d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/main.py
Expand Up @@ -42,7 +42,7 @@ async def robots(request):
app.run(
host="0.0.0.0",
port=5000,
auto_reload=settings.DEBUG,
debug=settings.DEBUG,
access_log=False,
motd=False,
fast=True,
Expand Down
1 change: 1 addition & 0 deletions app/utils/urls.py
Expand Up @@ -56,6 +56,7 @@ def clean(url: str) -> str:

# Replace invalid regex escape sequences
url = url.replace("\\", "~b")
url = url.replace("\n", "~n")

# Replace spaces with underscores
url = url.replace(" ", "_")
Expand Down
23 changes: 16 additions & 7 deletions app/views/shortcuts.py
Expand Up @@ -97,25 +97,34 @@ async def custom_path(request, template_id, text_paths):
logger.warning(f"Fixing trailing quote: {text_paths}")
text_paths = text_paths.rstrip('"')

if "." in text_paths.strip(".") and text_paths.split(".")[-1].isalnum():
text_filepath = text_paths
elif text_paths.startswith("."):
if text_paths.startswith("."):
return response.redirect(
request.app.url_for(
"images.detail_blank",
template_filename=f"{template_id}{text_paths}",
)
)
else:
text_filepath = text_paths + "." + settings.DEFAULT_EXTENSION

if not settings.DEBUG:
try:
url = request.app.url_for(
"images.detail_text",
template_id=template_id,
text_filepath=text_paths,
**params,
)
except exceptions.URLBuildError as e:
if "text_filepath" in str(e):
logger.warning(f"Handing missing extension: {e}")
else:
raise
url = request.app.url_for(
"images.detail_text",
template_id=template_id,
text_filepath=text_filepath,
text_filepath=text_paths + "." + settings.DEFAULT_EXTENSION,
**params,
)

if not settings.DEBUG:
return response.redirect(url)

template = models.Template.objects.get_or_create(template_id)
Expand Down

0 comments on commit 134d28d

Please sign in to comment.