Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ license = {text = "MIT"}

[tool.pdm.scripts]
start = "uvicorn src.paste.main:app --host 0.0.0.0 --port 8080 --workers 4"
dev = "uvicorn src.paste.main:app --host 0.0.0.0 --port 8080 --reload"
test = "pytest"
mypy = "mypy src/paste"

Expand Down
4 changes: 4 additions & 0 deletions src/paste/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__version__: str = "2.0.0"
__author__: str = "fosscu"
__contact__: str = "[email protected]"
__url__: str = "https://fosscu.org"
45 changes: 23 additions & 22 deletions src/paste/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,24 @@
from pygments.formatters import HtmlFormatter
from pygments.util import ClassNotFound
from typing import List, Optional
from . import __version__, __author__, __contact__, __url__

description: str = "paste.py 🐍 - A pastebin written in python."

limiter = Limiter(key_func=get_remote_address)
app: FastAPI = FastAPI(title="paste.py 🐍")
app: FastAPI = FastAPI(
title="paste.py 🐍",
version=__version__,
contact=dict(
name=__author__,
url=__url__,
email=__contact__,
),
license_info=dict(name="MIT", url="https://opensource.org/license/mit/"),
openapi_url=None,
docs_url=None,
redoc_url=None,
)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

Expand All @@ -60,9 +75,7 @@

@app.post("/file")
@limiter.limit("100/minute")
async def post_as_a_file(
request: Request, file: UploadFile = File(...)
) -> PlainTextResponse:
async def post_as_a_file(request: Request, file: UploadFile = File(...)) -> PlainTextResponse:
try:
uuid: str = generate_uuid()
if uuid in large_uuid_storage:
Expand Down Expand Up @@ -91,9 +104,7 @@ async def post_as_a_file(


@app.get("/paste/{uuid}")
async def get_paste_data(
uuid: str, user_agent: Optional[str] = Header(None)
) -> Response:
async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) -> Response:
path: str = f"data/{uuid}"
try:
with open(path, "rb") as f:
Expand All @@ -116,9 +127,7 @@ async def get_paste_data(
lexer = get_lexer_by_name(file_extension, stripall=True)
except ClassNotFound:
lexer = get_lexer_by_name("text", stripall=True) # Default lexer
formatter = HtmlFormatter(
style="colorful", full=True, linenos="inline", cssclass="code"
)
formatter = HtmlFormatter(style="colorful", full=True, linenos="inline", cssclass="code")
highlighted_code: str = highlight(content, lexer, formatter)
custom_style = """
.code pre span.linenos {
Expand Down Expand Up @@ -190,13 +199,9 @@ async def delete_paste(uuid: str) -> PlainTextResponse:
os.remove(path)
return PlainTextResponse(f"File successfully deleted {uuid}")
except FileNotFoundError:
raise HTTPException(
detail="File Not Found", status_code=status.HTTP_404_NOT_FOUND
)
raise HTTPException(detail="File Not Found", status_code=status.HTTP_404_NOT_FOUND)
except Exception as e:
raise HTTPException(
detail=f"The exception is {e}", status_code=status.HTTP_409_CONFLICT
)
raise HTTPException(detail=f"The exception is {e}", status_code=status.HTTP_409_CONFLICT)


@app.get("/web", response_class=HTMLResponse)
Expand All @@ -206,9 +211,7 @@ async def web(request: Request) -> Response:

@app.post("/web", response_class=PlainTextResponse)
@limiter.limit("100/minute")
async def web_post(
request: Request, content: str = Form(...), extension: Optional[str] = Form(None)
) -> RedirectResponse:
async def web_post(request: Request, content: str = Form(...), extension: Optional[str] = Form(None)) -> RedirectResponse:
try:
file_content: bytes = content.encode()
uuid: str = generate_uuid()
Expand All @@ -228,9 +231,7 @@ async def web_post(
status_code=status.HTTP_403_FORBIDDEN,
)

return RedirectResponse(
f"{BASE_URL}/paste/{uuid_}", status_code=status.HTTP_303_SEE_OTHER
)
return RedirectResponse(f"{BASE_URL}/paste/{uuid_}", status_code=status.HTTP_303_SEE_OTHER)


@app.get("/health", status_code=status.HTTP_200_OK)
Expand Down