@ -24,17 +24,8 @@ images_collection = db["images"]
pins_collection = db [ " pins " ]
pin_permissions_collection = db [ " pin_permissions " ]
# Configuration
UPLOAD_DIR = os . path . join ( os . path . dirname ( os . path . dirname ( os . path . dirname ( __file__ ) ) ) , " images " )
MAX_IMAGE_SIZE = 8 * 1024 * 1024 # 8 Mo
ALLOWED_MIME_TYPES = [
" image/jpeg " ,
" image/png " ,
" image/heic "
]
# Créer le dossier images s'il n'existe pas
os . makedirs ( UPLOAD_DIR, exist_ok = True )
os . makedirs ( config . UPLOAD_DIR , exist_ok = True )
images_router = APIRouter (
prefix = " /image " ,
@ -116,23 +107,23 @@ async def add_image(
) :
# Vérifier la taille du fichier
image_data = await image . read ( )
if len ( image_data ) > MAX_IMAGE_SIZE:
if len ( image_data ) > config. MAX_IMAGE_SIZE:
raise HTTPException ( status_code = 413 , detail = " Image too large (max 8MB) " )
# Vérifier le type MIME
mime_type = magic . from_buffer ( image_data , mime = True )
if mime_type not in ALLOWED_MIME_TYPES:
if mime_type not in config. ALLOWED_MIME_TYPES:
raise HTTPException ( status_code = 415 , detail = " Unsupported image type " )
# Sauvegarder temporairement le fichier
temp_path = os . path . join ( UPLOAD_DIR, f " temp_ { image . filename } " )
temp_path = os . path . join ( config. UPLOAD_DIR, f " temp_ { image . filename } " )
with open ( temp_path , " wb " ) as f :
f . write ( image_data )
try :
# Traiter l'image et obtenir son hash
file_hash , extension = process_image ( temp_path )
final_path = os . path . join ( UPLOAD_DIR, f " { file_hash } . { extension } " )
final_path = os . path . join ( config. UPLOAD_DIR, f " { file_hash } . { extension } " )
# Si l'image n'existe pas déjà physiquement, la déplacer
if not os . path . exists ( final_path ) :
@ -169,7 +160,7 @@ async def add_image(
)
async def get_image ( id : str , current_user : User = Depends ( get_current_user ) ) :
image = check_image_permissions ( id , current_user )
image_path = os . path . join ( UPLOAD_DIR, f " { image [ ' image_hash ' ] } .jpg " )
image_path = os . path . join ( config. UPLOAD_DIR, f " { image [ ' image_hash ' ] } .jpg " )
if not os . path . exists ( image_path ) :
raise HTTPException ( status_code = 404 , detail = " Image file not found " )
@ -191,7 +182,7 @@ async def delete_image(id: str, current_user: User = Depends(get_current_user)):
# Si plus personne n'utilise l'image, supprimer le fichier
if not other_images :
image_path = os . path . join ( UPLOAD_DIR, f " { image [ ' image_hash ' ] } .jpg " )
image_path = os . path . join ( config. UPLOAD_DIR, f " { image [ ' image_hash ' ] } .jpg " )
if os . path . exists ( image_path ) :
os . remove ( image_path )
@ -206,7 +197,7 @@ async def update_caption(
caption_data : ImageUpdateCaptionDTO ,
current_user : User = Depends ( get_current_user )
) :
image = check_image_permissions( id , current_user )
check_image_permissions( id , current_user )
images_collection . update_one (
{ " _id " : ObjectId ( id ) } ,