-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfiletypes_fallback.py
More file actions
159 lines (142 loc) · 3.67 KB
/
filetypes_fallback.py
File metadata and controls
159 lines (142 loc) · 3.67 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from KalturaCoreClient import KalturaMediaType
import magic # python-magic library for mime type checking.
import os
import sys
import urllib
image_types = [
'JPEG',
'JPG',
'PNG',
'TIFF',
'GIF',
'JP2',
'JPX',
'JPF',
'JPC',
'J2K',
'J2C',
'DNG',
'CRW',
'CR2',
'ZVI',
'SVG',
'BMP',
'XIF'
]
audio_types = [
'MP3',
'WMA',
'FLAC',
'AIFF',
'AAC',
'WEBA',
]
video_types = [
'AVI',
'SWF',
'FLV',
'WMV',
'MPEG',
'ASF',
'3GP',
'3G2',
'MOV',
'MP4',
'M4V',
'OGG',
'OGV',
'F4V',
'MKV',
'WEBM'
]
def image_type(ext):
for name in image_types:
if ext.lower() == name.lower():
return name
return None
def video_type(ext):
for name in video_types:
if ext.lower() == name.lower():
return name
return None
def audio_type(ext):
for name in audio_types:
if ext.lower() == name.lower():
return name
return None
def check_type(ext):
if video_type(ext):
return 'VIDEO'
elif audio_type(ext):
return 'AUDIO'
elif image_type(ext):
return 'IMAGE'
else:
return None
def get_KalturaMediaType_from_pull_url(purl, default):
'''
will try to read first 10kb of stream and guess mime type,
if can't, returns default.
'''
uo = urllib.urlopen(purl)
try:
if sys.version_info[0] > 2:
magic_type = magic.from_buffer(
uo.read(10240), mime=True).decode('utf-8')
else:
magic_type = magic.from_buffer(
uo.read(10240), mime=True)
magic_type = magic_type.split('/')[0].upper()
uo.close()
except:
print ('couldn\'t find type via magic, using ext...')
magic_type = check_type(os.path.splitext(purl)[1].lstrip('.'))
if magic_type == 'APPLICATION':
print ('magic returned arbitrary type (application/x-octet-stream thingy), tryna use ext instead')
magic_type = check_type(os.path.splitext(purl)[1].lstrip('.'))
if magic_type == 'VIDEO':
return KalturaMediaType.VIDEO
elif magic_type == 'AUDIO':
return KalturaMediaType.AUDIO
elif magic_type == 'IMAGE':
return KalturaMediaType.IMAGE
elif default.lower() == 'video':
print ('failed to get type, using default.')
return KalturaMediaType.VIDEO
elif default.lower() == 'audio':
print ('failed to get type, using default.')
return KalturaMediaType.AUDIO
elif default.lower() == 'image':
print ('failed to get type, using default.')
return KalturaMediaType.IMAGE
def get_KalturaMediaType_from_file(path):
ext = os.path.splitext(path)[1].lstrip('.')
typename = check_type(ext)
if sys.version_info[0] > 2:
magic_type = magic.from_file(path, mime=True).decode('utf-8')
else:
magic_type = magic.from_file(path, mime=True)
magic_type = magic_type.split('/')[0].upper()
if not typename:
if magic_type == 'VIDEO':
return KalturaMediaType.VIDEO
elif magic_type == 'AUDIO':
return KalturaMediaType.AUDIO
elif magic_type == 'IMAGE':
return KalturaMediaType.IMAGE
elif typename == 'VIDEO':
return KalturaMediaType.VIDEO
elif typename == 'AUDIO':
return KalturaMediaType.AUDIO
elif typename == 'IMAGE':
return KalturaMediaType.IMAGE
def get_specified_type(s):
s = s[0].lower() if s else ''
t = None
if s == 'v':
t = KalturaMediaType.VIDEO
elif s == 'a':
t = KalturaMediaType.AUDIO
elif s == 'i':
t = KalturaMediaType.IMAGE
return t