1- from fastapi import File , UploadFile , HTTPException , status , Request , Form , FastAPI , Header
2- from fastapi .responses import PlainTextResponse , HTMLResponse , RedirectResponse , JSONResponse
1+ from fastapi import (
2+ File ,
3+ UploadFile ,
4+ HTTPException ,
5+ status ,
6+ Request ,
7+ Form ,
8+ FastAPI ,
9+ Header ,
10+ Response ,
11+ )
12+ from fastapi .responses import (
13+ PlainTextResponse ,
14+ HTMLResponse ,
15+ RedirectResponse ,
16+ JSONResponse ,
17+ )
318import shutil
419import os
520import json
621from pathlib import Path
7- from fastapi import FastAPI
822from fastapi .templating import Jinja2Templates
923from fastapi .middleware .cors import CORSMiddleware
1024from slowapi .errors import RateLimitExceeded
1630from pygments .lexers import get_lexer_by_name , guess_lexer
1731from pygments .formatters import HtmlFormatter
1832from pygments .util import ClassNotFound
19- from typing import List , Optional , Any
33+ from typing import List , Optional
2034
2135limiter = Limiter (key_func = get_remote_address )
2236app : FastAPI = FastAPI (title = "paste.py 🐍" )
4155
4256BASE_DIR : Path = Path (__file__ ).resolve ().parent
4357
44- templates : Jinja2Templates = Jinja2Templates (
45- directory = str (Path (BASE_DIR , "templates" )))
58+ templates : Jinja2Templates = Jinja2Templates (directory = str (Path (BASE_DIR , "templates" )))
4659
4760
4861@app .post ("/file" )
4962@limiter .limit ("100/minute" )
50- async def post_as_a_file (request : Request , file : UploadFile = File (...)) -> PlainTextResponse :
63+ async def post_as_a_file (
64+ request : Request , file : UploadFile = File (...)
65+ ) -> PlainTextResponse :
5166 try :
5267 uuid : str = generate_uuid ()
5368 if uuid in large_uuid_storage :
5469 uuid = generate_uuid ()
5570 # Extract file extension from the filename
5671 try :
57- file_extension : str = Path (file .filename ).suffix [1 :]
58- path : str = f"data/{ uuid } .{ file_extension } "
72+ file_extension : Optional [str ] = None
73+ if file .filename is not None :
74+ file_extension = Path (file .filename ).suffix [1 :]
75+ path : str = f"data/{ uuid } { file_extension } "
5976 except Exception :
6077 path = f"data/{ uuid } "
6178 finally :
6279 val : str = "/" .join (path .split ("/" )[1 :])
6380 with open (path , "wb" ) as f :
6481 shutil .copyfileobj (file .file , f )
6582 large_uuid_storage .append (uuid )
66- print (large_uuid_storage )
6783 except Exception :
6884 raise HTTPException (
6985 detail = "There was an error uploading the file" ,
@@ -75,7 +91,9 @@ async def post_as_a_file(request: Request, file: UploadFile = File(...)) -> Plai
7591
7692
7793@app .get ("/paste/{uuid}" )
78- async def get_paste_data (uuid : str , user_agent : Optional [str ] = Header (None )) -> Any :
94+ async def get_paste_data (
95+ uuid : str , user_agent : Optional [str ] = Header (None )
96+ ) -> Response :
7997 path : str = f"data/{ uuid } "
8098 try :
8199 with open (path , "rb" ) as f :
@@ -97,13 +115,11 @@ async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) ->
97115 try :
98116 lexer = get_lexer_by_name (file_extension , stripall = True )
99117 except ClassNotFound :
100- lexer = get_lexer_by_name (
101- "text" , stripall = True ) # Default lexer
118+ lexer = get_lexer_by_name ("text" , stripall = True ) # Default lexer
102119 formatter = HtmlFormatter (
103- style = "colorful" , full = True , linenos = "inline" , cssclass = 'code' )
120+ style = "colorful" , full = True , linenos = "inline" , cssclass = "code"
121+ )
104122 highlighted_code : str = highlight (content , lexer , formatter )
105-
106- print (highlighted_code )
107123 custom_style = """
108124 .code pre span.linenos {
109125 color: #999;
@@ -154,19 +170,16 @@ async def get_paste_data(uuid: str, user_agent: Optional[str] = Header(None)) ->
154170 </body>
155171 </html>
156172 """
157- return HTMLResponse (
158- content = response_content
159- )
160- except Exception as e :
161- print (e )
173+ return HTMLResponse (content = response_content )
174+ except Exception :
162175 raise HTTPException (
163176 detail = "404: The Requested Resource is not found" ,
164177 status_code = status .HTTP_404_NOT_FOUND ,
165178 )
166179
167180
168181@app .get ("/" , response_class = HTMLResponse )
169- async def indexpage (request : Request ) -> HTMLResponse :
182+ async def indexpage (request : Request ) -> Response :
170183 return templates .TemplateResponse ("index.html" , {"request" : request })
171184
172185
@@ -187,7 +200,7 @@ async def delete_paste(uuid: str) -> PlainTextResponse:
187200
188201
189202@app .get ("/web" , response_class = HTMLResponse )
190- async def web (request : Request ) -> HTMLResponse :
203+ async def web (request : Request ) -> Response :
191204 return templates .TemplateResponse ("web.html" , {"request" : request })
192205
193206
@@ -209,8 +222,7 @@ async def web_post(
209222 with open (path , "wb" ) as f :
210223 f .write (file_content )
211224 large_uuid_storage .append (uuid_ )
212- except Exception as e :
213- print (e )
225+ except Exception :
214226 raise HTTPException (
215227 detail = "There was an error uploading the file" ,
216228 status_code = status .HTTP_403_FORBIDDEN ,
0 commit comments