def get_good_mime(music_file): mime = Mime.get_type_by_contents(music_file) if mime == None: # try magic instead mime = magic.open(magic.MAGIC_MIME) mime.load() mime = mime.file(music_file) mime = mime.split(";")[0] else: mime = str(mime) return 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_contents(self): tmpdir = tempfile.mkdtemp() try: test_file = os.path.join(tmpdir, "test") with open(test_file, "wb") as f: f.write(resources.png_data) imgpng = Mime.get_type_by_contents(test_file) self.check_mimetype(imgpng, 'image', 'png') finally: shutil.rmtree(tmpdir)