diff options
Diffstat (limited to 'libmat2/images.py')
| -rw-r--r-- | libmat2/images.py | 45 |
1 files changed, 4 insertions, 41 deletions
diff --git a/libmat2/images.py b/libmat2/images.py index a29cbb7..ad80892 100644 --- a/libmat2/images.py +++ b/libmat2/images.py | |||
| @@ -1,11 +1,6 @@ | |||
| 1 | import subprocess | ||
| 2 | import imghdr | 1 | import imghdr |
| 3 | import json | ||
| 4 | import os | 2 | import os |
| 5 | import shutil | 3 | from typing import Set |
| 6 | import tempfile | ||
| 7 | import re | ||
| 8 | from typing import Set, Dict, Union | ||
| 9 | 4 | ||
| 10 | import cairo | 5 | import cairo |
| 11 | 6 | ||
| @@ -13,44 +8,12 @@ import gi | |||
| 13 | gi.require_version('GdkPixbuf', '2.0') | 8 | gi.require_version('GdkPixbuf', '2.0') |
| 14 | from gi.repository import GdkPixbuf | 9 | from gi.repository import GdkPixbuf |
| 15 | 10 | ||
| 16 | from . import abstract, _get_exiftool_path | 11 | from . import exiftool |
| 17 | 12 | ||
| 18 | # Make pyflakes happy | 13 | # Make pyflakes happy |
| 19 | assert Set | 14 | assert Set |
| 20 | 15 | ||
| 21 | class _ImageParser(abstract.AbstractParser): | 16 | class PNGParser(exiftool.ExiftoolParser): |
| 22 | """ Since we use `exiftool` to get metadata from | ||
| 23 | all images fileformat, `get_meta` is implemented in this class, | ||
| 24 | and all the image-handling ones are inheriting from it.""" | ||
| 25 | meta_whitelist = set() # type: Set[str] | ||
| 26 | |||
| 27 | @staticmethod | ||
| 28 | def __handle_problematic_filename(filename: str, callback) -> bytes: | ||
| 29 | """ This method takes a filename with a problematic name, | ||
| 30 | and safely applies it a `callback`.""" | ||
| 31 | tmpdirname = tempfile.mkdtemp() | ||
| 32 | fname = os.path.join(tmpdirname, "temp_file") | ||
| 33 | shutil.copy(filename, fname) | ||
| 34 | out = callback(fname) | ||
| 35 | shutil.rmtree(tmpdirname) | ||
| 36 | return out | ||
| 37 | |||
| 38 | def get_meta(self) -> Dict[str, Union[str, dict]]: | ||
| 39 | """ There is no way to escape the leading(s) dash(es) of the current | ||
| 40 | self.filename to prevent parameter injections, so we need to take care | ||
| 41 | of this. | ||
| 42 | """ | ||
| 43 | fun = lambda f: subprocess.check_output([_get_exiftool_path(), '-json', f]) | ||
| 44 | if re.search('^[a-z0-9/]', self.filename) is None: | ||
| 45 | out = self.__handle_problematic_filename(self.filename, fun) | ||
| 46 | else: | ||
| 47 | out = fun(self.filename) | ||
| 48 | meta = json.loads(out.decode('utf-8'))[0] | ||
| 49 | for key in self.meta_whitelist: | ||
| 50 | meta.pop(key, None) | ||
| 51 | return meta | ||
| 52 | |||
| 53 | class PNGParser(_ImageParser): | ||
| 54 | mimetypes = {'image/png', } | 17 | mimetypes = {'image/png', } |
| 55 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', | 18 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', |
| 56 | 'Directory', 'FileSize', 'FileModifyDate', | 19 | 'Directory', 'FileSize', 'FileModifyDate', |
| @@ -77,7 +40,7 @@ class PNGParser(_ImageParser): | |||
| 77 | return True | 40 | return True |
| 78 | 41 | ||
| 79 | 42 | ||
| 80 | class GdkPixbufAbstractParser(_ImageParser): | 43 | class GdkPixbufAbstractParser(exiftool.ExiftoolParser): |
| 81 | """ GdkPixbuf can handle a lot of surfaces, so we're rending images on it, | 44 | """ GdkPixbuf can handle a lot of surfaces, so we're rending images on it, |
| 82 | this has the side-effect of completely removing metadata. | 45 | this has the side-effect of completely removing metadata. |
| 83 | """ | 46 | """ |
