Skip to content

Commit

Permalink
add image deletion api (#12)
Browse files Browse the repository at this point in the history
* add image deletion api

* psycho

---------

Co-authored-by: Josh Smith <[email protected]>
  • Loading branch information
FrostiDrinks and cmyui authored Oct 25, 2024
1 parent 47ddb6e commit 66b2aed
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
17 changes: 15 additions & 2 deletions app/adapters/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
async def upload(
body: bytes,
file_name: str,
folder: str,
directory: str,
content_type: str | None = None,
acl: str | None = None,
) -> None:
params: dict[str, Any] = {
"Bucket": settings.AWS_S3_BUCKET_NAME,
"Key": f"{folder}/{file_name}",
"Key": f"{directory}/{file_name}",
"Body": body,
}
if content_type is not None:
Expand Down Expand Up @@ -60,3 +60,16 @@ async def download(file_name: str, directory: str) -> DownloadResponse | None:
"body": body,
"content_type": content_type,
}


async def delete(file_name: str, directory: str) -> None:
try:
await app.clients.s3_client.delete_object(
Bucket=settings.AWS_S3_BUCKET_NAME,
Key=f"{directory}/{file_name}",
)
except app.clients.s3_client.exceptions.NoSuchKey:
return None
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as exc:
logging.error("Failed to download file from S3", exc_info=exc)
return None
18 changes: 18 additions & 0 deletions app/api/v1/avatars.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,21 @@ async def get_avatar(file_path: str):
content=download_response["body"],
media_type=download_response["content_type"],
)


@router.delete("/api/v1/users/{user_id}/avatar")
async def delete_avatar(
user_id: int,
authorization: AdminAuthorization = Depends(authorize_admin),
):
data = await app.usecases.images.delete_image(
image_type=ImageType.USER_AVATAR,
no_ext_file_name=f"{user_id}",
authorization=authorization,
)
if isinstance(data, Error):
return Response(
status_code=_get_status_code_for_error(data.code),
content=data.user_feedback,
)
return Response(status_code=201)
23 changes: 22 additions & 1 deletion app/usecases/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ async def upload_image(
await s3.upload(
body=image_content,
file_name=f"{no_ext_file_name}.{image_format.lower()}",
folder=image_type.get_s3_folder(),
directory=image_type.get_s3_folder(),
content_type=image_mime_type,
)
logging.info(
Expand All @@ -157,3 +157,24 @@ async def upload_image(
)

return None


async def delete_image(
image_type: ImageType,
no_ext_file_name: str,
authorization: AbstractAuthorization,
) -> None | Error:
for ext in ["png", "jpg", "jpeg", "gif", "webp"]:
await s3.delete(
file_name=f"{no_ext_file_name}.{ext}",
directory=image_type.get_s3_folder(),
)

logging.info(
"Deleted image",
extra={
"file_name": no_ext_file_name,
"image_type": image_type,
"authorization": authorization.format_for_logs(),
},
)

0 comments on commit 66b2aed

Please sign in to comment.