Skip to content

Commit

Permalink
Replace imghdr with filetype
Browse files Browse the repository at this point in the history
  • Loading branch information
ReRubis authored Jan 7, 2024
1 parent 4848f4a commit dc4476a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
9 changes: 2 additions & 7 deletions asyncord/base64_image.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Base64 encoded image."""

import base64
import imghdr
from collections.abc import Callable
from pathlib import Path
from typing import Annotated, Any, Self

import filetype
from pydantic import BaseModel
from pydantic_core import CoreSchema, core_schema

Expand Down Expand Up @@ -53,19 +53,14 @@ def build(cls, image_data: bytes | str, image_type: str | None = None) -> Self:

if isinstance(image_data, bytes):
if image_type is None:
image_type = imghdr.what(None, image_data)
image_type = filetype.guess(image_data)

if not image_type:
raise ValueError('Icon must be a valid image')

encoded_image = base64.b64encode(image_data).decode()
return cls(f'data:image/{image_type};base64, {encoded_image}')

if isinstance(image_data, cls):
return image_data

raise ValueError('Invalid value type')

@classmethod
def from_file(cls, file_path: str | Path) -> Self:
"""Build Base64Image from file path.
Expand Down
16 changes: 12 additions & 4 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies = [
"rich>=12.5.1",
"pydantic>=2.4.2",
"aiohttp<4",
"filetype>=1.2.0",
]


Expand Down

0 comments on commit dc4476a

Please sign in to comment.