Skip to content

Commit

Permalink
Move file_type prefix to media.py (#588)
Browse files Browse the repository at this point in the history
* Fixing header location for IDing webms

* Adding type_prefix to differentiate between image and video media types

* Removing 'image/' prefix from send_media file_type to support videos

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update steam/http.py

* Update steam/media.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: James Hilton-Balfe <[email protected]>
  • Loading branch information
3 people authored Sep 3, 2024
1 parent 0a9f6d8 commit 0237f9e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion steam/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ async def send_media(self, media: Media, **kwargs: int) -> None:
"file_sha": media.hash(contents),
"file_image_width": media.width,
"file_image_height": media.height,
"file_type": f"image/{media.type}",
"file_type": media.type,
}
resp = await self.post(URL.COMMUNITY / "chat/beginfileupload", data=payload)

Expand Down
8 changes: 6 additions & 2 deletions steam/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ def __init__(self, fp: MediaIO | StrOrBytesPath | int, *, spoiler: bool = False)
if check != 0x0D0A1A0A:
raise ValueError("Opened file's headers do not match a standard PNGs headers")
width, height = struct.unpack(">ii", headers[16:24])
type_prefix = "image"
case "gif":
width, height = struct.unpack("<HH", headers[6:10])
type_prefix = "image"
case "jpeg":
try:
self.fp.seek(self._tell) # read 0xff next
Expand All @@ -93,17 +95,19 @@ def __init__(self, fp: MediaIO | StrOrBytesPath | int, *, spoiler: bool = False)
# we are at a SOFn block
self.fp.seek(1, 1) # skip 'precision' byte.
height, width = struct.unpack(">HH", self.fp.read(4))
type_prefix = "image"
except Exception as exc:
raise ValueError from exc
case "webm" | "mp4" | "mpeg" | "ogv":
width, height = 0, 0
type_prefix = "video"
case _:
raise TypeError("Unsupported file format passed")
self.type = type
self.type = f"{type_prefix}/{type}"
self.spoiler = spoiler
self.width = width
self.height = height
self.name = f'{int(time())}_{getattr(self.fp, "name", f"media.{self.type}")}'
self.name = f'{int(time())}_{getattr(self.fp, "name", f"media.{type}")}'

def __enter__(self) -> Self:
return self
Expand Down

0 comments on commit 0237f9e

Please sign in to comment.