def get_mime_type(content): mime = Mime.get_type_by_data(content) """try harder to guess mime type""" """sometimes helps to trim leading newlines""" """FIXME: try with smaller chunk of content""" if mime is None or mime == "text/plain": tmpmime = Mime.get_type_by_data(content.strip("\n")) if tmpmime and tmpmime != "text/plain": mime = tmpmime else: mime = "text/x-csrc" return str(mime)
def TestOneInput(input_bytes): # We need to make the file an absolute path testfile_path = os.path.join(os.getcwd(), "testfile.tmp") with open(testfile_path, "wb") as f: f.write(input_bytes) # Test basic Mime API Mime.get_type2(testfile_path) Mime.get_type_by_contents(testfile_path) Mime.get_type_by_data(input_bytes) # Test GlobDB globs = Mime.GlobDB() try: globs.merge_file(testfile_path) globs.merge_file(testfile_path) except UnicodeError as e: pass except ValueError as e: if ("not enough values to unpack" in str(e) or "invalid literal for int" in str(e)): pass else: raise e # Test MagicDB magic = Mime.MagicDB() try: magic.merge_file(testfile_path) magic.finalise() except UnicodeDecodeError: pass except (OSError, ValueError) as e: msg = str(e) if ("Not a MIME magic file" in msg or "Malformed section heading" in msg): pass else: raise e # Cleanup os.remove(testfile_path)
def test_get_type_by_data(self): imgpng = Mime.get_type_by_data(resources.png_data) self.check_mimetype(imgpng, 'image', 'png')