From 0c9c8b73ab2ab04c2cb76a314694cb9c37e707d6 Mon Sep 17 00:00:00 2001 From: ReRubis Date: Sun, 7 Jan 2024 15:53:59 +0000 Subject: [PATCH] Replace imghdr with filetype Signed-off-by: ReRubis --- asyncord/base64_image.py | 9 ++------- pdm.lock | 16 ++++++++++++---- pyproject.toml | 1 + 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/asyncord/base64_image.py b/asyncord/base64_image.py index 76337f8..4bc565e 100644 --- a/asyncord/base64_image.py +++ b/asyncord/base64_image.py @@ -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 @@ -53,7 +53,7 @@ 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') @@ -61,11 +61,6 @@ def build(cls, image_data: bytes | str, image_type: str | None = None) -> Self: 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. diff --git a/pdm.lock b/pdm.lock index 3540486..f9130e9 100644 --- a/pdm.lock +++ b/pdm.lock @@ -3,10 +3,9 @@ [metadata] groups = ["default", "dev", "linting", "testing"] -cross_platform = true -static_urls = false -lock_version = "4.3" -content_hash = "sha256:31fad7aab22633a76b6d4b2a6b65086d60431138e699dfb30a5fd3742b6677d6" +strategy = ["cross_platform"] +lock_version = "4.4" +content_hash = "sha256:3df2bcf645ae45e0179033d02b6d1d1920461c6e4901c3ccaa639093a9689673" [[package]] name = "aiohttp" @@ -124,6 +123,15 @@ files = [ {file = "filelock-3.13.1.tar.gz", hash = "sha256:521f5f56c50f8426f5e03ad3b281b490a87ef15bc6c526f168290f0c7148d44e"}, ] +[[package]] +name = "filetype" +version = "1.2.0" +summary = "Infer file type and MIME type of any file/buffer. No external dependencies." +files = [ + {file = "filetype-1.2.0-py2.py3-none-any.whl", hash = "sha256:7ce71b6880181241cf7ac8697a2f1eb6a8bd9b429f7ad6d27b8db9ba5f1c2d25"}, + {file = "filetype-1.2.0.tar.gz", hash = "sha256:66b56cd6474bf41d8c54660347d37afcc3f7d1970648de365c102ef77548aadb"}, +] + [[package]] name = "frozenlist" version = "1.4.1" diff --git a/pyproject.toml b/pyproject.toml index 4e1e9e3..344a7d7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,7 @@ dependencies = [ "rich>=12.5.1", "pydantic>=2.4.2", "aiohttp<4", + "filetype>=1.2.0", ]