Only use an icon if it passes some sanity checks

This commit is contained in:
John Garland 2010-07-17 17:11:19 +10:00
parent 722a5cd9e1
commit 22a1448372
2 changed files with 28 additions and 1 deletions

View File

@ -315,10 +315,34 @@ class TrackerIcons(Component):
(url, mimetype) = icons.pop(0)
d = download_file(url, os.path.join(self.dir, host_to_icon_name(host, mimetype)),
force_filename=True)
d.addCallback(self.check_icon_is_valid)
if icons:
d.addErrback(self.on_download_icon_fail, host, icons)
return d
@proxy(threads.deferToThread)
def check_icon_is_valid(self, icon_name):
"""
Performs a sanity check on icon_name
:param icon_name: the name of the icon to check
:type icon_name: string
:returns: the name of the validated icon
:rtype: string
:raises: InvalidIconError
"""
if PIL_INSTALLED:
try:
Image.open(icon_name)
except IOError, e:
raise InvalidIconError(e)
else:
if os.stat(icon_name).st_size == 0L:
raise InvalidIconError, "empty icon"
return icon_name
def on_download_icon_complete(self, icon_name, host):
"""
Runs any download cleanup functions
@ -547,3 +571,6 @@ def extension_to_mimetype(extension):
class NoIconsError(Exception):
pass
class InvalidIconError(Exception):
pass

View File

@ -63,5 +63,5 @@ class TrackerIconsTestCase(unittest.TestCase):
def test_get_empty_string_tracker(self):
d = icons.get("")
d.addCallback(self.assertEquals, None)
d.addCallback(self.assertIdentical, None)
return d