hh
This commit is contained in:
@@ -9,6 +9,7 @@ from typing import Any, Dict, Iterable, List, Optional, Tuple
|
||||
from urllib.parse import quote
|
||||
|
||||
from API.requests_client import get_requests_session
|
||||
from SYS.utils import ffprobe as probe_media_metadata
|
||||
|
||||
from ProviderCore.base import Provider, SearchResult
|
||||
from SYS.provider_helpers import TableProviderMixin
|
||||
@@ -166,6 +167,71 @@ def _classify_matrix_upload(path: Path,
|
||||
return (mime_type or "application/octet-stream"), msgtype
|
||||
|
||||
|
||||
def _build_matrix_media_info(path: Path, *, mime_type: str, msgtype: str) -> Dict[str, Any]:
|
||||
"""Build Matrix `info` payload with dimensions/duration when available.
|
||||
|
||||
Matrix clients use `info.w`/`info.h` to size image/video placeholders. Supplying
|
||||
these fields keeps the preview aspect ratio aligned with the actual media.
|
||||
"""
|
||||
info: Dict[str, Any] = {
|
||||
"mimetype": str(mime_type or "application/octet-stream"),
|
||||
"size": int(path.stat().st_size),
|
||||
}
|
||||
|
||||
metadata: Dict[str, Any] = {}
|
||||
try:
|
||||
metadata = probe_media_metadata(str(path)) or {}
|
||||
except Exception:
|
||||
metadata = {}
|
||||
|
||||
width: Optional[int] = None
|
||||
height: Optional[int] = None
|
||||
|
||||
try:
|
||||
raw_w = metadata.get("width") if isinstance(metadata, dict) else None
|
||||
if isinstance(raw_w, (int, float)) and raw_w > 0:
|
||||
width = int(raw_w)
|
||||
except Exception:
|
||||
width = None
|
||||
|
||||
try:
|
||||
raw_h = metadata.get("height") if isinstance(metadata, dict) else None
|
||||
if isinstance(raw_h, (int, float)) and raw_h > 0:
|
||||
height = int(raw_h)
|
||||
except Exception:
|
||||
height = None
|
||||
|
||||
# Fallback for images when ffprobe metadata is unavailable.
|
||||
if msgtype == "m.image" and (width is None or height is None):
|
||||
try:
|
||||
from PIL import Image # type: ignore
|
||||
|
||||
with Image.open(path) as img:
|
||||
iw, ih = img.size
|
||||
if isinstance(iw, int) and iw > 0:
|
||||
width = iw
|
||||
if isinstance(ih, int) and ih > 0:
|
||||
height = ih
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if msgtype in {"m.image", "m.video"}:
|
||||
if width is not None:
|
||||
info["w"] = width
|
||||
if height is not None:
|
||||
info["h"] = height
|
||||
|
||||
if msgtype in {"m.video", "m.audio"}:
|
||||
try:
|
||||
raw_duration = metadata.get("duration") if isinstance(metadata, dict) else None
|
||||
if isinstance(raw_duration, (int, float)) and raw_duration > 0:
|
||||
info["duration"] = int(round(float(raw_duration) * 1000.0))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return info
|
||||
|
||||
|
||||
def _normalize_homeserver(value: str) -> str:
|
||||
text = str(value or "").strip()
|
||||
if not text:
|
||||
@@ -523,10 +589,7 @@ class Matrix(TableProviderMixin, Provider):
|
||||
except Exception:
|
||||
download_url_for_store = ""
|
||||
|
||||
info = {
|
||||
"mimetype": mime_type,
|
||||
"size": path.stat().st_size
|
||||
}
|
||||
info = _build_matrix_media_info(path, mime_type=mime_type, msgtype=msgtype)
|
||||
payload = {
|
||||
"msgtype": msgtype,
|
||||
"body": filename,
|
||||
|
||||
Reference in New Issue
Block a user