<gh_stars>1-10
import os
import shutil
import datetime
import uuid
from fastapi import UploadFile, File, HTTPException

class ImageUploader:
    __static: str
    __uploads: str

    def __init__(self):
        self.__static = os.getenv('STATIC_PATH')
        self.__uploads = os.getenv('UPLOADS_PATH')
        
    # Create a path
    def create_path(self, filename):
        # dir
        today = datetime.datetime.now().strftime('%Y%m%d')
        dir = self.__uploads + '/' + today + '/'
        if not os.path.exists(dir):
            os.makedirs(dir)
        # name
        ext = os.path.splitext(filename)[1]
        name = str(uuid.uuid4().hex) + str(ext)
        # path
        path = dir + name

        return path
    
    # Upload an image
    async def upload_image(self, image: UploadFile = File(...)):
        if image == None:
            raise HTTPException(status_code=400, detail='Bad Request')
        path = self.create_path(image.filename);   
        with open(path, 'wb') as buffer:
            shutil.copyfileobj(image.file, buffer)
        path = path.replace(self.__static, '/static')

        return path

import os
import shutil
import datetime
import uuid
from fastapi import UploadFile, File, HTTPException

class ImageUploader:
    __static: str
    __uploads: str

    def __init__(self):
        self.__static = os.getenv('STATIC_PATH')
        self.__uploads = os.getenv('UPLOADS_PATH')
        
    # Create a path
    def create_path(self, filename):
        # dir
        today = datetime.datetime.now().strftime('%Y%m%d')
        dir = self.__uploads + '/' + today + '/'
        if not os.path.exists(dir):
            os.makedirs(dir)
        # name
        ext = os.path.splitext(filename)[1]
        name = str(uuid.uuid4().hex) + str(ext)
        # path
        path = dir + name

        return path
    
    # Upload an image
    async def upload_image(self, image: UploadFile = File(...)):
        if image == None:
            raise HTTPException(status_code=400, detail='Bad Request')
        path = self.create_path(image.filename);   
        with open(path, 'wb') as buffer:
            shutil.copyfileobj(image.file, buffer)
        path = path.replace(self.__static, '/static')

        return path
# Create a path # dir # name # path # Upload an image