From 22a1448372a93f89abb17f639693ac16d5451987 Mon Sep 17 00:00:00 2001 From: John Garland Date: Sat, 17 Jul 2010 17:11:19 +1000 Subject: [PATCH] Only use an icon if it passes some sanity checks --- deluge/ui/tracker_icons.py | 27 +++++++++++++++++++++++++++ tests/test_tracker_icons.py | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/deluge/ui/tracker_icons.py b/deluge/ui/tracker_icons.py index 298e21cc0..91a02e46e 100644 --- a/deluge/ui/tracker_icons.py +++ b/deluge/ui/tracker_icons.py @@ -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 diff --git a/tests/test_tracker_icons.py b/tests/test_tracker_icons.py index fd86d7b50..ecf5ddb73 100644 --- a/tests/test_tracker_icons.py +++ b/tests/test_tracker_icons.py @@ -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