Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limited upload size #10

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ coverage.xml
.hypothesis/
.pytest_cache/
cover/
/data

# Translations
*.mo
Expand Down Expand Up @@ -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/
1 change: 1 addition & 0 deletions data/MAQR
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test data
1 change: 0 additions & 1 deletion data/test

This file was deleted.

10 changes: 10 additions & 0 deletions pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
37 changes: 23 additions & 14 deletions src/paste/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -32,43 +33,51 @@

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()

return PlainTextResponse(uuid, status_code=status.HTTP_201_CREATED)


@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)
Expand Down
Loading