Skip to content

Commit

Permalink
fix: Media overrides provided name and mimetype
Browse files Browse the repository at this point in the history
  • Loading branch information
lgc2333 authored Jun 24, 2024
1 parent 8927389 commit 182a5bd
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/nonebot_plugin_alconna/uniseg/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,11 @@ class Media(Segment):
__default_name__ = "media"
to_url: ClassVar[Optional[MediaToUrl]] = None

def __is_default_name(self) -> bool:
return self.name == self.__default_name__

def __post_init__(self):
if self.path:
if self.path and self.__is_default_name():
self.name = Path(self.path).name
if self.url and not urlparse(self.url).hostname:
self.url = f"https://{self.url}"
Expand All @@ -391,12 +394,14 @@ def __post_init__(self):
def raw_bytes(self) -> bytes:
if not self.raw:
raise ValueError(f"{self} has no raw data")
raw = self.raw.getvalue() if isinstance(self.raw, BytesIO) else self.raw
header = raw[:128]
info = fleep.get(header)
self.mimetype = info.mimes[0] if info.mimes else self.mimetype
if info.types and info.extensions:
self.name = f"{info.types[0]}.{info.extensions[0]}"
if (not self.mimetype) and self.__is_default_name():
raw = self.raw.getvalue() if isinstance(self.raw, BytesIO) else self.raw
header = raw[:128]
info = fleep.get(header)
if not self.mimetype:
self.mimetype = info.mimes[0] if info.mimes else self.mimetype
if self.__is_default_name() and info.types and info.extensions:
self.name = f"{info.types[0]}.{info.extensions[0]}"
return raw


Expand Down

0 comments on commit 182a5bd

Please sign in to comment.