Refactors Project for More Declarative Package Name
This commit is contained in:
0
storage_service/utils/__init__.py
Normal file
0
storage_service/utils/__init__.py
Normal file
0
storage_service/utils/enums/__init__.py
Normal file
0
storage_service/utils/enums/__init__.py
Normal file
6
storage_service/utils/enums/file_type.py
Normal file
6
storage_service/utils/enums/file_type.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class FileType(Enum):
|
||||
PNG = "png"
|
||||
JPEG = "jpeg"
|
||||
5
storage_service/utils/enums/storage_type.py
Normal file
5
storage_service/utils/enums/storage_type.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class StorageType(Enum):
|
||||
S3_STORAGE = "s3"
|
||||
9
storage_service/utils/file_handler/__init__.py
Normal file
9
storage_service/utils/file_handler/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
||||
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},
|
||||
}
|
||||
20
storage_service/utils/file_handler/handlers/image_handler.py
Normal file
20
storage_service/utils/file_handler/handlers/image_handler.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from PIL import Image
|
||||
|
||||
import io
|
||||
|
||||
|
||||
def image_handler(file_bytes: io.BytesIO) -> io.BytesIO:
|
||||
img = Image.open(file_bytes)
|
||||
|
||||
img.thumbnail((320, 320))
|
||||
|
||||
data = list(img.getdata())
|
||||
image_without_exif = Image.new(img.mode, img.size)
|
||||
image_without_exif.putdata(data)
|
||||
|
||||
new_byte_img = io.BytesIO()
|
||||
img.save(new_byte_img, format="PNG")
|
||||
|
||||
new_byte_img.seek(0)
|
||||
|
||||
return new_byte_img
|
||||
9
storage_service/utils/file_name_hash.py
Normal file
9
storage_service/utils/file_name_hash.py
Normal file
@@ -0,0 +1,9 @@
|
||||
import base64
|
||||
from hashlib import md5
|
||||
|
||||
|
||||
def file_name_hash(username: str, file_postfix: str) -> str:
|
||||
hashed_username = md5(username.encode("utf-8")).digest()
|
||||
hashed_username = base64.b64encode(hashed_username).decode()
|
||||
|
||||
return f"{hashed_username}_{file_postfix}"
|
||||
Reference in New Issue
Block a user