summaryrefslogtreecommitdiff
path: root/libmat2/images.py
diff options
context:
space:
mode:
Diffstat (limited to 'libmat2/images.py')
-rw-r--r--libmat2/images.py20
1 files changed, 10 insertions, 10 deletions
diff --git a/libmat2/images.py b/libmat2/images.py
index 8f7a98d..a29cbb7 100644
--- a/libmat2/images.py
+++ b/libmat2/images.py
@@ -5,7 +5,7 @@ import os
5import shutil 5import shutil
6import tempfile 6import tempfile
7import re 7import re
8from typing import Set 8from typing import Set, Dict, Union
9 9
10import cairo 10import cairo
11 11
@@ -25,7 +25,7 @@ class _ImageParser(abstract.AbstractParser):
25 meta_whitelist = set() # type: Set[str] 25 meta_whitelist = set() # type: Set[str]
26 26
27 @staticmethod 27 @staticmethod
28 def __handle_problematic_filename(filename: str, callback) -> str: 28 def __handle_problematic_filename(filename: str, callback) -> bytes:
29 """ This method takes a filename with a problematic name, 29 """ This method takes a filename with a problematic name,
30 and safely applies it a `callback`.""" 30 and safely applies it a `callback`."""
31 tmpdirname = tempfile.mkdtemp() 31 tmpdirname = tempfile.mkdtemp()
@@ -35,7 +35,7 @@ class _ImageParser(abstract.AbstractParser):
35 shutil.rmtree(tmpdirname) 35 shutil.rmtree(tmpdirname)
36 return out 36 return out
37 37
38 def get_meta(self): 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 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 40 self.filename to prevent parameter injections, so we need to take care
41 of this. 41 of this.
@@ -71,7 +71,7 @@ class PNGParser(_ImageParser):
71 except MemoryError: # pragma: no cover 71 except MemoryError: # pragma: no cover
72 raise ValueError 72 raise ValueError
73 73
74 def remove_all(self): 74 def remove_all(self) -> bool:
75 surface = cairo.ImageSurface.create_from_png(self.filename) 75 surface = cairo.ImageSurface.create_from_png(self.filename)
76 surface.write_to_png(self.output_filename) 76 surface.write_to_png(self.output_filename)
77 return True 77 return True
@@ -83,7 +83,12 @@ class GdkPixbufAbstractParser(_ImageParser):
83 """ 83 """
84 _type = '' 84 _type = ''
85 85
86 def remove_all(self): 86 def __init__(self, filename):
87 super().__init__(filename)
88 if imghdr.what(filename) != self._type: # better safe than sorry
89 raise ValueError
90
91 def remove_all(self) -> bool:
87 _, extension = os.path.splitext(self.filename) 92 _, extension = os.path.splitext(self.filename)
88 pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename) 93 pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename)
89 if extension.lower() == '.jpg': 94 if extension.lower() == '.jpg':
@@ -91,11 +96,6 @@ class GdkPixbufAbstractParser(_ImageParser):
91 pixbuf.savev(self.output_filename, extension[1:], [], []) 96 pixbuf.savev(self.output_filename, extension[1:], [], [])
92 return True 97 return True
93 98
94 def __init__(self, filename):
95 super().__init__(filename)
96 if imghdr.what(filename) != self._type: # better safe than sorry
97 raise ValueError
98
99 99
100class JPGParser(GdkPixbufAbstractParser): 100class JPGParser(GdkPixbufAbstractParser):
101 _type = 'jpeg' 101 _type = 'jpeg'