Create /upload_token endpoint
This commit is contained in:
+29
-14
@@ -1,5 +1,3 @@
|
||||
from fastapi import FastAPI, File, UploadFile
|
||||
|
||||
import dotenv
|
||||
import os
|
||||
import random
|
||||
@@ -8,6 +6,13 @@ import string
|
||||
import asyncio
|
||||
import uvicorn
|
||||
|
||||
from datetime import datetime
|
||||
from fastapi import FastAPI, Response, status, Request
|
||||
|
||||
from schemas.api_schemas import *
|
||||
from s3_worker import S3Worker
|
||||
from redis_worker import RedisWorker
|
||||
|
||||
dotenv.load_dotenv()
|
||||
disable_docs = os.getenv("DISABLE_DOCS", "true").lower() == "true"
|
||||
app: FastAPI = FastAPI(title="DropMeFiles analog")
|
||||
@@ -18,21 +23,31 @@ app: FastAPI = FastAPI(title="DropMeFiles analog")
|
||||
# docs_url=None if disable_docs else "/docs",
|
||||
# redoc_url=None if disable_docs else "/redoc",
|
||||
# openapi_url=None if disable_docs else "/openapi.json")
|
||||
s3_worker = S3Worker()
|
||||
redis_worker = RedisWorker()
|
||||
|
||||
data = dict()
|
||||
|
||||
@app.post("/upload_file")
|
||||
async def create_upload_file(file: UploadFile = File(...)):
|
||||
random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=6))
|
||||
data[random_string] = file.filename
|
||||
return {"filename": file.filename, "content_type": file.content_type, "id": random_string}
|
||||
@app.get("/upload_token", status_code=200)
|
||||
async def get_upload_token(file_name: str, file_type: str, file_size: int, response: Response, request: Request) -> UploadToken | BaseResponse:
|
||||
if file_size > int(os.getenv('MAX_FILES_SIZE')):
|
||||
response.status_code = status.HTTP_413_CONTENT_TOO_LARGE
|
||||
return BaseResponse(result="The uploaded file is too large", error=True)
|
||||
elif file_size <= 0:
|
||||
response.status_code = status.HTTP_400_BAD_REQUEST
|
||||
return BaseResponse(result="The file you are uploading is less than 1 byte, WTF?", error=True)
|
||||
|
||||
@app.get("/{item_id}")
|
||||
async def read_item(item_id: str):
|
||||
if item_id in data.keys():
|
||||
return {"return": data[item_id]}
|
||||
else:
|
||||
return {"return": "NO DATA!"}
|
||||
user_ip = request.client.host
|
||||
file_uuid = ''.join(random.choices(string.ascii_letters + string.digits, k=6))
|
||||
async with s3_worker as worker:
|
||||
try:
|
||||
post_data = await worker.generate_upload_post(file_name, content_type=file_type)
|
||||
except Exception as exception:
|
||||
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
return BaseResponse(result="Error generating S3 access token. Error: " + str(exception), error=True)
|
||||
|
||||
redis_worker.create_record(user_ip, file_name, file_uuid, file_type, datetime.now().isoformat(), file_size)
|
||||
print(post_data)
|
||||
return UploadToken.model_validate(post_data)
|
||||
|
||||
async def main():
|
||||
config = uvicorn.Config("main:app", port=5000, host="0.0.0.0", log_level="debug")
|
||||
|
||||
Reference in New Issue
Block a user