diff options
| author | jvoisin | 2018-10-22 19:12:39 +0200 |
|---|---|---|
| committer | jvoisin | 2018-10-23 13:49:58 +0200 |
| commit | 38df679a88a19db3a4a82fdb8e20a42c9a53d1a1 (patch) | |
| tree | e7eea36bae3341a97e2b9a730964cfe632fb74f5 /libmat2/exiftool.py | |
| parent | 44f267a5964ea8dbb59c26c319e43fad84afb45a (diff) | |
Optimize the handling of problematic files
Diffstat (limited to 'libmat2/exiftool.py')
| -rw-r--r-- | libmat2/exiftool.py | 23 |
1 files changed, 2 insertions, 21 deletions
diff --git a/libmat2/exiftool.py b/libmat2/exiftool.py index 331ae0c..11dd36d 100644 --- a/libmat2/exiftool.py +++ b/libmat2/exiftool.py | |||
| @@ -1,11 +1,7 @@ | |||
| 1 | import json | 1 | import json |
| 2 | import os | 2 | import os |
| 3 | import re | ||
| 4 | import shutil | ||
| 5 | import subprocess | 3 | import subprocess |
| 6 | import tempfile | 4 | from typing import Dict, Union, Set |
| 7 | |||
| 8 | from typing import Dict, Union, Set, Callable, Any | ||
| 9 | 5 | ||
| 10 | from . import abstract | 6 | from . import abstract |
| 11 | 7 | ||
| @@ -20,23 +16,8 @@ class ExiftoolParser(abstract.AbstractParser): | |||
| 20 | """ | 16 | """ |
| 21 | meta_whitelist = set() # type: Set[str] | 17 | meta_whitelist = set() # type: Set[str] |
| 22 | 18 | ||
| 23 | def _handle_problematic_filename(self, callback: Callable[[str], Any]) -> bytes: | ||
| 24 | """ This method takes a filename with a potentially problematic name, | ||
| 25 | and safely applies a `callback` to it. | ||
| 26 | """ | ||
| 27 | if re.search('^[a-z0-9/]', self.filename) is not None: | ||
| 28 | return callback(self.filename) | ||
| 29 | |||
| 30 | tmpdirname = tempfile.mkdtemp() | ||
| 31 | fname = os.path.join(tmpdirname, "temp_file") | ||
| 32 | shutil.copy(self.filename, fname) | ||
| 33 | out = callback(fname) | ||
| 34 | shutil.rmtree(tmpdirname) | ||
| 35 | return out | ||
| 36 | |||
| 37 | def get_meta(self) -> Dict[str, Union[str, dict]]: | 19 | def get_meta(self) -> Dict[str, Union[str, dict]]: |
| 38 | fun = lambda f: subprocess.check_output([_get_exiftool_path(), '-json', f]) | 20 | out = subprocess.check_output([_get_exiftool_path(), '-json', self.filename]) |
| 39 | out = self._handle_problematic_filename(fun) | ||
| 40 | meta = json.loads(out.decode('utf-8'))[0] | 21 | meta = json.loads(out.decode('utf-8'))[0] |
| 41 | for key in self.meta_whitelist: | 22 | for key in self.meta_whitelist: |
| 42 | meta.pop(key, None) | 23 | meta.pop(key, None) |
