From af36529554c39a2eefcc2c8723715e2d25b401b8 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 8 Jun 2014 13:39:18 +0200 Subject: Rename the MAT folder to libmat. This commit fixes some issues for dump operating systems who doesn't handle capitalization. --- libmat/exiftool.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 libmat/exiftool.py (limited to 'libmat/exiftool.py') diff --git a/libmat/exiftool.py b/libmat/exiftool.py new file mode 100644 index 0000000..9e38f04 --- /dev/null +++ b/libmat/exiftool.py @@ -0,0 +1,78 @@ +''' Care about images with help of the amazing (perl) library Exiftool. +''' + +import parser +import subprocess + + +class ExiftoolStripper(parser.GenericParser): + ''' A generic stripper class using exiftool as backend + ''' + + def __init__(self, filename, parser, mime, backup, is_writable, **kwargs): + super(ExiftoolStripper, self).__init__(filename, parser, mime, backup, is_writable, **kwargs) + self.allowed = set(['ExifTool Version Number', 'File Name', 'Directory', + 'File Size', 'File Modification Date/Time', 'File Access Date/Time', 'File Permissions', + 'File Type', 'MIME Type', 'Image Width', 'Image Height', + 'Image Size', 'File Inode Change Date/Time']) + self._set_allowed() + + def _set_allowed(self): + ''' Virtual method. Set the allowed/harmless list of metadata + ''' + raise NotImplementedError + + def remove_all(self): + ''' Remove all metadata with help of exiftool + ''' + try: + if self.backup: + self.create_backup_copy() + # Note: '-All=' must be followed by a known exiftool option. + subprocess.call(['exiftool', '-m', '-all=', + '-adobe=', '-overwrite_original', self.filename], + stdout=open('/dev/null')) + return True + except: + return False + + def is_clean(self): + ''' Check if the file is clean with the help of exiftool + ''' + return not self.get_meta() + + def get_meta(self): + ''' Return every harmful meta with help of exiftool. + Exiftool output looks like this: + field name : value + field name : value + ''' + output = subprocess.Popen(['exiftool', self.filename], + stdout=subprocess.PIPE).communicate()[0] + meta = {} + for i in output.split('\n')[:-1]: # chop last char ('\n') + key = i.split(':')[0].strip() + if key not in self.allowed: + meta[key] = i.split(':')[1].strip() # add the field name to the metadata set + return meta + + +class JpegStripper(ExiftoolStripper): + ''' Care about jpeg files with help + of exiftool + ''' + def _set_allowed(self): + self.allowed.update(['JFIF Version', 'Resolution Unit', + 'X Resolution', 'Y Resolution', 'Encoding Process', + 'Bits Per Sample', 'Color Components', 'Y Cb Cr Sub Sampling']) + + +class PngStripper(ExiftoolStripper): + ''' Care about png files with help + of exiftool + ''' + def _set_allowed(self): + self.allowed.update(['Bit Depth', 'Color Type', + 'Compression', 'Filter', 'Interlace', 'Pixels Per Unit X', + 'Pixels Per Unit Y', 'Pixel Units', 'Significant Bits', + 'Background Color', 'SRGB Rendering']) -- cgit v1.3