Module: deluge
Branch: 1.3-stable
Commit: 1ce480ff2390bb5a60dd5e32ee012051dac81f0d

Author: John Garland <[email protected]>
Date:   Sat Jul 17 17:11:19 2010 +1000

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 deletions(-)

diff --git a/deluge/ui/tracker_icons.py b/deluge/ui/tracker_icons.py
index 298e21c..91a02e4 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 9e69f2f..61a5565 100644
--- a/tests/test_tracker_icons.py
+++ b/tests/test_tracker_icons.py
@@ -62,5 +62,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

-- 
You received this message because you are subscribed to the Google Groups 
"deluge-commit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/deluge-commit?hl=en.

Reply via email to