Skip to content

Commit

Permalink
Change the sanitization function to a asyncio.to_thread (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy authored May 20, 2024
1 parent aae3379 commit ba8f937
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions views/htmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from __future__ import annotations

import asyncio
import datetime
import json
from typing import TYPE_CHECKING, Any, cast
Expand Down Expand Up @@ -46,26 +47,36 @@ class HTMXView(starlette_plus.View, prefix="htmx"):
def __init__(self, app: Application) -> None:
self.app: Application = app

def highlight_code(self, filename: str, content: str, *, index: int, raw_url: str, annotation: str) -> str:
filename = bleach.clean(filename, attributes=[], tags=[])
filename = "_".join(filename.splitlines())

content = bleach.clean(content.replace("<!", "&lt;&#33;"), attributes=[], tags=[], strip_comments=False)
annotations: str = f'<small class="annotations">❌ {annotation}</small>' if annotation else ""

return f"""
<div id="__paste_a_{index}" class="pasteArea">
<div class="pasteHeader">
<div style="display: flex; gap: 0.5rem; align-items: center;">
<span class="filenameArea">{filename}</span>
<span class="pasteButton" onclick="hideFile(this, {index})">Hide</span>
<span id="__paste_copy_{index}" class="pasteButton" onclick="copyFile({index})">Copy</span>
<a class="pasteButton" href="{raw_url}/{index + 1}">Raw</a>
def highlight_code(self, *, files: list[dict[str, Any]]) -> str:
html: str = ""

for index, file in enumerate(files):
filename = bleach.clean(file["filename"], attributes=[], tags=[])
filename = "_".join(filename.splitlines())

raw_url: str = f'/raw/{file["parent_id"]}'
annotation: str = file["annotation"]

content = bleach.clean(
file["content"].replace("<!", "&lt;&#33;"), attributes=[], tags=[], strip_comments=False
)
annotations: str = f'<small class="annotations">❌ {annotation}</small>' if annotation else ""

html += f"""
<div id="__paste_a_{index}" class="pasteArea">
<div class="pasteHeader">
<div style="display: flex; gap: 0.5rem; align-items: center;">
<span class="filenameArea">{filename}</span>
<span class="pasteButton" onclick="hideFile(this, {index})">Hide</span>
<span id="__paste_copy_{index}" class="pasteButton" onclick="copyFile({index})">Copy</span>
<a class="pasteButton" href="{raw_url}/{index + 1}">Raw</a>
</div>
</div>
</div>
{annotations}
<pre id="__paste_c_{index}" class="fileContent" style="display: flex; flex-grow: 1;"><code>{content}</code></pre>
</div>"""
{annotations}
<pre id="__paste_c_{index}" class="fileContent" style="display: flex; flex-grow: 1;"><code>{content}</code></pre>
</div>"""

return html

def check_discord(self, request: starlette_plus.Request) -> starlette_plus.Response | None:
agent: str = request.headers.get("user-agent", "")
Expand Down Expand Up @@ -154,15 +165,7 @@ async def paste(self, request: starlette_plus.Request) -> starlette_plus.Respons
</div>
"""

for i, file in enumerate(files):
html += self.highlight_code(
file["filename"],
file["content"],
index=i,
raw_url=raw_url,
annotation=file["annotation"],
)

html += await asyncio.to_thread(self.highlight_code, files=files)
if htmx_url and password:
return starlette_plus.HTMLResponse(html, headers={"HX-Replace-Url": f"{url}?pastePassword={password}"})

Expand Down

0 comments on commit ba8f937

Please sign in to comment.