anonymous / gist:3417652
Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Detect a file's category.

View gist:3417652
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
import mimetypes
import magic
 
 
magic_mime = magic.open(magic.MAGIC_MIME)
magic_mime.load()
 
 
class FileCategory(list):
def __init__(self, *mimes):
for mime in mimes:
self.append(mime)
 
class FileCategory:
IMAGE = FileCategory()
AUDIO = FileCategory()
VIDEO = FileCategory()
DOCUMENT = FileCategory(
'application/vnd.oasis.opendocument.text', 'application/vnd.oasis.opendocument.presentation',
'application/msword' #... etc.
)
ARCHIVE = FileCategory(
'application/x-tar', 'application/x-cpio', 'application/x-bzip2',
'application/x-7z-compressed' #... etc.
)
OTHER = FileCategory()
 
def get_file_category(path):
mime_type = magic_mime.file(path)
if mime_type:
mime_type = mime_type.split(';')[0]
else:
mime_type = mimetypes.guess_type(path)[0]
if mime_type.startswith('image/'):
return FileCategory.IMAGE
if mime_type.startswith('audio/'):
return FileCategory.AUDIO
if mime_type.startswith('video/'):
return FileCategory.VIDEO
if mime_type.startswith('text/'):
return FileCategory.DOCUMENT
if mime_type in FileCategory.IMAGE:
return FileCategory.IMAGE
if mime_type in FileCategory.AUDIO:
return FileCategory.AUDIO
if mime_type in FileCategory.VIDEO:
return FileCategory.VIDEO
if mime_type in FileCategory.DOCUMENT:
return FileCategory.DOCUMENT
if mime_type in FileCategory.ARCHIVE:
return FileCategory.ARCHIVE
return FileCategory.OTHER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.