File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -48,7 +48,8 @@ async def upload_file(
4848 caption = caption ,
4949 url = upload_result .url ,
5050 file_type = "video" if file .content_type .startswith ("video/" ) else "image" ,
51- file_name = upload_result .name
51+ file_name = upload_result .name ,
52+ file_id = upload_result .file_id
5253
5354 )
5455 session .add (post )
@@ -115,6 +116,13 @@ async def delete_post(
115116 if post .user_id != user .id :
116117 raise HTTPException (status_code = 403 , detail = "You are not authorized to delete this post" )
117118
119+ try :
120+ imagekit .delete_file (file_id = post .file_id )
121+ except Exception as e :
122+ # We print the error but don't stop the function,
123+ # ensuring the post is deleted from DB even if ImageKit fails
124+ print (f"Error deleting from ImageKit: { e } " )
125+
118126 await session .delete (post )
119127 await session .commit ()
120128
Original file line number Diff line number Diff line change 88from sqlalchemy .orm import relationship
99from sqlalchemy import ForeignKey
1010from fastapi import Depends
11+ import os
1112
1213DATABASE_URL = os .getenv ("DATABASE_URL" , "sqlite+aiosqlite:///./test.db?timeout=10" )
1314
You can’t perform that action at this time.
0 commit comments