summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjvoisin2018-06-08 17:34:53 +0200
committerjvoisin2018-06-08 17:34:53 +0200
commite86e8e3c237a8c517701701800a823159354b2c1 (patch)
tree22e97525f6d84e73d8444b486c21c22d7b5bcda0
parent11261c3d870b7c4b9fa3f7a3864bc65956ad786c (diff)
Improve the code to handle problematic filenames
-rw-r--r--libmat2/images.py31
1 files changed, 20 insertions, 11 deletions
diff --git a/libmat2/images.py b/libmat2/images.py
index e425735..fe700d2 100644
--- a/libmat2/images.py
+++ b/libmat2/images.py
@@ -3,6 +3,7 @@ import json
3import os 3import os
4import shutil 4import shutil
5import tempfile 5import tempfile
6import re
6 7
7import cairo 8import cairo
8 9
@@ -14,19 +15,27 @@ from . import abstract
14 15
15 16
16class __ImageParser(abstract.AbstractParser): 17class __ImageParser(abstract.AbstractParser):
18 @staticmethod
19 def __handle_problematic_filename(filename:str, callback) -> str:
20 """ This method takes a filename with a problematic name,
21 and safely applies it a `callback`."""
22 tmpdirname = tempfile.mkdtemp()
23 fname = os.path.join(tmpdirname, "temp_file")
24 shutil.copy(filename, fname)
25 out = callback(fname)
26 shutil.rmtree(tmpdirname)
27 return out
28
17 def get_meta(self): 29 def get_meta(self):
18 """ There is no way to escape the leading(s) dash(es) of the current 30 """ There is no way to escape the leading(s) dash(es) of the current
19 self.filename to prevent parameter injections, so we do have to copy it 31 self.filename to prevent parameter injections, so we need to take care
32 of this.
20 """ 33 """
21 fname = self.filename 34 fun = lambda f: subprocess.check_output(['/usr/bin/exiftool', '-json', f])
22 tmpdirname = "" 35 if not re.match('^[a-z0-9]', self.filename):
23 if self.filename.startswith('-'): 36 out = self.__handle_problematic_filename(self.filename, fun)
24 tmpdirname = tempfile.mkdtemp() 37 else:
25 fname = os.path.join(tmpdirname, self.filename) 38 out = fun(self.filename)
26 shutil.copy(self.filename, fname)
27 out = subprocess.check_output(['/usr/bin/exiftool', '-json', fname])
28 if self.filename.startswith('-'):
29 shutil.rmtree(tmpdirname)
30 meta = json.loads(out.decode('utf-8'))[0] 39 meta = json.loads(out.decode('utf-8'))[0]
31 for key in self.meta_whitelist: 40 for key in self.meta_whitelist:
32 meta.pop(key, None) 41 meta.pop(key, None)
@@ -63,7 +72,7 @@ class GdkPixbufAbstractParser(__ImageParser):
63 _, extension = os.path.splitext(self.filename) 72 _, extension = os.path.splitext(self.filename)
64 pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename) 73 pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename)
65 if extension == '.jpg': 74 if extension == '.jpg':
66 extension = '.jpeg' 75 extension = '.jpeg' # gdk is picky
67 pixbuf.savev(self.output_filename, extension[1:], [], []) 76 pixbuf.savev(self.output_filename, extension[1:], [], [])
68 return True 77 return True
69 78