(string, content_type="application/octet-stream")
| 21 | |
| 22 | |
| 23 | def json_safe(string, content_type="application/octet-stream") -> str: |
| 24 | try: |
| 25 | string = string.decode("utf-8") |
| 26 | json.dumps(string) |
| 27 | return string |
| 28 | except (ValueError, TypeError): |
| 29 | return b"".join( |
| 30 | [ |
| 31 | b"data:", |
| 32 | content_type.encode("utf-8"), |
| 33 | b";base64,", |
| 34 | base64.b64encode(string), |
| 35 | ] |
| 36 | ).decode("utf-8") |
| 37 | |
| 38 | |
| 39 | def flattern(d: "MultiDict[K, V]") -> dict[K, V | list[V]]: |