Refactors Services and Initial Test Implementation
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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
|
||||
)
|
||||
0
storage_service/utils/file/__init__.py
Normal file
0
storage_service/utils/file/__init__.py
Normal 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()
|
||||
|
||||
1
storage_service/utils/file/validators/__init__.py
Normal file
1
storage_service/utils/file/validators/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .image_handler import image_validator
|
||||
@@ -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)
|
||||
@@ -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},
|
||||
}
|
||||
Reference in New Issue
Block a user