diff options
| author | jvoisin | 2018-06-08 17:34:53 +0200 |
|---|---|---|
| committer | jvoisin | 2018-06-08 17:34:53 +0200 |
| commit | e86e8e3c237a8c517701701800a823159354b2c1 (patch) | |
| tree | 22e97525f6d84e73d8444b486c21c22d7b5bcda0 /libmat2 | |
| parent | 11261c3d870b7c4b9fa3f7a3864bc65956ad786c (diff) | |
Improve the code to handle problematic filenames
Diffstat (limited to 'libmat2')
| -rw-r--r-- | libmat2/images.py | 31 |
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 | |||
| 3 | import os | 3 | import os |
| 4 | import shutil | 4 | import shutil |
| 5 | import tempfile | 5 | import tempfile |
| 6 | import re | ||
| 6 | 7 | ||
| 7 | import cairo | 8 | import cairo |
| 8 | 9 | ||
| @@ -14,19 +15,27 @@ from . import abstract | |||
| 14 | 15 | ||
| 15 | 16 | ||
| 16 | class __ImageParser(abstract.AbstractParser): | 17 | class __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 | ||
