|
6 | 6 | from __future__ import annotations |
7 | 7 |
|
8 | 8 | # %% auto 0 |
9 | | -__all__ = ['spark_chars', 'UNSET', 'walk', 'globtastic', 'maybe_open', 'mkdir', 'image_size', 'img_bytes', 'bunzip', 'loads', |
10 | | - 'loads_multi', 'dumps', 'untar_dir', 'repo_details', 'shell', 'ssh', 'rsync_multi', 'run', 'open_file', |
11 | | - 'save_pickle', 'load_pickle', 'parse_env', 'expand_wildcards', 'dict2obj', 'obj2dict', 'repr_dict', |
12 | | - 'is_listy', 'mapped', 'IterLen', 'ReindexCollection', 'SaveReturn', 'trim_wraps', 'save_iter', 'asave_iter', |
13 | | - 'friendly_name', 'n_friendly_names', 'exec_eval', 'get_source_link', 'truncstr', 'sparkline', |
| 9 | +__all__ = ['spark_chars', 'UNSET', 'walk', 'globtastic', 'maybe_open', 'mkdir', 'image_size', 'img_bytes', 'detect_mime', |
| 10 | + 'bunzip', 'loads', 'loads_multi', 'dumps', 'untar_dir', 'repo_details', 'shell', 'ssh', 'rsync_multi', 'run', |
| 11 | + 'open_file', 'save_pickle', 'load_pickle', 'parse_env', 'expand_wildcards', 'dict2obj', 'obj2dict', |
| 12 | + 'repr_dict', 'is_listy', 'mapped', 'IterLen', 'ReindexCollection', 'SaveReturn', 'trim_wraps', 'save_iter', |
| 13 | + 'asave_iter', 'friendly_name', 'n_friendly_names', 'exec_eval', 'get_source_link', 'truncstr', 'sparkline', |
14 | 14 | 'modify_exception', 'round_multiple', 'set_num_threads', 'join_path_file', 'autostart', 'EventTimer', |
15 | 15 | 'stringfmt_names', 'PartialFormatter', 'partial_format', 'utc2local', 'local2utc', 'trace', 'modified_env', |
16 | 16 | 'ContextManagers', 'shufflish', 'console_help', 'hl_md', 'type2str', 'dataclass_src', 'Unset', 'nullable_dc', |
@@ -134,6 +134,30 @@ def img_bytes(img, fmt='PNG'): |
134 | 134 | img.save(buf, format=fmt) |
135 | 135 | return buf.getvalue() |
136 | 136 |
|
| 137 | +# %% ../nbs/03_xtras.ipynb |
| 138 | +_sigs = { |
| 139 | + (b'%PDF', 0): 'application/pdf', |
| 140 | + (b'RIFF', 0): lambda d: 'audio/wav' if d[8:12]==b'WAVE' else 'video/avi' if d[8:12]==b'AVI ' else None, |
| 141 | + (b'ID3', 0): 'audio/mp3', |
| 142 | + (b'\xff\xfb', 0): 'audio/mp3', |
| 143 | + (b'\xff\xf3', 0): 'audio/mp3', |
| 144 | + (b'FORM', 0): lambda d: 'audio/aiff' if d[8:12]==b'AIFF' else None, |
| 145 | + (b'OggS', 0): 'audio/ogg', |
| 146 | + (b'fLaC', 0): 'audio/flac', |
| 147 | + (b'ftyp', 4): lambda d: 'video/3gpp' if d[8:11]==b'3gp' else 'video/mp4', |
| 148 | + (b'\x1a\x45\xdf', 0): 'video/webm', |
| 149 | + (b'FLV', 0): 'video/x-flv', |
| 150 | + (b'\x30\x26\xb2\x75', 0): 'video/wmv', |
| 151 | + (b'\x00\x00\x01\xb3', 0): 'video/mpeg', |
| 152 | +} |
| 153 | + |
| 154 | +def detect_mime(data): |
| 155 | + "Get the MIME type for bytes `data`, covering common PDF, audio, video, and image types" |
| 156 | + import mimetypes |
| 157 | + for (sig,pos),mime in _sigs.items(): |
| 158 | + if data[pos:pos+len(sig)]==sig: return mime(data) if callable(mime) else mime |
| 159 | + return mimetypes.types_map.get(f'.{imghdr.what(None, h=data)}') |
| 160 | + |
137 | 161 | # %% ../nbs/03_xtras.ipynb |
138 | 162 | def bunzip(fn): |
139 | 163 | "bunzip `fn`, raising exception if output already exists" |
|
0 commit comments