def test_not_found(self): """Bad file type via string |""" try: with self.assertRaises(puremagic.PureError): puremagic.from_string("not applicable string") except TypeError: # Python 2.6 doesn't support using assertRaises as a context manager pass
def test_not_found(self): """Bad file type via string |""" try: with self.assertRaises(puremagic.PureError): puremagic.from_string("not applicable string") except TypeError: # Python 2.6 doesn't support using # assertRaises as a context manager pass
def test_string_with_filename_hint(self): """String identification with filename hint |""" filename = os.path.join(OFFICE_DIR, "test.xlsx") with open(filename, "rb") as f: data = f.read() ext = puremagic.from_string(data) # .docx and .xlsx have same signature self.assertEqual(".docx", ext) # with the hint from_string() shoud find the correct extension ext = puremagic.from_string(data, filename=filename) self.assertEqual(".xlsx", ext)
def attach(self, binary): # determine the format of the file ext = puremagic.from_string(binary) page = None # if the attachment is a PDF if ext == ".pdf": # use PyPDF2 to read the stream pdf = PdfFileReader(BytesIO(binary)) # if it is a multi-page PDF if pdf.getNumPages() > 1: # add the pages individually for pdf_page in pdf.pages: output = PdfFileWriter() output.addPage(pdf_page) pdf_page_buf = BytesIO() output.write(pdf_page_buf) page = self.add_page(pdf_page_buf.getvalue()) # if it is just a single page PDF else: # then add the original bytestream page = self.add_page(binary) # if the attachment is a recognized image elif ext in [".png", ".jfif", ".gif", ".jpeg", ".jpg"]: page = self.add_page(binary) # could not recognize file else: pass if page: return (page)
def _image(self, author, image_meta): ftype = 'data/png' b64 = KETSUBAN if self.client: try: f = self.client.as_user(self.client.user(author)).file( image_meta['boxFileId']) except BoxException as e: self.logger.error( 'Failed to download image from Box, using placeholder image', exc_info=e) else: raw = f.content() b64 = base64.b64encode(raw).decode('ascii') try: ftype = puremagic.from_string( raw, mime=True, filename=image_meta.get('fileName')) except puremagic.PureError: ftype = '' else: self.logger.debug('No Box client, using placeholder image') return 'data:{};base64,{}'.format(ftype, ''.join(b64.splitlines()))
def guess_mimetype(uploaded_file: UploadedFile, filename: str = None) -> Optional[str]: try: head, foot = _uploaded_file_obj_to_buffer(uploaded_file) _mime = puremagic.from_string(head + foot, mime=True, filename=filename) except puremagic.PureError: _mime = None if _mime: return _mime else: if filename and Path(filename).suffix in EXTS: return settings.FTL_SUPPORTED_DOCUMENTS_TYPES[ Path(filename).suffix.lower() ][0]
def test_string(self): """String identification |""" ext = puremagic.from_string(bytes(self.mp4magic)) self.assertEqual(self.expect_ext, ext)
def test_hex_string(self): """Hex string identification""" ext = puremagic.from_string(self.mp4magic) self.assertEqual(self.expect_ext, ext)