Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import typing as t |
| 2 | from pathlib import Path |
| 3 | |
| 4 | _default = "application/octet-stream" |
| 5 | # Extension (lowercase, with leading dot) -> MIME type. Mirrors |
| 6 | # `MIME_TYPES_BY_EXTENSION` in the TypeScript SDK |
| 7 | # (`ts/packages/core/src/utils/mime.ts`), whose values come from the `mime` npm |
| 8 | # package; keep the two identical. |
| 9 | _types = { |
| 10 | ".3g2": "video/3gpp2", |
| 11 | ".3gp": "video/3gpp", |
| 12 | ".3gpp": "video/3gpp", |
| 13 | ".7z": "application/x-7z-compressed", |
| 14 | ".aab": "application/x-authorware-bin", |
| 15 | ".aac": "audio/aac", |
| 16 | ".aam": "application/x-authorware-map", |
| 17 | ".aas": "application/x-authorware-seg", |
| 18 | ".adts": "audio/aac", |
| 19 | ".ai": "application/postscript", |
| 20 | ".aif": "audio/x-aiff", |
| 21 | ".aifc": "audio/x-aiff", |
| 22 | ".aiff": "audio/x-aiff", |
| 23 | ".arc": "application/x-freearc", |
| 24 | ".arj": "application/x-arj", |
| 25 | ".asf": "video/x-ms-asf", |
| 26 | ".asm": "text/x-asm", |
| 27 | ".asx": "video/x-ms-asf", |
| 28 | ".au": "audio/basic", |
| 29 | ".avi": "video/x-msvideo", |
| 30 | ".avif": "image/avif", |
| 31 | ".bat": "application/x-msdownload", |
| 32 | ".bcpio": "application/x-bcpio", |
| 33 | ".bmp": "image/bmp", |
| 34 | ".book": "application/vnd.framemaker", |
| 35 | ".boz": "application/x-bzip2", |
| 36 | ".bz": "application/x-bzip", |
| 37 | ".bz2": "application/x-bzip2", |
| 38 | ".c": "text/x-c", |
| 39 | ".cat": "application/vnd.ms-pki.seccat", |
| 40 | ".cc": "text/x-c", |
| 41 | ".cco": "application/x-cocoa", |
| 42 | ".cdf": "application/x-netcdf", |
| 43 | ".cer": "application/pkix-cert", |
| 44 | ".chat": "application/x-chat", |
| 45 | ".class": "application/java-vm", |
| 46 | ".com": "application/x-msdownload", |
| 47 | ".conf": "text/plain", |
| 48 | ".cpio": "application/x-cpio", |
| 49 | ".cpp": "text/x-c", |
| 50 | ".cpt": "application/mac-compactpro", |
| 51 | ".crl": "application/pkix-crl", |
| 52 | ".crt": "application/x-x509-ca-cert", |
| 53 | ".csh": "application/x-csh", |
| 54 | ".css": "text/css", |
| 55 | ".csv": "text/csv", |
| 56 | ".cxx": "text/x-c", |
| 57 | ".dcr": "application/x-director", |
| 58 | ".def": "text/plain", |
| 59 | ".der": "application/x-x509-ca-cert", |
| 60 | ".dir": "application/x-director", |
| 61 | ".doc": "application/msword", |
| 62 | ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| 63 | ".dot": "application/msword", |
| 64 | ".dp": "application/vnd.osgi.dp", |
| 65 | ".dvi": "application/x-dvi", |
| 66 | ".dwf": "model/vnd.dwf", |
| 67 | ".dwg": "image/vnd.dwg", |
| 68 | ".dxf": "image/vnd.dxf", |
| 69 | ".dxr": "application/x-director", |
| 70 | ".eml": "message/rfc822", |
| 71 | ".eot": "application/vnd.ms-fontobject", |
| 72 | ".eps": "application/postscript", |
| 73 | ".epub": "application/epub+zip", |
| 74 | ".etx": "text/x-setext", |
| 75 | ".evy": "application/x-envoy", |
| 76 | ".f": "text/x-fortran", |
| 77 | ".f77": "text/x-fortran", |
| 78 | ".f90": "text/x-fortran", |
| 79 | ".fdf": "application/fdf", |
| 80 | ".flac": "audio/x-flac", |
| 81 | ".fli": "video/x-fli", |
| 82 | ".flo": "application/vnd.micrografx.flo", |
| 83 | ".flx": "text/vnd.fmi.flexstor", |
| 84 | ".for": "text/x-fortran", |
| 85 | ".fpx": "image/vnd.fpx", |
| 86 | ".g3": "image/g3fax", |
| 87 | ".gif": "image/gif", |
| 88 | ".gtar": "application/x-gtar", |
| 89 | ".gz": "application/gzip", |
| 90 | ".h": "text/x-c", |
| 91 | ".hdf": "application/x-hdf", |
| 92 | ".heic": "image/heic", |
| 93 | ".heif": "image/heif", |
| 94 | ".hh": "text/x-c", |
| 95 | ".hlp": "application/winhlp", |
| 96 | ".hpgl": "application/vnd.hp-hpgl", |
| 97 | ".hqx": "application/mac-binhex40", |
| 98 | ".htc": "text/x-component", |
| 99 | ".htm": "text/html", |
| 100 | ".html": "text/html", |
| 101 | ".ice": "x-conference/x-cooltalk", |
| 102 | ".ico": "image/vnd.microsoft.icon", |
| 103 | ".ics": "text/calendar", |
| 104 | ".ief": "image/ief", |
| 105 | ".iges": "model/iges", |
| 106 | ".igs": "model/iges", |
| 107 | ".jam": "application/vnd.jam", |
| 108 | ".java": "text/x-java-source", |
| 109 | ".jfif": "image/pjpeg", |
| 110 | ".jpe": "image/jpeg", |
| 111 | ".jpeg": "image/jpeg", |
| 112 | ".jpg": "image/jpeg", |
| 113 | ".js": "text/javascript", |
| 114 | ".json": "application/json", |
| 115 | ".kar": "audio/midi", |
| 116 | ".key": "application/vnd.apple.keynote", |
| 117 | ".latex": "application/x-latex", |
| 118 | ".lha": "application/x-lzh-compressed", |
| 119 | ".list": "text/plain", |
| 120 | ".log": "text/plain", |
| 121 | ".lzh": "application/x-lzh-compressed", |
| 122 | ".m1v": "video/mpeg", |
| 123 | ".m2a": "audio/mpeg", |
| 124 | ".m2v": "video/mpeg", |
| 125 | ".m3u": "audio/x-mpegurl", |
| 126 | ".m3u8": "application/vnd.apple.mpegurl", |
| 127 | ".m4a": "audio/mp4", |
| 128 | ".man": "text/troff", |
| 129 | ".map": "application/json", |
| 130 | ".mcd": "application/vnd.mcd", |
| 131 | ".md": "text/markdown", |
| 132 | ".me": "text/troff", |
| 133 | ".mht": "message/rfc822", |
| 134 | ".mhtml": "message/rfc822", |
| 135 | ".mid": "audio/midi", |
| 136 | ".midi": "audio/midi", |
| 137 | ".mif": "application/vnd.mif", |
| 138 | ".mime": "message/rfc822", |
| 139 | ".mjs": "text/javascript", |
| 140 | ".mka": "audio/x-matroska", |
| 141 | ".mkv": "video/x-matroska", |
| 142 | ".mov": "video/quicktime", |
| 143 | ".movie": "video/x-sgi-movie", |
| 144 | ".mp2": "audio/mpeg", |
| 145 | ".mp3": "audio/mpeg", |
| 146 | ".mp4": "video/mp4", |
| 147 | ".mpc": "application/vnd.mophun.certificate", |
| 148 | ".mpe": "video/mpeg", |
| 149 | ".mpeg": "video/mpeg", |
| 150 | ".mpg": "video/mpeg", |
| 151 | ".mpga": "audio/mpeg", |
| 152 | ".mpp": "application/dash-patch+xml", |
| 153 | ".mpt": "application/vnd.ms-project", |
| 154 | ".mrc": "application/marc", |
| 155 | ".ms": "text/troff", |
| 156 | ".nc": "application/x-netcdf", |
| 157 | ".nsc": "application/x-conference", |
| 158 | ".numbers": "application/vnd.apple.numbers", |
| 159 | ".obj": "model/obj", |
| 160 | ".oda": "application/oda", |
| 161 | ".odg": "application/vnd.oasis.opendocument.graphics", |
| 162 | ".odp": "application/vnd.oasis.opendocument.presentation", |
| 163 | ".ods": "application/vnd.oasis.opendocument.spreadsheet", |
| 164 | ".odt": "application/vnd.oasis.opendocument.text", |
| 165 | ".ogg": "audio/ogg", |
| 166 | ".opus": "audio/ogg", |
| 167 | ".otf": "font/otf", |
| 168 | ".p": "text/x-pascal", |
| 169 | ".p10": "application/pkcs10", |
| 170 | ".p12": "application/x-pkcs12", |
| 171 | ".p7c": "application/pkcs7-mime", |
| 172 | ".p7m": "application/pkcs7-mime", |
| 173 | ".p7r": "application/x-pkcs7-certreqresp", |
| 174 | ".p7s": "application/pkcs7-signature", |
| 175 | ".pages": "application/vnd.apple.pages", |
| 176 | ".pas": "text/x-pascal", |
| 177 | ".pbm": "image/x-portable-bitmap", |
| 178 | ".pcl": "application/vnd.hp-pcl", |
| 179 | ".pct": "image/x-pict", |
| 180 | ".pcx": "image/vnd.zbrush.pcx", |
| 181 | ".pdb": "application/vnd.palm", |
| 182 | ".pdf": "application/pdf", |
| 183 | ".pfx": "application/x-pkcs12", |
| 184 | ".pgm": "image/x-portable-graymap", |
| 185 | ".pic": "image/x-pict", |
| 186 | ".pl": "application/x-perl", |
| 187 | ".pm": "application/x-perl", |
| 188 | ".png": "image/png", |
| 189 | ".pnm": "image/x-portable-anymap", |
| 190 | ".pot": "application/vnd.ms-powerpoint", |
| 191 | ".ppm": "image/x-portable-pixmap", |
| 192 | ".pps": "application/vnd.ms-powerpoint", |
| 193 | ".ppt": "application/vnd.ms-powerpoint", |
| 194 | ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", |
| 195 | ".pre": "application/vnd.lotus-freelance", |
| 196 | ".ps": "application/postscript", |
| 197 | ".psd": "image/vnd.adobe.photoshop", |
| 198 | ".pyo": "model/vnd.pytha.pyox", |
| 199 | ".qt": "video/quicktime", |
| 200 | ".ra": "audio/x-pn-realaudio", |
| 201 | ".ram": "audio/x-pn-realaudio", |
| 202 | ".rar": "application/vnd.rar", |
| 203 | ".ras": "image/x-cmu-raster", |
| 204 | ".rdf": "application/rdf+xml", |
| 205 | ".rgb": "image/x-rgb", |
| 206 | ".rm": "application/vnd.rn-realmedia", |
| 207 | ".rmi": "audio/midi", |
| 208 | ".rmp": "audio/x-pn-realaudio-plugin", |
| 209 | ".rng": "application/xml", |
| 210 | ".roff": "text/troff", |
| 211 | ".rpm": "application/x-redhat-package-manager", |
| 212 | ".rtf": "application/rtf", |
| 213 | ".rtx": "text/richtext", |
| 214 | ".s": "text/x-asm", |
| 215 | ".s3m": "audio/s3m", |
| 216 | ".scm": "application/vnd.lotus-screencam", |
| 217 | ".sdp": "application/sdp", |
| 218 | ".sea": "application/x-sea", |
| 219 | ".sgm": "text/sgml", |
| 220 | ".sgml": "text/sgml", |
| 221 | ".sh": "application/x-sh", |
| 222 | ".shar": "application/x-shar", |
| 223 | ".shtml": "text/html", |
| 224 | ".sid": "image/x-mrsid-image", |
| 225 | ".sit": "application/x-stuffit", |
| 226 | ".skd": "application/vnd.koan", |
| 227 | ".skm": "application/vnd.koan", |
| 228 | ".skp": "application/vnd.koan", |
| 229 | ".skt": "application/vnd.koan", |
| 230 | ".smi": "application/smil+xml", |
| 231 | ".smil": "application/smil+xml", |
| 232 | ".snd": "audio/basic", |
| 233 | ".spc": "application/x-pkcs7-certificates", |
| 234 | ".spl": "application/x-futuresplash", |
| 235 | ".src": "application/x-wais-source", |
| 236 | ".step": "model/step", |
| 237 | ".stl": "model/stl", |
| 238 | ".stp": "model/step", |
| 239 | ".sv4cpio": "application/x-sv4cpio", |
| 240 | ".sv4crc": "application/x-sv4crc", |
| 241 | ".svg": "image/svg+xml", |
| 242 | ".swf": "application/x-shockwave-flash", |
| 243 | ".t": "text/troff", |
| 244 | ".tar": "application/x-tar", |
| 245 | ".tcl": "application/x-tcl", |
| 246 | ".tex": "application/x-tex", |
| 247 | ".texi": "application/x-texinfo", |
| 248 | ".texinfo": "application/x-texinfo", |
| 249 | ".text": "text/plain", |
| 250 | ".tif": "image/tiff", |
| 251 | ".tiff": "image/tiff", |
| 252 | ".tr": "text/troff", |
| 253 | ".ts": "video/mp2t", |
| 254 | ".tsv": "text/tab-separated-values", |
| 255 | ".txt": "text/plain", |
| 256 | ".uri": "text/uri-list", |
| 257 | ".uris": "text/uri-list", |
| 258 | ".ustar": "application/x-ustar", |
| 259 | ".uu": "text/x-uuencode", |
| 260 | ".vcd": "application/x-cdlink", |
| 261 | ".vcf": "text/x-vcard", |
| 262 | ".vcs": "text/x-vcalendar", |
| 263 | ".viv": "video/vnd.vivo", |
| 264 | ".vox": "application/x-authorware-bin", |
| 265 | ".vrml": "model/vrml", |
| 266 | ".vsd": "application/vnd.visio", |
| 267 | ".vst": "application/vnd.visio", |
| 268 | ".vsw": "application/vnd.visio", |
| 269 | ".wasm": "application/wasm", |
| 270 | ".wav": "audio/wav", |
| 271 | ".wbmp": "image/vnd.wap.wbmp", |
| 272 | ".webm": "video/webm", |
| 273 | ".webmanifest": "application/manifest+json", |
| 274 | ".webp": "image/webp", |
| 275 | ".wmf": "image/wmf", |
| 276 | ".wml": "text/vnd.wap.wml", |
| 277 | ".wmlc": "application/vnd.wap.wmlc", |
| 278 | ".wmls": "text/vnd.wap.wmlscript", |
| 279 | ".wmlsc": "application/vnd.wap.wmlscriptc", |
| 280 | ".woff": "font/woff", |
| 281 | ".woff2": "font/woff2", |
| 282 | ".wpd": "application/vnd.wordperfect", |
| 283 | ".wri": "application/x-mswrite", |
| 284 | ".wrl": "model/vrml", |
| 285 | ".wsc": "message/vnd.wfa.wsc", |
| 286 | ".wsdl": "application/wsdl+xml", |
| 287 | ".xbm": "image/x-xbitmap", |
| 288 | ".xif": "image/vnd.xiff", |
| 289 | ".xla": "application/vnd.ms-excel", |
| 290 | ".xlc": "application/vnd.ms-excel", |
| 291 | ".xlm": "application/vnd.ms-excel", |
| 292 | ".xls": "application/vnd.ms-excel", |
| 293 | ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
| 294 | ".xlt": "application/vnd.ms-excel", |
| 295 | ".xlw": "application/vnd.ms-excel", |
| 296 | ".xm": "audio/xm", |
| 297 | ".xml": "application/xml", |
| 298 | ".xpm": "image/x-xpixmap", |
| 299 | ".xsl": "application/xml", |
| 300 | ".xul": "application/vnd.mozilla.xul+xml", |
| 301 | ".xwd": "image/x-xwindowdump", |
| 302 | ".xyz": "chemical/x-xyz", |
| 303 | ".yaml": "text/yaml", |
| 304 | ".yml": "text/yaml", |
| 305 | ".zip": "application/zip", |
| 306 | # Not known to the `mime` npm package; values from Python's standard |
| 307 | # `mimetypes`, except `.tgz`. |
| 308 | ".3gpp2": "audio/3gpp2", |
| 309 | ".ass": "audio/aac", |
| 310 | ".h5": "application/x-hdf5", |
| 311 | ".ksh": "text/plain", |
| 312 | ".loas": "audio/aac", |
| 313 | ".mpa": "video/mpeg", |
| 314 | ".nws": "message/rfc822", |
| 315 | ".ppa": "application/vnd.ms-powerpoint", |
| 316 | ".pwz": "application/vnd.ms-powerpoint", |
| 317 | ".py": "text/x-python", |
| 318 | ".pyc": "application/x-python-code", |
| 319 | ".tgz": "application/gzip", |
| 320 | ".wiz": "application/msword", |
| 321 | ".xlb": "application/vnd.ms-excel", |
| 322 | ".xpdl": "application/xml", |
| 323 | } |
| 324 | |
| 325 | |
| 326 | def guess(file: t.Union[str, Path]) -> str: |
| 327 | return _types.get(Path(file).suffix.lower(), _default) |
| 328 | |
| 329 | |
| 330 | # MIME type -> extension (no leading dot). Used when deriving filenames from |
| 331 | # content-type headers (e.g. for URLs without path segments). |
| 332 | _MIME_TO_EXT: t.Dict[str, str] = { |
| 333 | "text/plain": "txt", |
| 334 | "text/html": "html", |
| 335 | "text/css": "css", |
| 336 | "text/csv": "csv", |
| 337 | "text/javascript": "js", |
| 338 | "application/json": "json", |
| 339 | "application/xml": "xml", |
| 340 | "application/pdf": "pdf", |
| 341 | "application/zip": "zip", |
| 342 | "application/x-zip-compressed": "zip", |
| 343 | "application/gzip": "gz", |
| 344 | "application/octet-stream": "bin", |
| 345 | "application/x-tar": "tar", |
| 346 | "application/msword": "doc", |
| 347 | "application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx", |
| 348 | "application/vnd.ms-excel": "xls", |
| 349 | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx", |
| 350 | "application/vnd.ms-powerpoint": "ppt", |
| 351 | "application/vnd.openxmlformats-officedocument.presentationml.presentation": "pptx", |
| 352 | "image/jpeg": "jpg", |
| 353 | "image/jpg": "jpg", |
| 354 | "image/png": "png", |
| 355 | "image/gif": "gif", |
| 356 | "image/svg+xml": "svg", |
| 357 | "image/webp": "webp", |
| 358 | "image/bmp": "bmp", |
| 359 | "image/tiff": "tiff", |
| 360 | "audio/mpeg": "mp3", |
| 361 | "audio/wav": "wav", |
| 362 | "audio/ogg": "ogg", |
| 363 | "video/mp4": "mp4", |
| 364 | "video/mpeg": "mpeg", |
| 365 | "video/quicktime": "mov", |
| 366 | "video/x-msvideo": "avi", |
| 367 | "video/webm": "webm", |
| 368 | } |
| 369 | |
| 370 | |
| 371 | def get_extension_from_mime_type(mime_type: str) -> str: |
| 372 | """Map MIME type to file extension (no leading dot). |
| 373 | |
| 374 | Used when deriving filenames from content-type headers |
| 375 | (e.g. for URLs without path segments). |
| 376 | """ |
| 377 | clean = mime_type.split(";")[0].lower().strip() |
| 378 | if clean in _MIME_TO_EXT: |
| 379 | return _MIME_TO_EXT[clean] |
| 380 | parts = clean.split("/") |
| 381 | if len(parts) == 2: |
| 382 | subtype = parts[1].lower() |
| 383 | if "+" in subtype: |
| 384 | plus_parts = subtype.split("+") |
| 385 | suffix = plus_parts[-1] |
| 386 | known_prefixes = ("svg", "atom", "rss") |
| 387 | if plus_parts[0] in known_prefixes: |
| 388 | return plus_parts[0] |
| 389 | structured_suffixes = ("json", "xml", "yaml", "zip", "gzip") |
| 390 | if suffix in structured_suffixes: |
| 391 | return suffix |
| 392 | return suffix |
| 393 | return subtype or "txt" |
| 394 | return "bin" |