From b1dab4c844e2fae4909eca47c2b42698d12c55fc Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sat, 18 Jun 2011 02:13:37 +0200 Subject: Hachoir is so ugly : I am able to display meta, but not to remove them (because I didn't manage to find an elegant/efficient way to find the "path" to a field). So I remove a whole "Rootfield" (exif, author, comment, ...). It's efficient, but not very elegant. --- cli.py | 12 ------------ mat.py | 44 +++++++++++++++++--------------------------- 2 files changed, 17 insertions(+), 39 deletions(-) diff --git a/cli.py b/cli.py index b5bea20..bd87409 100755 --- a/cli.py +++ b/cli.py @@ -16,8 +16,6 @@ def parse(): help='List all the meta of a file without removing them') common.add_option('--check', '-c', action='store_true', default=False, help='Check if a file is free of harmfull metadatas') - common.add_option('--harmful', action='store_true', default=False, - help='List all the harmful meta of a file without removing them') common.add_option('--version', action='callback', callback=displayVersion, help='Display version and exit') @@ -42,14 +40,6 @@ def list_meta(class_file, filename): for key, item in class_file.get_meta().iteritems(): print('\t%s : %s' % (key, item) ) -def list_harmful_meta(class_file, filename): - ''' - Print all the harmful meta of 'filename' on stdout - ''' - print('[+] File %s :' % filename) - for key, item in class_file.get_harmful().iteritems(): - print('\t%s : %s' % (key, item) ) - def is_clean(class_file, filename): ''' Say if 'filename' is clean or not @@ -78,8 +68,6 @@ def main(): func = list_meta elif args.check is True: #only check if the file is clean func = is_clean - elif args.harmful is True: #only print harmful metadatas - func = list_harmful_meta else: #clean the file func = clean_meta diff --git a/mat.py b/mat.py index f59bdce..200fc04 100755 --- a/mat.py +++ b/mat.py @@ -18,6 +18,7 @@ import hachoir_parser.image __version__ = "0.1" __author__ = "jvoisin" +POSTFIX = ".cleaned" class file(): def __init__(self, realname, filename, parser, editor): @@ -52,8 +53,8 @@ class file(): ''' Check if the file is clean from harmful metadatas ''' - for key, field in self.meta.iteritems(): - if self._should_remove(key): + for field in self.editor: + if self._should_remove(field): return False return True @@ -61,37 +62,25 @@ class file(): ''' Remove all the files that are compromizing ''' - for key, field in self.meta.iteritems(): - if self._should_remove(key): - self._remove(key) - hachoir_core.field.writeIntoFile(self.editor, self.filename + "cleaned") + for field in self.editor: + if self._should_remove(field): + self._remove(field) + hachoir_core.field.writeIntoFile(self.editor, self.filename + POSTFIX) - - def _remove(self, key): + def _remove(self, field): ''' Remove the given field ''' - del self.editor[key] + del self.editor[field.name] def get_meta(self): ''' return a dict with all the meta of the file ''' + #am I useless ? return self.meta - def get_harmful(self): - ''' - return a dict with all the harmful meta of the file - ''' - harmful = {} - for key, value in self.meta.iteritems(): - if self._should_remove(key): - harmful[key] = value - return harmful - - - def _should_remove(self, key): ''' return True if the field is compromizing @@ -100,19 +89,20 @@ class file(): raise NotImplementedError() class JpegStripper(file): - def _should_remove(self, key): - if key in ('comment', 'author'): + def _should_remove(self, field): + if field.name.startswith('comment'): + return True + elif field.name in ("photoshop", "exif", "adobe"): return True else: return False class PngStripper(file): - def _should_remove(self, key): - if key in ('comment', 'author'): + def _should_remove(self, field): + if field.name in ('comment'): return True else: return False - return False strippers = { hachoir_parser.image.JpegFile: JpegStripper, @@ -131,7 +121,7 @@ def create_class_file(name): filename = "" realname = name filename = hachoir_core.cmd_line.unicodeFilename(name) - parser = hachoir_parser.createParser(filename, realname) + parser = hachoir_parser.createParser(filename) if not parser: print("Unable to parse the file %s : sorry" % filename) sys.exit(1) -- cgit v1.3