Skip to content

Commit c43485b

Browse files
committed
Added a method to get the content type of a file in MIME format (example: image/jpeg)
1 parent 2a85418 commit c43485b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/cuckoo/common/objects.py

+31
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,37 @@ def get_type(self):
209209

210210
return file_type
211211

212+
def get_content_type(self):
213+
"""Get MIME content file type (example: image/jpeg).
214+
@return: file content type.
215+
"""
216+
file_type = None
217+
if HAVE_MAGIC:
218+
try:
219+
ms = magic.open(magic.MAGIC_MIME)
220+
ms.load()
221+
file_type = ms.file(self.file_path)
222+
except:
223+
try:
224+
file_type = magic.from_file(self.file_path, mime=True)
225+
except:
226+
pass
227+
finally:
228+
try:
229+
ms.close()
230+
except:
231+
pass
232+
233+
if file_type is None:
234+
try:
235+
p = subprocess.Popen(["file", "-b", "--mime-type", self.file_path],
236+
stdout=subprocess.PIPE)
237+
file_type = p.stdout.read().strip()
238+
except:
239+
pass
240+
241+
return file_type
242+
212243
def get_yara(self, rulepath=os.path.join(CUCKOO_ROOT, "data", "yara", "index_binaries.yar")):
213244
"""Get Yara signatures matches.
214245
@return: matched Yara signatures.

tests/objects_tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def test_get_ssdeep(self):
6262
def test_get_type(self):
6363
assert_equal("empty", self.file.get_type())
6464

65+
def test_get_content_type(self):
66+
assert_equal("inode/x-empty", self.file.get_content_type())
67+
6568
def test_get_all_type(self):
6669
assert isinstance(self.file.get_all(), dict)
6770

0 commit comments

Comments
 (0)