Python magic.MAGIC_MIME_TYPE Examples

The following are 3 code examples of magic.MAGIC_MIME_TYPE(). 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 magic , or try the search function .
Example #1
Source File: client.py    From jira with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _try_magic(self):
        try:
            import magic
            import weakref
        except ImportError:
            self._magic = None
        else:
            try:
                _magic = magic.Magic(flags=magic.MAGIC_MIME_TYPE)

                def cleanup(x):
                    _magic.close()

                self._magic_weakref = weakref.ref(self, cleanup)
                self._magic = _magic
            except TypeError:
                self._magic = None
            except AttributeError:
                self._magic = None 
Example #2
Source File: base.py    From machinae with MIT License 6 votes vote down vote up
def unzip_content(r, *args, **kwargs):
        content = r.content

        with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
            mime = m.id_buffer(content)

        if mime == "application/zip":
            zip_buffer = io.BytesIO(content)
            with zipfile.ZipFile(zip_buffer) as zf:
                fn = zf.namelist()[0]
                with zf.open(fn) as f:
                    r._content = f.read()
        elif mime == "application/x-gzip":
            gz_buffer = io.BytesIO(content)
            with gzip.GzipFile(fileobj=gz_buffer) as gz:
                r._content = gz.read()
        else:
            r._content = content

        return r 
Example #3
Source File: mimetype.py    From stoq-plugins-public with Apache License 2.0 5 votes vote down vote up
def scan(self, payload: Payload, request: Request) -> WorkerResponse:
        if USE_PYTHON_MAGIC:
            magic_scan = magic.Magic(mime=True)
            magic_result = magic_scan.from_buffer(payload.content[0:1000])
        else:
            with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
                magic_result = m.id_buffer(payload.content[0:1000])
        if hasattr(magic_result, 'decode'):
            magic_result = magic_result.decode('utf-8')
        return WorkerResponse(results={'mimetype': magic_result})