forked from dd388/voidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
voidity.py
executable file
·199 lines (159 loc) · 4.94 KB
/
voidity.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/env python3
import os
import docx
import sys
import magic
from PIL import Image
from glob import glob
from argparse import ArgumentParser
from collections import defaultdict
import json
fmtstext = ['application/msword',
'application/pdf',
'text/css',
'text/html',
'text/plain',
'text/xml',
'text/csv',
'text/tab-separated-values',
'text/rtf',
'text/sgml'
]
fmtsimg = ['image/bmp',
'image/gif',
'image/jp2',
'image/jpeg',
'image/png',
'image/tiff',
'image/x-jng'
]
fmtsau = ['application/ogg',
'audio/mpeg',
'audio/midi',
'audio/ogg',
'audio/x-wav'
]
fmtsvid = ['video/mp4',
'video/mpeg',
'video/ogg',
'video/quicktime'
]
fmtsarchive = ['application/zip']
## GENERAL TESTS ##
# Size of file
def _size(obj):
try:
res = os.stat(obj)
except:
return None
if res.st_size > 1000000:
return False
else:
return True
## TEXT TESTS ##
def _word_length(obj):
if len(obj.paragraphs) < 2:
return True
else:
w = 0
for o in obj.paragraphs:
w = w + len(o.text)
if w < 100:
return True
else:
return False
def _text_length(obj):
if len(obj) < 100:
return True
else:
return False
## IMAGE TESTS ##
def _image_dimensions(obj):
if (obj.size[0] < 100) or (obj.size[1] < 100):
return True
else:
return False
def _image_color(obj):
if len(obj.getcolors()) < 3:
return True
else:
return False
## IMAGE FORMAT TESTS ##
#http://git.imagemagick.org/repos/ImageMagick/commit/501b648ee40f804228c76fddc02ca479c75666f3
def _png_min_size(obj):
if os.path.getsize(obj) < 61:
return True
else:
return False
#http://git.imagemagick.org/repos/ImageMagick/commit/f9574dc71cc1ab8219b3bdfba11bf67dc2d98c71
def _jpeg_min_size(obj):
if os.path.getsize(obj) < 107:
return True
else:
return False
#http://git.imagemagick.org/repos/ImageMagick/commit/3cc9d45352ebb92947d27c46e2604104b7ebfe90
def _jng_min_size(obj):
if os.path.getsize(obj) < 147:
return True
else:
return False
##ARCHIVE FORMAT TESTS ##
#https://en.wikipedia.org/wiki/Zip_(file_format), see Limits
def _zip_min_size(obj):
if os.path.getsize(obj) < 22:
return True
else:
return False
def runtests(filename):
results = defaultdict(dict)
fmime = magic.from_file(filename, mime=True)
results['general']['mime_type'] = fmime
results['general']['less_than_1mb'] = _size(filename)
if fmime in fmtsimg:
try:
imgfile = Image.open(filename)
except:
imgfile = None
else:
if imgfile:
results['images']['less_than_100x100'] = _image_dimensions(imgfile)
results['images']['less_than_3_colors'] = _image_color(imgfile)
else:
results['images']['less_than_100x100'] = None
results['images']['less_than_3_colors'] = None
finally:
if fmime == 'image/png':
results['images']['under_png_min_size'] = _png_min_size(filename)
if fmime == 'image/jpeg':
results['images']['under_jpeg_min_size'] = _jpeg_min_size(filename)
if fmime == 'image/x-jng':
results['images']['under_jng_min_size'] = _jng_min_size(filename)
if fmime in fmtstext:
if fmime == 'application/msword':
results['text']['length_less_than_100c'] = _word_length(docx.Document(filename))
else:
results['text']['length_less_than_100c'] = _text_length(open(filename).read())
if fmine in fmtsarchive:
if fmime == 'application/zip':
results['archive']['under_zip_min_size'] = _zip_min_size(filename)
return results
def main():
parser = ArgumentParser()
parser.add_argument('input_dir', metavar='[input directory]',
help='Path of files to scan')
parser.add_argument('output', metavar='[output file]',
help='File to output report (will not overwrite)')
args = parser.parse_args()
if os.path.isfile(args.output):
sys.exit('error: output file already exists')
if os.path.exists(args.input_dir) and os.path.isdir(args.input_dir):
tc = [_tc for _tc in glob(os.path.join(args.input_dir, '*')) if not os.path.isdir(_tc)]
else:
sys.exit('error: input directory doesn\'t exist or the input directory isn\'t a directory')
allres = {}
for tf in tc:
allres[tf] = runtests(tf)
with open(args.output, 'w') as output:
json.dump(allres, output, indent=4, sort_keys=True)
if __name__ == '__main__':
main()