[f-spot] Fix two potential problems with thumbnailing.



commit 09827d73e29d0bb9a1ca70f5aca0f43d568a370f
Author: Ruben Vermeersch <ruben savanne be>
Date:   Tue Jun 15 22:22:41 2010 +0200

    Fix two potential problems with thumbnailing.
    
    Crashes could happen when:
    
     * The file was not found
     * The thumbnail is not loadable
    
    Added small test case to validate that the first case is fixed. The
    second case is harder to test.

 src/Utils/Makefile.am                    |    3 ++-
 src/Utils/Tests/XdgThumbnailSpecTests.cs |   30 ++++++++++++++++++++++++++++++
 src/Utils/XdgThumbnailSpec.cs            |   23 +++++++++++++++++++----
 3 files changed, 51 insertions(+), 5 deletions(-)
---
diff --git a/src/Utils/Makefile.am b/src/Utils/Makefile.am
index ce7ba7c..7ea4608 100644
--- a/src/Utils/Makefile.am
+++ b/src/Utils/Makefile.am
@@ -19,7 +19,8 @@ SOURCES = \
 	UriExtensions.cs \
 	UriUtils.cs \
 	XdgThumbnailSpec.cs \
-	Tests/SafeUriTests.cs
+	Tests/SafeUriTests.cs \
+	Tests/XdgThumbnailSpecTests.cs
 
 RESOURCES =
 
diff --git a/src/Utils/Tests/XdgThumbnailSpecTests.cs b/src/Utils/Tests/XdgThumbnailSpecTests.cs
new file mode 100644
index 0000000..8e97064
--- /dev/null
+++ b/src/Utils/Tests/XdgThumbnailSpecTests.cs
@@ -0,0 +1,30 @@
+#if ENABLE_TESTS
+using NUnit.Framework;
+using System;
+using Hyena;
+using FSpot;
+
+namespace FSpot.Utils.Tests
+{
+    [TestFixture]
+    public class XdgThumbnailSpecTests
+	{
+		[SetUp]
+        public void Initialize () {
+            GLib.GType.Init ();
+        }
+
+		[Test]
+		public void TestMissingFile ()
+		{
+			XdgThumbnailSpec.DefaultLoader = (u) => {
+				throw new Exception ("not found!");
+			};
+
+			var uri = new SafeUri ("file:///invalid");
+			var pixbuf = XdgThumbnailSpec.LoadThumbnail (uri, ThumbnailSize.Large);
+			Assert.IsNull (pixbuf);
+		}
+	}
+}
+#endif
diff --git a/src/Utils/XdgThumbnailSpec.cs b/src/Utils/XdgThumbnailSpec.cs
index 1d0850f..b31a385 100644
--- a/src/Utils/XdgThumbnailSpec.cs
+++ b/src/Utils/XdgThumbnailSpec.cs
@@ -63,7 +63,14 @@ namespace FSpot.Utils
         private static Pixbuf CreateFrom (SafeUri uri, SafeUri thumb_uri, ThumbnailSize size, PixbufLoader loader)
         {
             var pixels = size == ThumbnailSize.Normal ? 128 : 256;
-            var pixbuf = loader (uri); 
+            Pixbuf pixbuf;
+            try {
+                pixbuf = loader (uri);
+            } catch (Exception e) {
+                Log.DebugFormat ("Failed loading image for thumbnailing: {0}", uri);
+                Log.DebugException (e);
+                return null;
+            }
             double scale_x = (double) pixbuf.Width / pixels;
             double scale_y = (double) pixbuf.Height / pixels;
             double scale = Math.Max (1.0, Math.Max (scale_x, scale_y));
@@ -94,9 +101,17 @@ namespace FSpot.Utils
             var file = GLib.FileFactory.NewForUri (uri);
             if (!file.Exists)
                 return null;
-            var stream = new GLib.GioStream (file.Read (null)); 
-            var pixbuf = new Pixbuf (stream);
-            stream.Close ();
+            Pixbuf pixbuf;
+            using (var stream = new GLib.GioStream (file.Read (null))) {
+                try {
+                    pixbuf = new Pixbuf (stream);
+                } catch (Exception e) {
+                    file.Delete ();
+                    Log.DebugFormat ("Failed thumbnail: {0}", uri);
+                    Log.DebugException (e);
+                    return null;
+                }
+            }
             return pixbuf;
         }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]