diff options
| author | jvoisin | 2011-07-26 18:33:56 +0200 |
|---|---|---|
| committer | jvoisin | 2011-07-26 18:33:56 +0200 |
| commit | acac4b3d847706c7c95ca77ab7b141fadf7e6449 (patch) | |
| tree | 2af94a3959bccbf8091923605eaebfd71fe8a46a /lib | |
| parent | 962e9aec5ffcdaae39e06f277dd47d1943205c37 (diff) | |
Correct handling of non-existent files
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/mat.py | 37 |
1 files changed, 21 insertions, 16 deletions
| @@ -17,6 +17,7 @@ import images | |||
| 17 | import audio | 17 | import audio |
| 18 | import office | 18 | import office |
| 19 | import archive | 19 | import archive |
| 20 | import container | ||
| 20 | 21 | ||
| 21 | __version__ = '0.1' | 22 | __version__ = '0.1' |
| 22 | __author__ = 'jvoisin' | 23 | __author__ = 'jvoisin' |
| @@ -26,16 +27,22 @@ LOGGING_LEVEL = logging.DEBUG | |||
| 26 | logging.basicConfig(level=LOGGING_LEVEL) | 27 | logging.basicConfig(level=LOGGING_LEVEL) |
| 27 | 28 | ||
| 28 | STRIPPERS = { | 29 | STRIPPERS = { |
| 29 | hachoir_parser.image.JpegFile: images.JpegStripper, | ||
| 30 | hachoir_parser.image.PngFile: images.PngStripper, | ||
| 31 | hachoir_parser.audio.MpegAudioFile: audio.MpegAudioStripper, | ||
| 32 | hachoir_parser.misc.PDFDocument: office.PdfStripper, | ||
| 33 | hachoir_parser.archive.TarFile: archive.TarStripper, | 30 | hachoir_parser.archive.TarFile: archive.TarStripper, |
| 34 | hachoir_parser.archive.gzip_parser.GzipParser: archive.GzipStripper, | 31 | hachoir_parser.archive.gzip_parser.GzipParser: archive.GzipStripper, |
| 35 | hachoir_parser.archive.bzip2_parser.Bzip2Parser: archive.Bzip2Stripper, | 32 | hachoir_parser.archive.bzip2_parser.Bzip2Parser: archive.Bzip2Stripper, |
| 36 | hachoir_parser.archive.zip.ZipFile: archive.ZipStripper, | 33 | hachoir_parser.archive.zip.ZipFile: archive.ZipStripper, |
| 34 | hachoir_parser.audio.MpegAudioFile: audio.MpegAudioStripper, | ||
| 35 | hachoir_parser.image.JpegFile: images.JpegStripper, | ||
| 36 | hachoir_parser.image.PngFile: images.PngStripper, | ||
| 37 | hachoir_parser.container.OggFile: container.OggStripper, | ||
| 38 | hachoir_parser.misc.PDFDocument: office.PdfStripper, | ||
| 37 | } | 39 | } |
| 38 | 40 | ||
| 41 | try: | ||
| 42 | import mutagen | ||
| 43 | STRIPPERS[hachoir_parser.audio.FlacParser] = audio.FLACStripper | ||
| 44 | except ImportError: | ||
| 45 | print('unable to import python-mutagen : limited audio format support') | ||
| 39 | 46 | ||
| 40 | def secure_remove(filename): | 47 | def secure_remove(filename): |
| 41 | ''' | 48 | ''' |
| @@ -51,10 +58,11 @@ def is_secure(filename): | |||
| 51 | ''' | 58 | ''' |
| 52 | Prevent shell injection | 59 | Prevent shell injection |
| 53 | ''' | 60 | ''' |
| 54 | |||
| 55 | if not(os.path.isfile(filename)): # check if the file exist | 61 | if not(os.path.isfile(filename)): # check if the file exist |
| 56 | logging.error('Error: %s is not a valid file' % filename) | 62 | logging.error('Error: %s is not a valid file' % filename) |
| 57 | return False | 63 | return False |
| 64 | else: | ||
| 65 | return True | ||
| 58 | 66 | ||
| 59 | 67 | ||
| 60 | def create_class_file(name, backup, add2archive): | 68 | def create_class_file(name, backup, add2archive): |
| @@ -62,7 +70,7 @@ def create_class_file(name, backup, add2archive): | |||
| 62 | return a $FILETYPEStripper() class, | 70 | return a $FILETYPEStripper() class, |
| 63 | corresponding to the filetype of the given file | 71 | corresponding to the filetype of the given file |
| 64 | ''' | 72 | ''' |
| 65 | if is_secure(name): | 73 | if not is_secure(name): |
| 66 | return | 74 | return |
| 67 | 75 | ||
| 68 | filename = '' | 76 | filename = '' |
| @@ -95,16 +103,13 @@ def create_class_file(name, backup, add2archive): | |||
| 95 | elif editor.input.__class__ == hachoir_parser.archive.zip.ZipFile: | 103 | elif editor.input.__class__ == hachoir_parser.archive.zip.ZipFile: |
| 96 | #zip based format | 104 | #zip based format |
| 97 | mime = mimetypes.guess_type(filename)[0] | 105 | mime = mimetypes.guess_type(filename)[0] |
| 98 | try: # ugly workaround, cleaning open document delete mime (wtf?) | 106 | if mime.startswith('application/vnd.oasis.opendocument'): |
| 99 | if mime.startswith('application/vnd.oasis.opendocument'): | 107 | return office.OpenDocumentStripper(realname, filename, parser, |
| 100 | return office.OpenDocumentStripper(realname, filename, parser, | 108 | editor, backup, add2archive) |
| 101 | editor, backup, add2archive) | 109 | else: # normal zip |
| 102 | else: # normal zip | 110 | return stripper_class(realname, filename, parser, editor, |
| 103 | return stripper_class(realname, filename, parser, editor, | 111 | backup, add2archive) |
| 104 | backup, add2archive) | 112 | |
| 105 | except: # normal zip | ||
| 106 | return stripper_class(realname, filename, parser, editor, backup, | ||
| 107 | add2archive) | ||
| 108 | else: # normal handling | 113 | else: # normal handling |
| 109 | return stripper_class(realname, filename, parser, editor, backup, | 114 | return stripper_class(realname, filename, parser, editor, backup, |
| 110 | add2archive) | 115 | add2archive) |
