Skip to content

Commit da809bc

Browse files
committed
fix: now apiclients, curl will give plaintext as response
1 parent 77623fb commit da809bc

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/paste/main.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from fastapi import File, UploadFile, HTTPException, status, Request, Form, FastAPI
1+
from fastapi import File, UploadFile, HTTPException, status, Request, Form, FastAPI, Header
22
from fastapi.responses import PlainTextResponse, HTMLResponse, RedirectResponse, JSONResponse
33
import shutil
44
import os
@@ -16,7 +16,7 @@
1616
from pygments.lexers import get_lexer_by_name, guess_lexer
1717
from pygments.formatters import HtmlFormatter
1818
from pygments.util import ClassNotFound
19-
from typing import List, Optional
19+
from typing import List, Optional, Any
2020

2121
limiter = Limiter(key_func=get_remote_address)
2222
app: FastAPI = FastAPI(title="paste.py 🐍")
@@ -74,11 +74,18 @@ async def post_as_a_file(request: Request, file: UploadFile = File(...)) -> Plai
7474

7575

7676
@app.get("/paste/{uuid}")
77-
async def get_paste_data(uuid: str) -> HTMLResponse:
77+
async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) -> Any:
7878
path: str = f"data/{uuid}"
7979
try:
8080
with open(path, "rb") as f:
8181
content: str = f.read().decode("utf-8")
82+
# Check if the request is from a browser
83+
is_browser_request = "Mozilla" in user_agent if user_agent else False
84+
85+
if not is_browser_request:
86+
# Return plain text response
87+
return PlainTextResponse(content)
88+
8289
# Get file extension from the filename
8390
file_extension: str = Path(path).suffix[1:]
8491
if file_extension == "":

0 commit comments

Comments
 (0)