diff options
| -rw-r--r-- | libmat2/images.py | 11 | ||||
| -rw-r--r-- | tests/test_libmat2.py | 16 |
2 files changed, 25 insertions, 2 deletions
diff --git a/libmat2/images.py b/libmat2/images.py index 03718e6..a7a9cad 100644 --- a/libmat2/images.py +++ b/libmat2/images.py | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | import subprocess | 1 | import subprocess |
| 2 | import imghdr | ||
| 2 | import json | 3 | import json |
| 3 | import os | 4 | import os |
| 4 | import shutil | 5 | import shutil |
| @@ -68,6 +69,8 @@ class GdkPixbufAbstractParser(__ImageParser): | |||
| 68 | """ GdkPixbuf can handle a lot of surfaces, so we're rending images on it, | 69 | """ GdkPixbuf can handle a lot of surfaces, so we're rending images on it, |
| 69 | this has the side-effect of removing metadata completely. | 70 | this has the side-effect of removing metadata completely. |
| 70 | """ | 71 | """ |
| 72 | _type = '' | ||
| 73 | |||
| 71 | def remove_all(self): | 74 | def remove_all(self): |
| 72 | _, extension = os.path.splitext(self.filename) | 75 | _, extension = os.path.splitext(self.filename) |
| 73 | pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename) | 76 | pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename) |
| @@ -76,8 +79,14 @@ class GdkPixbufAbstractParser(__ImageParser): | |||
| 76 | pixbuf.savev(self.output_filename, extension[1:], [], []) | 79 | pixbuf.savev(self.output_filename, extension[1:], [], []) |
| 77 | return True | 80 | return True |
| 78 | 81 | ||
| 82 | def __init__(self, filename): | ||
| 83 | super().__init__(filename) | ||
| 84 | if imghdr.what(filename) != self._type: # better safe than sorry | ||
| 85 | raise ValueError | ||
| 86 | |||
| 79 | 87 | ||
| 80 | class JPGParser(GdkPixbufAbstractParser): | 88 | class JPGParser(GdkPixbufAbstractParser): |
| 89 | _type = 'jpeg' | ||
| 81 | mimetypes = {'image/jpeg'} | 90 | mimetypes = {'image/jpeg'} |
| 82 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', | 91 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', |
| 83 | 'Directory', 'FileSize', 'FileModifyDate', | 92 | 'Directory', 'FileSize', 'FileModifyDate', |
| @@ -90,6 +99,7 @@ class JPGParser(GdkPixbufAbstractParser): | |||
| 90 | 99 | ||
| 91 | 100 | ||
| 92 | class TiffParser(GdkPixbufAbstractParser): | 101 | class TiffParser(GdkPixbufAbstractParser): |
| 102 | _type = 'tiff' | ||
| 93 | mimetypes = {'image/tiff'} | 103 | mimetypes = {'image/tiff'} |
| 94 | meta_whitelist = {'Compression', 'ExifByteOrder', 'ExtraSamples', | 104 | meta_whitelist = {'Compression', 'ExifByteOrder', 'ExtraSamples', |
| 95 | 'FillOrder', 'PhotometricInterpretation', | 105 | 'FillOrder', 'PhotometricInterpretation', |
| @@ -103,6 +113,7 @@ class TiffParser(GdkPixbufAbstractParser): | |||
| 103 | 113 | ||
| 104 | 114 | ||
| 105 | class BMPParser(GdkPixbufAbstractParser): | 115 | class BMPParser(GdkPixbufAbstractParser): |
| 116 | _type = 'bmp' | ||
| 106 | mimetypes = {'image/x-ms-bmp'} | 117 | mimetypes = {'image/x-ms-bmp'} |
| 107 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', 'Directory', | 118 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', 'Directory', |
| 108 | 'FileSize', 'FileModifyDate', 'FileAccessDate', | 119 | 'FileSize', 'FileModifyDate', 'FileAccessDate', |
diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py index e1d949d..0df333d 100644 --- a/tests/test_libmat2.py +++ b/tests/test_libmat2.py | |||
| @@ -87,13 +87,25 @@ class TestCorruptedFiles(unittest.TestCase): | |||
| 87 | f.write("trailing garbage") | 87 | f.write("trailing garbage") |
| 88 | p = torrent.TorrentParser('./tests/data/clean.torrent') | 88 | p = torrent.TorrentParser('./tests/data/clean.torrent') |
| 89 | self.assertEqual(p.get_meta(), expected) | 89 | self.assertEqual(p.get_meta(), expected) |
| 90 | |||
| 91 | os.remove('./tests/data/clean.torrent') | 90 | os.remove('./tests/data/clean.torrent') |
| 92 | 91 | ||
| 93 | def test_odg(self): | 92 | def test_odg(self): |
| 94 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.odg') | 93 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.odg') |
| 95 | with self.assertRaises(ValueError): | 94 | with self.assertRaises(ValueError): |
| 96 | office.LibreOfficeParser('./tests/data/clean.odg') | 95 | office.LibreOfficeParser('./tests/data/clean.odg') |
| 96 | os.remove('./tests/data/clean.odg') | ||
| 97 | |||
| 98 | def test_bmp(self): | ||
| 99 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.bmp') | ||
| 100 | with self.assertRaises(ValueError): | ||
| 101 | p = images.BMPParser('./tests/data/clean.bmp') | ||
| 102 | os.remove('./tests/data/clean.bmp') | ||
| 103 | |||
| 104 | def test_docx(self): | ||
| 105 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.docx') | ||
| 106 | with self.assertRaises(ValueError): | ||
| 107 | p = office.MSOfficeParser('./tests/data/clean.docx') | ||
| 108 | os.remove('./tests/data/clean.docx') | ||
| 97 | 109 | ||
| 98 | class TestGetMeta(unittest.TestCase): | 110 | class TestGetMeta(unittest.TestCase): |
| 99 | def test_pdf(self): | 111 | def test_pdf(self): |
| @@ -123,7 +135,7 @@ class TestGetMeta(unittest.TestCase): | |||
| 123 | self.assertEqual(meta['Comment'], 'Created with GIMP') | 135 | self.assertEqual(meta['Comment'], 'Created with GIMP') |
| 124 | 136 | ||
| 125 | def test_tiff(self): | 137 | def test_tiff(self): |
| 126 | p = images.JPGParser('./tests/data/dirty.tiff') | 138 | p = images.TiffParser('./tests/data/dirty.tiff') |
| 127 | meta = p.get_meta() | 139 | meta = p.get_meta() |
| 128 | self.assertEqual(meta['Make'], 'OLYMPUS IMAGING CORP.') | 140 | self.assertEqual(meta['Make'], 'OLYMPUS IMAGING CORP.') |
| 129 | self.assertEqual(meta['Model'], 'C7070WZ') | 141 | self.assertEqual(meta['Model'], 'C7070WZ') |
