Skip to content
Snippets Groups Projects
Commit e86e8e3c authored by Julien (jvoisin) Voisin's avatar Julien (jvoisin) Voisin
Browse files

Improve the code to handle problematic filenames

parent 11261c3d
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ import json ...@@ -3,6 +3,7 @@ import json
import os import os
import shutil import shutil
import tempfile import tempfile
import re
import cairo import cairo
...@@ -14,19 +15,27 @@ from . import abstract ...@@ -14,19 +15,27 @@ from . import abstract
class __ImageParser(abstract.AbstractParser): class __ImageParser(abstract.AbstractParser):
@staticmethod
def __handle_problematic_filename(filename:str, callback) -> str:
""" This method takes a filename with a problematic name,
and safely applies it a `callback`."""
tmpdirname = tempfile.mkdtemp()
fname = os.path.join(tmpdirname, "temp_file")
shutil.copy(filename, fname)
out = callback(fname)
shutil.rmtree(tmpdirname)
return out
def get_meta(self): def get_meta(self):
""" There is no way to escape the leading(s) dash(es) of the current """ There is no way to escape the leading(s) dash(es) of the current
self.filename to prevent parameter injections, so we do have to copy it self.filename to prevent parameter injections, so we need to take care
of this.
""" """
fname = self.filename fun = lambda f: subprocess.check_output(['/usr/bin/exiftool', '-json', f])
tmpdirname = "" if not re.match('^[a-z0-9]', self.filename):
if self.filename.startswith('-'): out = self.__handle_problematic_filename(self.filename, fun)
tmpdirname = tempfile.mkdtemp() else:
fname = os.path.join(tmpdirname, self.filename) out = fun(self.filename)
shutil.copy(self.filename, fname)
out = subprocess.check_output(['/usr/bin/exiftool', '-json', fname])
if self.filename.startswith('-'):
shutil.rmtree(tmpdirname)
meta = json.loads(out.decode('utf-8'))[0] meta = json.loads(out.decode('utf-8'))[0]
for key in self.meta_whitelist: for key in self.meta_whitelist:
meta.pop(key, None) meta.pop(key, None)
...@@ -63,7 +72,7 @@ class GdkPixbufAbstractParser(__ImageParser): ...@@ -63,7 +72,7 @@ class GdkPixbufAbstractParser(__ImageParser):
_, extension = os.path.splitext(self.filename) _, extension = os.path.splitext(self.filename)
pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename) pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename)
if extension == '.jpg': if extension == '.jpg':
extension = '.jpeg' extension = '.jpeg' # gdk is picky
pixbuf.savev(self.output_filename, extension[1:], [], []) pixbuf.savev(self.output_filename, extension[1:], [], [])
return True return True
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment