From 1d0d6fcf9ddf38a359641f7e4ba77e51924594ee Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Sat, 30 Dec 2023 05:49:19 +0530 Subject: [PATCH 1/9] :recycle: Refactor: Added typehints --- src/paste/utils.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/paste/utils.py b/src/paste/utils.py index d2e50ce..50c804f 100644 --- a/src/paste/utils.py +++ b/src/paste/utils.py @@ -1,18 +1,19 @@ import random import string import os +from pathlib import Path -def generate_uuid(): +def generate_uuid() -> str: # Combine uppercase letters, lowercase letters, and digits - characters = string.ascii_letters + string.digits + characters: str = string.ascii_letters + string.digits # Generate a random 4-character code - random_code = ''.join(random.choice(characters) for _ in range(4)) + random_code: str = "".join(random.choice(characters) for _ in range(4)) return random_code -def extract_extension(file_name): +def extract_extension(file_name: Path) -> str: _, extension = os.path.splitext(file_name) return extension From f2402b4c62bafb0721270a75757ccb3b911ec706 Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Sat, 30 Dec 2023 05:52:45 +0530 Subject: [PATCH 2/9] :bug: Fix: Fixed typehints and extensions --- src/paste/main.py | 62 ++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/src/paste/main.py b/src/paste/main.py index 1fc041c..f61a411 100644 --- a/src/paste/main.py +++ b/src/paste/main.py @@ -1,10 +1,24 @@ -from fastapi import File, UploadFile, HTTPException, status, Request, Form, FastAPI, Header -from fastapi.responses import PlainTextResponse, HTMLResponse, RedirectResponse, JSONResponse +from fastapi import ( + File, + UploadFile, + HTTPException, + status, + Request, + Form, + FastAPI, + Header, + Response, +) +from fastapi.responses import ( + PlainTextResponse, + HTMLResponse, + RedirectResponse, + JSONResponse, +) import shutil import os import json from pathlib import Path -from fastapi import FastAPI from fastapi.templating import Jinja2Templates from fastapi.middleware.cors import CORSMiddleware from slowapi.errors import RateLimitExceeded @@ -16,7 +30,7 @@ from pygments.lexers import get_lexer_by_name, guess_lexer from pygments.formatters import HtmlFormatter from pygments.util import ClassNotFound -from typing import List, Optional, Any +from typing import List, Optional limiter = Limiter(key_func=get_remote_address) app: FastAPI = FastAPI(title="paste.py 🐍") @@ -41,21 +55,24 @@ BASE_DIR: Path = Path(__file__).resolve().parent -templates: Jinja2Templates = Jinja2Templates( - directory=str(Path(BASE_DIR, "templates"))) +templates: Jinja2Templates = Jinja2Templates(directory=str(Path(BASE_DIR, "templates"))) @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: uuid = generate_uuid() # Extract file extension from the filename try: - file_extension: str = Path(file.filename).suffix[1:] - path: str = f"data/{uuid}.{file_extension}" + file_extension: Optional[str] = None + if file.filename is not None: + file_extension = Path(file.filename).suffix[1:] + path: str = f"data/{uuid}{file_extension}" except Exception: path = f"data/{uuid}" finally: @@ -63,7 +80,6 @@ async def post_as_a_file(request: Request, file: UploadFile = File(...)) -> Plai with open(path, "wb") as f: shutil.copyfileobj(file.file, f) large_uuid_storage.append(uuid) - print(large_uuid_storage) except Exception: raise HTTPException( detail="There was an error uploading the file", @@ -75,7 +91,9 @@ async def post_as_a_file(request: Request, file: UploadFile = File(...)) -> Plai @app.get("/paste/{uuid}") -async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) -> Any: +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: @@ -97,13 +115,11 @@ async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) -> try: lexer = get_lexer_by_name(file_extension, stripall=True) except ClassNotFound: - lexer = get_lexer_by_name( - "text", stripall=True) # Default lexer + lexer = get_lexer_by_name("text", stripall=True) # Default lexer formatter = HtmlFormatter( - style="colorful", full=True, linenos="inline", cssclass='code') + style="colorful", full=True, linenos="inline", cssclass="code" + ) highlighted_code: str = highlight(content, lexer, formatter) - - print(highlighted_code) custom_style = """ .code pre span.linenos { color: #999; @@ -154,11 +170,8 @@ async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) -> """ - return HTMLResponse( - content=response_content - ) - except Exception as e: - print(e) + return HTMLResponse(content=response_content) + except Exception: raise HTTPException( detail="404: The Requested Resource is not found", status_code=status.HTTP_404_NOT_FOUND, @@ -166,7 +179,7 @@ async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) -> @app.get("/", response_class=HTMLResponse) -async def indexpage(request: Request) -> HTMLResponse: +async def indexpage(request: Request) -> Response: return templates.TemplateResponse("index.html", {"request": request}) @@ -187,7 +200,7 @@ async def delete_paste(uuid: str) -> PlainTextResponse: @app.get("/web", response_class=HTMLResponse) -async def web(request: Request) -> HTMLResponse: +async def web(request: Request) -> Response: return templates.TemplateResponse("web.html", {"request": request}) @@ -209,8 +222,7 @@ async def web_post( with open(path, "wb") as f: f.write(file_content) large_uuid_storage.append(uuid_) - except Exception as e: - print(e) + except Exception: raise HTTPException( detail="There was an error uploading the file", status_code=status.HTTP_403_FORBIDDEN, From d1570688acb45b528fda13588ed07015e5108726 Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Sat, 30 Dec 2023 05:53:17 +0530 Subject: [PATCH 3/9] :memo: Docs: Update docs --- src/paste/templates/index.html | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/paste/templates/index.html b/src/paste/templates/index.html index 3bb2389..d6412d2 100644 --- a/src/paste/templates/index.html +++ b/src/paste/templates/index.html @@ -1,15 +1,18 @@ - paste.py 🐍 - - - - - - - - + paste.py 🐍 + + + + + + + +