diff options
| author | jvoisin | 2018-06-22 20:38:29 +0200 |
|---|---|---|
| committer | jvoisin | 2018-06-22 20:38:29 +0200 |
| commit | b4ef0c9622a0741bcfa0da1f65d9082251fb4107 (patch) | |
| tree | 37d3a70ef17f14267e8a8bbe88f97589f15848df /libmat2 | |
| parent | dfccf79f22a9d0dd2f0d9015dbe0cb398b01078c (diff) | |
Improve reliability against corrupted image files
Diffstat (limited to 'libmat2')
| -rw-r--r-- | libmat2/images.py | 11 |
1 files changed, 11 insertions, 0 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', |
