MCPcopy Create free account
hub / github.com/AstrBotDevs/AstrBot / _decode_base64_payload

Function _decode_base64_payload

astrbot/core/utils/media_utils.py:272–300  ·  view source on GitHub ↗

Decode a base64 payload while tolerating omitted padding. Args: payload: Base64 payload without a data URI header. error_message: Message to use when decoding fails. validate: Whether to ask ``base64.b64decode`` to reject non-base64 characters. Returns:

(
    payload: str,
    *,
    error_message: str,
    validate: bool = False,
)

Source from the content-addressed store, hash-verified

270
271
272def _decode_base64_payload(
273 payload: str,
274 *,
275 error_message: str,
276 validate: bool = False,
277) -> bytes:
278 """Decode a base64 payload while tolerating omitted padding.
279
280 Args:
281 payload: Base64 payload without a data URI header.
282 error_message: Message to use when decoding fails.
283 validate: Whether to ask ``base64.b64decode`` to reject non-base64
284 characters.
285
286 Returns:
287 Decoded bytes.
288
289 Raises:
290 ValueError: Raised when the payload cannot be decoded.
291 """
292 payload = "".join(payload.split())
293 missing_padding = len(payload) % 4
294 if missing_padding:
295 payload += "=" * (4 - missing_padding)
296
297 try:
298 return base64.b64decode(payload, validate=validate)
299 except (binascii.Error, ValueError) as exc:
300 raise ValueError(error_message) from exc
301
302
303def describe_media_ref(media_ref: object | None) -> str:

Callers 6

to_bytesMethod · 0.85
_parse_base64_data_uriFunction · 0.85
describe_media_refFunction · 0.85
_materialize_media_refFunction · 0.85
to_base64_dataMethod · 0.85
compress_imageFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected