Python mimetypes.guess_all_extensions() Examples
The following are 9
code examples of mimetypes.guess_all_extensions().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
mimetypes
, or try the search function
.
Example #1
Source File: OPKHelper.py From PyMenu with GNU General Public License v3.0 | 6 votes |
def getFileExtensions(mime): if(not mimetypes.inited): mimetypes.init() initKnowMimetypes() types = mime.split(";") result = [] for t in types: if(t): res = mimetypes.guess_all_extensions(t) #print("getting extensions for mime " + str(t) + " " + str(res)) result.extend(res) if(len(result) == 0): result.append(".*") return result
Example #2
Source File: bridge.py From hangoutsbot with GNU Affero General Public License v3.0 | 6 votes |
def _relay_msg_image(self, msg, conv_id): filename = os.path.basename(msg.file) logger.info("Uploading Slack image '{}' to Hangouts - {}".format(filename, json.dumps(msg.file))) for retry_count in range(3): try: logger.debug("Attempt {} at downloading file".format(retry_count+1)) # Retrieve the image content from Slack. resp = yield from Base.slacks[self.team].get(msg.file) name_ext = "." + filename.rsplit(".", 1).pop().lower() # Check the file extension matches the MIME type. mime_type = resp.content_type mime_exts = mimetypes.guess_all_extensions(mime_type) if name_ext.lower() not in [ext.lower() for ext in mime_exts]: raise ValueError("MIME '{}' does not match extension '{}', we probably didn't get the right file." + " Attempt [{}/3]" .format(mime_type, name_ext, retry_count+1)) image = yield from resp.read() image_id = yield from self.bot._client.upload_image(BytesIO(image), filename=filename) yield from self._relay_msg(msg, conv_id, image_id) break except ValueError as err: logger.error(err) yield from asyncio.sleep(2)
Example #3
Source File: http.py From gallery-dl with GNU General Public License v2.0 | 6 votes |
def get_extension(self, response): mtype = response.headers.get("Content-Type", "image/jpeg") mtype = mtype.partition(";")[0] if "/" not in mtype: mtype = "image/" + mtype if mtype in MIMETYPE_MAP: return MIMETYPE_MAP[mtype] exts = mimetypes.guess_all_extensions(mtype, strict=False) if exts: exts.sort() return exts[-1][1:] self.log.warning( "No filename extension found for MIME type '%s'", mtype) return "txt"
Example #4
Source File: amodels.py From dvhb-hybrid with MIT License | 6 votes |
def from_field(cls, file_field, *, user, connection=None): name = file_field.name exts = mimetypes.guess_all_extensions(file_field.content_type) for ext in exts: if name.endswith(ext): break else: if exts: name += exts[-1] name = await cls.app.loop.run_in_executor( None, image_storage.save, name, file_field.file) image_uuid = image_storage.uuid(name) return await cls.create( uuid=image_uuid, image=name, mime_type=file_field.content_type, created_at=utils.now(), author_id=user.pk, connection=connection )
Example #5
Source File: filecheck.py From PyCIRCLean with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _check_mimetype(self): """ Compare mimetype (as determined by libmagic) to extension. Determine whether the extension that are normally associated with the mimetype include the file's actual extension. """ if not self.has_mimetype: self.make_dangerous('File has no mimetype') else: if self.mimetype in Config.aliases: mimetype = Config.aliases[self.mimetype] else: mimetype = self.mimetype expected_extensions = mimetypes.guess_all_extensions(mimetype, strict=False) if mimetype in Config.aliases: expected_extensions += mimetypes.guess_all_extensions(Config.aliases[mimetype], strict=False) if expected_extensions: if self.has_extension and self.extension not in expected_extensions: self.make_dangerous('Extension does not match expected extensions ({}) for this mimetype'.format(expected_extensions))
Example #6
Source File: fileextensions.py From multiscanner with Mozilla Public License 2.0 | 6 votes |
def _get_tikaresults(results, fname): # Tika mime type is under Content-Type key. # Take the content type and make a guess of the # mimetype using python's built in mimetypes lib. tikadict = dict(results) try: content_types = tikadict.get(fname, {}).get('Content-Type') if not isinstance(content_types, list): content_types = [content_types] tika_extensions = [] for ctype in content_types: tika_extensions += mimetypes.guess_all_extensions(ctype) return list(set(tika_extensions)) except AttributeError: return []
Example #7
Source File: splittable_tab_widget.py From Turing with MIT License | 5 votes |
def get_filter(cls, mimetype): """ Returns a filter string for the file dialog. The filter is based on the mime type. :param mimetype: path from which the filter must be derived. :return: Filter string """ filters = ' '.join( ['*%s' % ext for ext in mimetypes.guess_all_extensions(mimetype)]) return '%s (%s)' % (mimetype, filters)
Example #8
Source File: amodels.py From dvhb-hybrid with MIT License | 5 votes |
def from_url(cls, url, *, user, connection=None): async with client.ClientSession() as session: async with session.get(url) as response: if response.status != 200: return l = response.content_length if not l or l > 2 ** 23: return content_type = response.content_type if not content_type.startswith('image'): return content = BytesIO(await response.read()) filename = response.url.name exts = mimetypes.guess_all_extensions(content_type) for ext in exts: if filename.endswith(ext): break else: if exts: filename += exts[-1] name = await cls.app.loop.run_in_executor( None, image_storage.save, filename, content) image_uuid = image_storage.uuid(name) return await cls.create( uuid=image_uuid, image=name, mime_type=content_type, created_at=utils.now(), author_id=user.pk, connection=connection )
Example #9
Source File: generic.py From PyCIRCLean with BSD 3-Clause "New" or "Revised" License | 4 votes |
def __init__(self, src_path, dst_path): ''' Init file object, set the mimetype ''' super(File, self).__init__(src_path, dst_path) self.is_recursive = False if not self.has_mimetype(): # No mimetype, should not happen. self.make_dangerous() if self.is_dangerous(): return self.log_details.update({'maintype': self.main_type, 'subtype': self.sub_type, 'extension': self.extension}) # If the mimetype matches as text/*, it will be sent to LibreOffice, no need to cross check the mime/ext if self.main_type == 'text': return # Check correlation known extension => actual mime type if propertype.get(self.extension) is not None: expected_mimetype = propertype.get(self.extension) else: expected_mimetype, encoding = mimetypes.guess_type(self.src_path, strict=False) if aliases.get(expected_mimetype) is not None: expected_mimetype = aliases.get(expected_mimetype) is_known_extension = self.extension in mimetypes.types_map.keys() if is_known_extension and expected_mimetype != self.mimetype: self.log_details.update({'expected_mimetype': expected_mimetype}) self.make_dangerous() # check correlation actual mime type => known extensions if aliases.get(self.mimetype) is not None: mimetype = aliases.get(self.mimetype) else: mimetype = self.mimetype expected_extensions = mimetypes.guess_all_extensions(mimetype, strict=False) if expected_extensions: if len(self.extension) > 0 and self.extension not in expected_extensions: self.log_details.update({'expected_extensions': expected_extensions}) self.make_dangerous() else: # there are no known extensions associated to this mimetype. pass