Refactor illegal character removal to a separate module so we don't have circular dependencies
This commit is contained in:
@ -9,6 +9,7 @@ import sentry_sdk
|
|||||||
from fastapi import FastAPI, File, Request, UploadFile
|
from fastapi import FastAPI, File, Request, UploadFile
|
||||||
from fastapi.responses import HTMLResponse, JSONResponse
|
from fastapi.responses import HTMLResponse, JSONResponse
|
||||||
|
|
||||||
|
from discord_embed.misc import remove_illegal_chars
|
||||||
from discord_embed.settings import serve_domain, upload_folder, webhook_url
|
from discord_embed.settings import serve_domain, upload_folder, webhook_url
|
||||||
from discord_embed.video_file_upload import do_things
|
from discord_embed.video_file_upload import do_things
|
||||||
from discord_embed.webhook import send_webhook
|
from discord_embed.webhook import send_webhook
|
||||||
@ -70,48 +71,6 @@ async def upload_file(file: Annotated[UploadFile, File()]) -> JSONResponse:
|
|||||||
return JSONResponse(content={"html_url": html_url})
|
return JSONResponse(content={"html_url": html_url})
|
||||||
|
|
||||||
|
|
||||||
def remove_illegal_chars(file_name: str) -> str:
|
|
||||||
"""Remove illegal characters from the filename.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
file_name: The filename to remove illegal characters from.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Returns a string with the filename without illegal characters.
|
|
||||||
"""
|
|
||||||
filename: str = file_name.replace(" ", ".")
|
|
||||||
illegal_characters: list[str] = [
|
|
||||||
'"',
|
|
||||||
",",
|
|
||||||
";",
|
|
||||||
":",
|
|
||||||
"?",
|
|
||||||
"{",
|
|
||||||
"}",
|
|
||||||
"「",
|
|
||||||
"」",
|
|
||||||
"@",
|
|
||||||
"*",
|
|
||||||
"/",
|
|
||||||
"&",
|
|
||||||
"#",
|
|
||||||
"%",
|
|
||||||
"^",
|
|
||||||
"+",
|
|
||||||
"<",
|
|
||||||
"=",
|
|
||||||
">",
|
|
||||||
"|",
|
|
||||||
"△",
|
|
||||||
"$",
|
|
||||||
]
|
|
||||||
for character in illegal_characters:
|
|
||||||
filename: str = filename.replace(character, "")
|
|
||||||
logger.info("Removed illegal character: %s from filename", character)
|
|
||||||
|
|
||||||
return filename
|
|
||||||
|
|
||||||
|
|
||||||
index_html: str = """
|
index_html: str = """
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
||||||
|
47
discord_embed/misc.py
Normal file
47
discord_embed/misc.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger: logging.Logger = logging.getLogger("uvicorn.error")
|
||||||
|
|
||||||
|
|
||||||
|
def remove_illegal_chars(file_name: str) -> str:
|
||||||
|
"""Remove illegal characters from the filename.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_name: The filename to remove illegal characters from.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Returns a string with the filename without illegal characters.
|
||||||
|
"""
|
||||||
|
filename: str = file_name.replace(" ", ".")
|
||||||
|
illegal_characters: list[str] = [
|
||||||
|
'"',
|
||||||
|
",",
|
||||||
|
";",
|
||||||
|
":",
|
||||||
|
"?",
|
||||||
|
"{",
|
||||||
|
"}",
|
||||||
|
"「",
|
||||||
|
"」",
|
||||||
|
"@",
|
||||||
|
"*",
|
||||||
|
"/",
|
||||||
|
"&",
|
||||||
|
"#",
|
||||||
|
"%",
|
||||||
|
"^",
|
||||||
|
"+",
|
||||||
|
"<",
|
||||||
|
"=",
|
||||||
|
">",
|
||||||
|
"|",
|
||||||
|
"△",
|
||||||
|
"$",
|
||||||
|
]
|
||||||
|
for character in illegal_characters:
|
||||||
|
filename: str = filename.replace(character, "")
|
||||||
|
logger.info("Removed illegal character: %s from filename", character)
|
||||||
|
|
||||||
|
return filename
|
@ -6,7 +6,7 @@ from typing import TYPE_CHECKING
|
|||||||
|
|
||||||
from discord_embed import settings
|
from discord_embed import settings
|
||||||
from discord_embed.generate_html import generate_html_for_videos
|
from discord_embed.generate_html import generate_html_for_videos
|
||||||
from discord_embed.main import remove_illegal_chars
|
from discord_embed.misc import remove_illegal_chars
|
||||||
from discord_embed.video import Resolution, make_thumbnail, video_resolution
|
from discord_embed.video import Resolution, make_thumbnail, video_resolution
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
Reference in New Issue
Block a user