52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
from base64 import b64decode
|
|
|
|
from fastapi import FastAPI, status
|
|
|
|
from exceptions import InternalServerErrorException
|
|
from image_service import SUPPORTED_FORMATS, process_image, upload_images
|
|
from schemas import SUploadedImages, SFormats, SAvailableFormats, SInternalServerError
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.post(
|
|
"/process_images",
|
|
status_code=status.HTTP_200_OK,
|
|
response_model=SUploadedImages,
|
|
responses={
|
|
status.HTTP_500_INTERNAL_SERVER_ERROR: {
|
|
"model": SInternalServerError,
|
|
"description": "Internal Server Error",
|
|
},
|
|
},
|
|
)
|
|
async def process_images(upload_data: SFormats):
|
|
try:
|
|
all_urls = []
|
|
for image in upload_data.images:
|
|
imgs = process_image(b64decode(image.image), upload_data.formats)
|
|
urls = await upload_images(imgs, image.name, str(upload_data.upload_url), upload_data.bucket_name)
|
|
all_urls.append(urls)
|
|
|
|
return {"images_urls": all_urls}
|
|
except Exception as e:
|
|
print(e)
|
|
raise InternalServerErrorException
|
|
|
|
|
|
|
|
@app.get(
|
|
"/available_formats",
|
|
status_code=status.HTTP_200_OK,
|
|
response_model=SAvailableFormats,
|
|
responses={
|
|
status.HTTP_500_INTERNAL_SERVER_ERROR: {
|
|
"model": SInternalServerError,
|
|
"description": "Internal Server Error",
|
|
},
|
|
},
|
|
)
|
|
async def get_available_formats():
|
|
return {"available_formats": SUPPORTED_FORMATS}
|
|
|
|
|