Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the sanitization function to a asyncio.to_thread #44

Merged
merged 2 commits into from
May 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading