Guess the type of a file. Argument is a PATH (a filename). Return value is a string of the form type/subtype, usable for a MIME Content-type header. The default implementation looks the file's extension up in the table self.extensions_map, using application
(self, path)
| 914 | shutil.copyfileobj(source, outputfile) |
| 915 | |
| 916 | def guess_type(self, path): |
| 917 | """Guess the type of a file. |
| 918 | |
| 919 | Argument is a PATH (a filename). |
| 920 | |
| 921 | Return value is a string of the form type/subtype, |
| 922 | usable for a MIME Content-type header. |
| 923 | |
| 924 | The default implementation looks the file's extension |
| 925 | up in the table self.extensions_map, using application/octet-stream |
| 926 | as a default; however it would be permissible (if |
| 927 | slow) to look inside the data to make a better guess. |
| 928 | |
| 929 | """ |
| 930 | base, ext = posixpath.splitext(path) |
| 931 | if ext in self.extensions_map: |
| 932 | return self.extensions_map[ext] |
| 933 | ext = ext.lower() |
| 934 | if ext in self.extensions_map: |
| 935 | return self.extensions_map[ext] |
| 936 | guess, _ = mimetypes.guess_file_type(path) |
| 937 | if guess: |
| 938 | return guess |
| 939 | return 'application/octet-stream' |
| 940 | |
| 941 | |
| 942 | nobody = None |