summaryrefslogtreecommitdiff
path: root/libmat2/images.py
diff options
context:
space:
mode:
Diffstat (limited to 'libmat2/images.py')
-rw-r--r--libmat2/images.py45
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 @@
1import subprocess
2import imghdr 1import imghdr
3import json
4import os 2import os
5import shutil 3from typing import Set
6import tempfile
7import re
8from typing import Set, Dict, Union
9 4
10import cairo 5import cairo
11 6
@@ -13,44 +8,12 @@ import gi
13gi.require_version('GdkPixbuf', '2.0') 8gi.require_version('GdkPixbuf', '2.0')
14from gi.repository import GdkPixbuf 9from gi.repository import GdkPixbuf
15 10
16from . import abstract, _get_exiftool_path 11from . import exiftool
17 12
18# Make pyflakes happy 13# Make pyflakes happy
19assert Set 14assert Set
20 15
21class _ImageParser(abstract.AbstractParser): 16class 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
53class 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
80class GdkPixbufAbstractParser(_ImageParser): 43class 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 """