1
0
forked from crony/UpFast

Fix error when using python 3.9

This commit is contained in:
CronyAkatsuki 2023-03-26 08:53:01 +02:00
parent ea7409556a
commit 93c062eadf

View File

@ -1,4 +1,5 @@
import shutil, magic, re import shutil, magic, re
from typing import Annotated, Union
from os import listdir, remove from os import listdir, remove
from os.path import abspath, dirname, exists from os.path import abspath, dirname, exists
from fastapi import FastAPI, Request, UploadFile, File, Header from fastapi import FastAPI, Request, UploadFile, File, Header
@ -56,7 +57,7 @@ app.mount("/static", StaticFiles(directory="static"), name='static')
# show the homepage when just getting the website # show the homepage when just getting the website
@app.get("/", response_class=HTMLResponse) @app.get("/", response_class=HTMLResponse)
async def index(request: Request, user_agent: str | None = Header(default=None)): async def index(request: Request, user_agent: Annotated[Union[str, None], Header()] = None):
if re.search("^curl/.*", str(user_agent)): if re.search("^curl/.*", str(user_agent)):
return PlainTextResponse("It fucking works!\n") return PlainTextResponse("It fucking works!\n")
else: else:
@ -66,7 +67,7 @@ async def index(request: Request, user_agent: str | None = Header(default=None))
# get the file user want's to upload and save it # get the file user want's to upload and save it
@app.post("/") @app.post("/")
async def upload( request: Request, file: UploadFile = File(...), user_agent: str | None = Header(default=None)): async def upload( request: Request, file: UploadFile = File(...), user_agent: Annotated[Union[str, None], Header()] = None):
file_location = f"upload/{file.filename}" file_location = f"upload/{file.filename}"
if exists(file_location): if exists(file_location):
context = f'''File: "{request.url._url}files/{file.filename}" already exists on the server! context = f'''File: "{request.url._url}files/{file.filename}" already exists on the server!
@ -82,7 +83,7 @@ Please rename the file or delete the one on the server\n'''
# show the files page with the list of all files or if run with curl list all files in url format # show the files page with the list of all files or if run with curl list all files in url format
@app.get("/files") @app.get("/files")
async def files( request: Request, user_agent: str | None = Header(default=None)): async def files( request: Request, user_agent: Annotated[Union[str, None], Header()] = None):
path = f"{dirname(abspath(__file__))}/upload/" path = f"{dirname(abspath(__file__))}/upload/"
file_list = listdir(path) file_list = listdir(path)
files = file_list_generator(path, file_list) files = file_list_generator(path, file_list)
@ -98,7 +99,7 @@ async def files( request: Request, user_agent: str | None = Header(default=None)
# delete specific file when getting this request # delete specific file when getting this request
@app.get("/delete/{file}") @app.get("/delete/{file}")
async def delete(request: Request, file: str, user_agent: str | None = Header(default=None)): async def delete(request: Request, file: str, user_agent: Annotated[Union[str, None], Header()] = None):
file_path = f"{dirname(abspath(__file__))}/upload/{file}" file_path = f"{dirname(abspath(__file__))}/upload/{file}"
if exists(file_path): if exists(file_path):
remove(file_path) remove(file_path)