From 661112e147f2b22f252262bfc749e24c2515b662 Mon Sep 17 00:00:00 2001 From: gourav Date: Sat, 8 Aug 2026 12:53:00 +0530 Subject: [PATCH] first commit --- .dockerignore | 1 + .gitignore | 3 +++ Dockerfile | 13 ++++++++++++ buid_requirements.txt | 5 +++++ compose.yaml | 10 +++++++++ main.py | 47 +++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 44 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 123 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 buid_requirements.txt create mode 100644 compose.yaml create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..95a48fb --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +uploads \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f7725c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env +__pycache__ +uploads/* \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0b7e634 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.14-alpine + +WORKDIR /app + +COPY buid_requirements.txt . +RUN pip install --no-cache-dir -r buid_requirements.txt + +COPY main.py . + +RUN mkdir /uploads +COPY uploads/test.txt /uploads/test.txt + +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/buid_requirements.txt b/buid_requirements.txt new file mode 100644 index 0000000..926839f --- /dev/null +++ b/buid_requirements.txt @@ -0,0 +1,5 @@ +bcrypt==5.0.0 +fastapi==0.141.1 +python-dotenv==1.2.2 +python-multipart==0.0.32 +uvicorn==0.52.1 diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..e80b241 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,10 @@ +services: + uploader: + build: . + container_name: file-uploader + ports: + - "8899:8000" + environment: + UPLOAD_HASH: "${UPLOAD_HASH}" + volumes: + - ./uploads:/uploads \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..645d1e6 --- /dev/null +++ b/main.py @@ -0,0 +1,47 @@ +import os + +import bcrypt +from fastapi import Depends, FastAPI, HTTPException, UploadFile, Response +from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer +from dotenv import load_dotenv +from pathlib import Path + +load_dotenv() + +app = FastAPI() + +UPLOAD_HASH = os.environ["UPLOAD_HASH"] +UPLOAD_PATH = "uploads" + +security = HTTPBearer() + + +def authenticate( + credentials: HTTPAuthorizationCredentials = Depends(security), +): + token = credentials.credentials.encode() + + if not bcrypt.checkpw(token, UPLOAD_HASH.encode()): + raise HTTPException( + status_code=401, + detail="Invalid authentication credentials", + ) + + +@app.get('/test', dependencies=[Depends(authenticate)]) +async def file_test(): + test_file = f"/{UPLOAD_PATH}/test.txt" + with open(test_file) as f: + content = f.read() + return {'message': content} + + +@app.post("/upload", dependencies=[Depends(authenticate)]) +async def upload(file: UploadFile): + with open(Path(f"/{UPLOAD_PATH}/{file.filename}"), "wb") as f: + while chunk := await file.read(64 * 1024): + f.write(chunk) + + await file.close() + + return {"status": "success"} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..89ebb60 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,44 @@ +annotated-doc==0.0.5 +annotated-types==0.8.0 +anyio==4.14.2 +bcrypt==5.0.0 +certifi==2026.7.22 +click==8.4.2 +detect-installer==0.1.0 +dnspython==2.8.0 +email-validator==2.3.0 +fastapi==0.141.1 +fastapi-cli==0.0.32 +fastapi-cloud-cli==0.23.0 +fastar==0.11.0 +h11==0.16.0 +httpcore==1.0.9 +httptools==0.8.0 +httpx==0.28.1 +idna==3.18 +Jinja2==3.1.6 +markdown-it-py==4.2.0 +MarkupSafe==3.0.3 +mdurl==0.1.2 +pydantic==2.13.4 +pydantic-extra-types==2.11.1 +pydantic-settings==2.15.0 +pydantic_core==2.46.4 +Pygments==2.20.0 +python-dotenv==1.2.2 +python-multipart==0.0.32 +PyYAML==6.0.3 +rich==15.0.0 +rich-toolkit==0.20.3 +rignore==0.8.1 +sentry-sdk==2.66.1 +shellingham==1.5.4 +starlette==1.4.1 +typer==0.27.1 +typing-inspection==0.4.2 +typing_extensions==4.16.0 +urllib3==2.7.0 +uvicorn==0.52.1 +uvloop==0.22.1 +watchfiles==1.2.0 +websockets==17.0.1