uploader/main.py

81 lines
2.2 KiB
Python
Raw Normal View History

2024-05-06 08:03:24 +00:00
import logging
2024-01-01 05:56:43 +00:00
import os
import tempfile
import uuid
2024-05-06 08:03:24 +00:00
import aioboto3
2024-01-01 05:56:43 +00:00
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
from starlette.requests import Request
2024-05-06 09:00:57 +00:00
from auth import login_required
from settings import (
PORT,
STORJ_ACCESS_KEY,
STORJ_SECRET_KEY,
CDN_DOMAIN,
STORJ_BUCKET_NAME,
STORJ_END_POINT,
)
2024-01-01 05:56:43 +00:00
2024-05-06 08:03:24 +00:00
# Logging configuration
2024-05-06 09:00:57 +00:00
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s"
)
2024-01-01 05:56:43 +00:00
2024-05-06 09:00:57 +00:00
@login_required
2024-01-01 05:56:43 +00:00
async def upload_handler(request: Request):
2024-05-06 08:03:24 +00:00
logging.debug("Received upload request")
2024-01-01 05:56:43 +00:00
form = await request.form()
2024-05-06 09:00:57 +00:00
file = form.get("file")
2024-01-01 05:56:43 +00:00
if file is None:
2024-05-06 08:03:24 +00:00
logging.error("No file uploaded")
2024-05-06 09:00:57 +00:00
return JSONResponse({"error": "No file uploaded"}, status_code=400)
2024-01-01 05:56:43 +00:00
file_name, file_extension = os.path.splitext(file.filename)
key = str(uuid.uuid4()) + file_extension
2024-05-06 08:03:24 +00:00
logging.debug(f"Generated file key: {key}")
2024-01-01 05:56:43 +00:00
2024-05-06 09:00:57 +00:00
async with aioboto3.client(
"s3",
aws_access_key_id=STORJ_ACCESS_KEY,
aws_secret_access_key=STORJ_SECRET_KEY,
endpoint_url=STORJ_END_POINT,
) as s3:
2024-01-01 05:56:43 +00:00
with tempfile.NamedTemporaryFile() as tmp_file:
while True:
2024-05-06 07:50:19 +00:00
chunk = await file.read(8192)
2024-01-01 05:56:43 +00:00
if not chunk:
break
tmp_file.write(chunk)
2024-05-06 07:50:19 +00:00
tmp_file.flush()
2024-01-01 05:56:43 +00:00
2024-05-06 08:03:24 +00:00
logging.debug("Starting file upload to S3")
2024-05-06 07:50:19 +00:00
await s3.upload_file(
2024-01-01 05:56:43 +00:00
Filename=tmp_file.name,
Bucket=STORJ_BUCKET_NAME,
Key=key,
2024-05-06 09:00:57 +00:00
ExtraArgs={"ContentType": file.content_type},
2024-01-01 05:56:43 +00:00
)
2024-05-06 08:03:24 +00:00
logging.debug("File upload completed")
2024-01-01 05:56:43 +00:00
2024-05-06 09:00:57 +00:00
url = f"http://{CDN_DOMAIN}/{key}"
2024-05-06 08:03:24 +00:00
logging.info(f"File uploaded successfully: {url}")
2024-05-06 09:00:57 +00:00
return JSONResponse({"url": url, "originalFilename": file.filename})
2024-01-01 05:56:43 +00:00
async def home(request: Request):
2024-05-06 08:03:24 +00:00
logging.debug("Home route called")
2024-05-06 09:00:57 +00:00
return JSONResponse({"message": "Hello World!"})
2024-01-01 05:56:43 +00:00
routes = [
2024-05-06 09:00:57 +00:00
Route("/test", home, methods=["GET"]),
Route("/", upload_handler, methods=["POST"]),
2024-01-01 05:56:43 +00:00
]
app = Starlette(debug=True, routes=routes)