diff --git a/.gitignore b/.gitignore index 2dc53ca..7d490f1 100644 --- a/.gitignore +++ b/.gitignore @@ -50,6 +50,7 @@ coverage.xml .hypothesis/ .pytest_cache/ cover/ +/data # Translations *.mo @@ -152,9 +153,10 @@ dmypy.json # Cython debug symbols cython_debug/ + # PyCharm # JetBrains specific template is maintained in a separate JetBrains.gitignore that can # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -.idea/ +.idea/ \ No newline at end of file diff --git a/data/MAQR b/data/MAQR new file mode 100644 index 0000000..7d957d0 --- /dev/null +++ b/data/MAQR @@ -0,0 +1 @@ +This is a test data \ No newline at end of file diff --git a/data/test b/data/test deleted file mode 100644 index af27ff4..0000000 --- a/data/test +++ /dev/null @@ -1 +0,0 @@ -This is a test file. \ No newline at end of file diff --git a/pre-commit-config.yaml b/pre-commit-config.yaml new file mode 100644 index 0000000..6f4b854 --- /dev/null +++ b/pre-commit-config.yaml @@ -0,0 +1,10 @@ +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.1.9 + hooks: + - id: ruff + args: + - --fix + - id: ruff-format +ci: + autoupdate_schedule: weekly diff --git a/src/paste/main.py b/src/paste/main.py index b60bb1b..c4eb327 100644 --- a/src/paste/main.py +++ b/src/paste/main.py @@ -2,6 +2,7 @@ from fastapi.responses import PlainTextResponse, HTMLResponse, RedirectResponse import shutil import os +import sys from pathlib import Path from fastapi import FastAPI from fastapi.templating import Jinja2Templates @@ -32,25 +33,29 @@ templates = Jinja2Templates(directory=str(Path(BASE_DIR, "templates"))) +MAX_UPLOAD_SIZE = 20_000_000 # 20 MB @app.post("/file") @limiter.limit("100/minute") -async def post_as_a_file(request: Request, file: UploadFile = File(...)): +async def post_as_a_file(file: UploadFile = File(...)): + if file.content_type != "text/plain": + raise HTTPException(detail="Only text/plain is supported", + status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE) + if file.filesize > MAX_UPLOAD_SIZE: + raise HTTPException(detail="File size is too large", + status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE) 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() @@ -58,17 +63,21 @@ async def post_as_a_file(request: Request, file: UploadFile = File(...)): @app.get("/paste/{uuid}") -async def post_as_a_text(uuid): +def post_as_a_text(uuid): path = f"data/{uuid}" + text = "" try: - with open(path, "rb") as f: - return PlainTextResponse(f.read()) + with open(path, 'rb') as f: + text = f.read() + if sys.getsizeof(text) > MAX_UPLOAD_SIZE: + raise HTTPException(detail="File size is too large", + status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE) + else: + return PlainTextResponse(text) 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)