From b07e3824041b6aecc01297005fbfe0e1d869f0a2 Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Mon, 11 Dec 2023 04:22:24 +0530 Subject: [PATCH] fix: minors --- Dockerfile | 2 +- src/paste/main.py | 32 ++++++++++++++++++++------------ src/paste/templates/index.html | 14 +++++++------- src/paste/test.py | 0 src/paste/utils.py | 4 ++-- 5 files changed, 30 insertions(+), 22 deletions(-) delete mode 100644 src/paste/test.py diff --git a/Dockerfile b/Dockerfile index b20aa25..ba5cb14 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ RUN pip install pdm # copy files COPY pyproject.toml pdm.lock README.md /project/ -COPY data/ project/ +COPY data/ project/data COPY src/ /project/src diff --git a/src/paste/main.py b/src/paste/main.py index a04b5ef..d217eb2 100644 --- a/src/paste/main.py +++ b/src/paste/main.py @@ -5,7 +5,6 @@ from pathlib import Path from fastapi import FastAPI from fastapi.templating import Jinja2Templates -from .schema import Data from .utils import generate_uuid app = FastAPI(title="paste.py 🐍") @@ -30,13 +29,15 @@ def post_as_a_file(file: UploadFile = File(...)): 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("/{uuid}/") + +@app.get("/paste/{uuid}") def post_as_a_text(uuid): path = f"data/{uuid}" try: @@ -44,27 +45,34 @@ def post_as_a_text(uuid): 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): return templates.TemplateResponse("index.html", {"request": request}) -@app.delete("/{uuid}/", response_class=PlainTextResponse) + +@app.delete("/paste/{uuid}", response_class=PlainTextResponse) 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 ) + raise HTTPException( + detail=f"The exception is {e}", status_code=status.HTTP_409_CONFLICT) + @app.get("/web", response_class=HTMLResponse) 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) @@ -78,10 +86,10 @@ def web_post(content: str = Form(...)): with open(path, 'wb') as f: f.write(file_content) large_uuid_storage.append(uuid) - print(large_uuid_storage) 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) - - return RedirectResponse(f"http://paste.fosscu.org/{uuid}/", status_code=status.HTTP_303_SEE_OTHER) + 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) diff --git a/src/paste/templates/index.html b/src/paste/templates/index.html index 17482f6..213f87d 100644 --- a/src/paste/templates/index.html +++ b/src/paste/templates/index.html @@ -11,7 +11,7 @@ API USAGE - POST https://paste.fosscu.org/ + POST https://paste.fosscu.org/paste Send the raw data along. Will respond with a link to the paste. @@ -21,12 +21,12 @@ uploaded. If the response code is anything else, an error has occurred. Pasting is heavily rate limited. - GET https://paste.fosscu.org/<id>/; + GET https://paste.fosscu.org/paste/<id> Retrieve the paste with the given id as plain-text. - DELETE https://paste.fosscu.org/<id>/; + DELETE https://paste.fosscu.org/paste/<id> Delete the paste with the given id. @@ -34,15 +34,15 @@ Paste a file named 'file.txt' using cURL: - curl -X POST -f "file=@file.txt" https://paste.fosscu.org/ + curl -X POST -f "file=@file.txt" https://paste.fosscu.org/file Paste from stdin using cURL: - echo "Hello, world." | curl -X POST -f "file=@-" https://paste.fosscu.org/ + echo "Hello, world." | curl -X POST -f "file=@-" https://paste.fosscu.org/file Delete an existing paste with id <id> using cURL: - curl -X DELETE https://paste.fosscu.org/<id>/; + curl -X DELETE https://paste.fosscu.org/paste/<id> A shell function that can be added to `.bashrc` or `.bash_profle` for quick pasting from the command line. The command takes a filename or reads @@ -51,7 +51,7 @@ function paste() { local file=${1:-/dev/stdin} - curl -X POST -f "file=@${file}" https://paste.fosscu.org + curl -X POST -f "file=@${file}" https://paste.fosscu.org/file } \ No newline at end of file diff --git a/src/paste/test.py b/src/paste/test.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/paste/utils.py b/src/paste/utils.py index 32a4709..d2e50ce 100644 --- a/src/paste/utils.py +++ b/src/paste/utils.py @@ -2,6 +2,7 @@ import string import os + def generate_uuid(): # Combine uppercase letters, lowercase letters, and digits characters = string.ascii_letters + string.digits @@ -11,8 +12,7 @@ def generate_uuid(): return random_code + def extract_extension(file_name): _, extension = os.path.splitext(file_name) return extension - -print(extract_extension("file.txt")) \ No newline at end of file