Refactors Services and Initial Test Implementation

This commit is contained in:
2024-05-21 03:35:24 -03:00
parent b93faf11b3
commit 7eb4d5b64d
21 changed files with 336 additions and 34 deletions

View File

@@ -1,6 +1,26 @@
from enum import Enum
from io import BytesIO
from typing import Callable
from storage_service.utils.file.validators import image_validator
class FileType(Enum):
PNG = "png"
JPEG = "jpeg"
def get_content_type(self) -> str:
match self:
case FileType.PNG:
return "image/png"
case FileType.JPEG:
return "image/jpeg"
case _:
raise ValueError("File Type Not Implemented")
def get_validator(self) -> Callable[[BytesIO], BytesIO]:
match self:
case FileType.PNG | FileType.JPEG:
return image_validator
case _:
raise ValueError("File Type Not Implemented")

View File

@@ -0,0 +1,8 @@
from fastapi import HTTPException, status
class FileNotFoundException(HTTPException):
def __init__(self, message: str):
super().__init__(
status.HTTP_400_BAD_REQUEST, detail=message
)

View File

View File

@@ -2,7 +2,7 @@ import base64
from hashlib import md5
def file_name_hash(file_key: str, file_postfix: str) -> str:
def generate_file_hash(file_key: str, file_postfix: str) -> str:
hashed_file_key = md5(file_key.encode("utf-8")).digest()
hashed_file_key = base64.b64encode(hashed_file_key).decode()

View File

@@ -0,0 +1 @@
from .image_handler import image_validator

View File

@@ -3,10 +3,10 @@ from PIL import Image
import io
def image_handler(file_bytes: io.BytesIO) -> io.BytesIO:
def image_validator(file_bytes: io.BytesIO) -> io.BytesIO:
img = Image.open(file_bytes)
img.thumbnail((320, 320))
img.thumbnail((180, 180))
data = list(img.getdata())
image_without_exif = Image.new(img.mode, img.size)

View File

@@ -1,9 +0,0 @@
from storage_service.utils.enums.file_type import FileType
from storage_service.utils.file_handler.handlers.image_handler import (
image_handler,
)
FILE_HANDLER = {
FileType.PNG: {"content_type": "image/png", "handler": image_handler},
FileType.JPEG: {"content_type": "image/jpeg", "handler": image_handler},
}