Spaces:
Sleeping
Sleeping
Download app/services/attachments.py from adrija93/TDS-Project1-API: direct link, hf CLI and curl.
- Browser
- Download file 536 Bytes
-
https://huggingface.co/spaces/adrija93/TDS-Project1-API/resolve/main/app/services/attachments.py
- Command line
-
hf download hf://spaces/adrija93/TDS-Project1-API/app/services/attachments.py
-
curl -L -o attachments.py https://huggingface.co/spaces/adrija93/TDS-Project1-API/resolve/main/app/services/attachments.py
536 Bytes
| import base64 | |
| from typing import Tuple | |
| # Parse data: URIs like data:text/plain;base64,SGVsbG8= | |
| def is_data_uri(url: str) -> bool: | |
| return url.startswith("data:") | |
| def decode_data_uri(url: str) -> Tuple[str, bytes]: | |
| # returns (mime, bytes) | |
| assert url.startswith("data:"), "Not a data URI" | |
| header, b64 = url.split(",", 1) | |
| mime = header[5:].split(";")[0] or "application/octet-stream" | |
| if ";base64" in header: | |
| data = base64.b64decode(b64) | |
| else: | |
| data = b64.encode("utf-8") | |
| return mime, data | |