Skip to content

Commit

Permalink
🎨 chore: Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Sunglasses committed Dec 28, 2023
1 parent db7d327 commit 484f070
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions src/paste/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,88 +24,95 @@

BASE_DIR = Path(__file__).resolve().parent

templates = Jinja2Templates(directory=str(Path(BASE_DIR, 'templates')))
templates = Jinja2Templates(directory=str(Path(BASE_DIR, "templates")))


@app.post("/file")
def post_as_a_file(file: UploadFile = File(...)):
async def post_as_a_file(file: UploadFile = File(...)):
try:
uuid = generate_uuid()
if uuid in large_uuid_storage:
uuid = generate_uuid()
path = f"data/{uuid}"
with open(path, 'wb') as f:
with open(path, "wb") as f:
shutil.copyfileobj(file.file, f)
large_uuid_storage.append(uuid)
print(large_uuid_storage)
except Exception:
# return {"message": "There was an error uploading the file"}
raise HTTPException(detail="There was an error uploading the file",
status_code=status.HTTP_403_FORBIDDEN)
raise HTTPException(
detail="There was an error uploading the file",
status_code=status.HTTP_403_FORBIDDEN,
)
finally:
file.file.close()

return PlainTextResponse(uuid, status_code=status.HTTP_201_CREATED)


@app.get("/paste/{uuid}")
def post_as_a_text(uuid):
async def post_as_a_text(uuid):
path = f"data/{uuid}"
try:
with open(path, 'rb') as f:
with open(path, "rb") as f:
return PlainTextResponse(f.read())
except Exception as e:
print(e)
raise HTTPException(detail="404: The Requested Resource is not found",
status_code=status.HTTP_404_NOT_FOUND)
raise HTTPException(
detail="404: The Requested Resource is not found",
status_code=status.HTTP_404_NOT_FOUND,
)


@app.get("/", response_class=HTMLResponse)
def indexpage(request: Request):
async def indexpage(request: Request):
return templates.TemplateResponse("index.html", {"request": request})


@app.delete("/paste/{uuid}", response_class=PlainTextResponse)
def delete_paste(uuid):
async def delete_paste(uuid):
path = f"data/{uuid}"
try:
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)
detail=f"The exception is {e}", status_code=status.HTTP_409_CONFLICT
)


@app.get("/web", response_class=HTMLResponse)
def web(request: Request):
async def web(request: Request):
return templates.TemplateResponse("web.html", {"request": request})


@app.post("/web", response_class=PlainTextResponse)
def web_post(content: str = Form(...)):
# print(content)
# return PlainTextResponse(content=content)
async def web_post(content: str = Form(...)):
try:
file_content = content.encode()
uuid = generate_uuid()
if uuid in large_uuid_storage:
uuid = generate_uuid()
path = f"data/{uuid}"
with open(path, 'wb') as f:
with open(path, "wb") as f:
f.write(file_content)
large_uuid_storage.append(uuid)
except Exception as e:
# return {"message": "There was an error uploading the file"}
print(e)
raise HTTPException(detail="There was an error uploading the file",
status_code=status.HTTP_403_FORBIDDEN)
raise HTTPException(
detail="There was an error uploading the file",
status_code=status.HTTP_403_FORBIDDEN,
)

return RedirectResponse(f"http://paste.fosscu.org/paste/{uuid}", status_code=status.HTTP_303_SEE_OTHER)
return RedirectResponse(
f"http://paste.fosscu.org/paste/{uuid}", status_code=status.HTTP_303_SEE_OTHER
)


@app.get("/health", status_code=status.HTTP_200_OK)
def health() -> dict[str, str]:
async def health() -> dict[str, str]:
return {"status": "ok"}

0 comments on commit 484f070

Please sign in to comment.