first commit
This commit is contained in:
commit
661112e147
1
.dockerignore
Normal file
1
.dockerignore
Normal file
@ -0,0 +1 @@
|
|||||||
|
uploads
|
||||||
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.env
|
||||||
|
__pycache__
|
||||||
|
uploads/*
|
||||||
13
Dockerfile
Normal file
13
Dockerfile
Normal file
@ -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"]
|
||||||
5
buid_requirements.txt
Normal file
5
buid_requirements.txt
Normal file
@ -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
|
||||||
10
compose.yaml
Normal file
10
compose.yaml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
services:
|
||||||
|
uploader:
|
||||||
|
build: .
|
||||||
|
container_name: file-uploader
|
||||||
|
ports:
|
||||||
|
- "8899:8000"
|
||||||
|
environment:
|
||||||
|
UPLOAD_HASH: "${UPLOAD_HASH}"
|
||||||
|
volumes:
|
||||||
|
- ./uploads:/uploads
|
||||||
47
main.py
Normal file
47
main.py
Normal file
@ -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"}
|
||||||
44
requirements.txt
Normal file
44
requirements.txt
Normal file
@ -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
|
||||||
Loading…
Reference in New Issue
Block a user