[f-spot] Make Metadata and Sidecar file parsing more reliable



commit fac7e489f312680434383de8790d540cdfdf82e3
Author: Mike Gemünde <mike gemuende de>
Date:   Tue Jul 20 09:03:08 2010 +0200

    Make Metadata and Sidecar file parsing more reliable
    
    The application should not crash when reading metadata or sidecar files fail.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=624794

 src/Imaging/ImageFile.cs          |    4 ++-
 src/Imaging/Tiff.cs               |   20 ++++++++++++---
 src/Utils/Metadata.cs             |    9 ++++++-
 src/Utils/SidecarXmpExtensions.cs |   49 +++++++++++++++++++++++++++----------
 4 files changed, 63 insertions(+), 19 deletions(-)
---
diff --git a/src/Imaging/ImageFile.cs b/src/Imaging/ImageFile.cs
index aa674d5..013d697 100644
--- a/src/Imaging/ImageFile.cs
+++ b/src/Imaging/ImageFile.cs
@@ -186,12 +186,14 @@ namespace FSpot.Imaging {
             Orientation = ImageOrientation.TopLeft;
 
             using (var metadata_file = Metadata.Parse (uri)) {
-                Orientation = metadata_file.ImageTag.Orientation;
                 ExtractMetadata (metadata_file);
             }
         }
 
         protected virtual void ExtractMetadata (TagLib.Image.File metadata) {
+            if (metadata != null) {
+                Orientation = metadata.ImageTag.Orientation;
+            }
         }
 
 		~BaseImageFile ()
diff --git a/src/Imaging/Tiff.cs b/src/Imaging/Tiff.cs
index 6958820..ac3de7c 100644
--- a/src/Imaging/Tiff.cs
+++ b/src/Imaging/Tiff.cs
@@ -109,6 +109,11 @@ namespace FSpot.Imaging.Tiff {
 
 		protected override void ExtractMetadata (TagLib.Image.File metadata)
 		{
+            base.ExtractMetadata (metadata);
+
+            if (metadata == null)
+                return;
+
 			try {
 				var tag = metadata.GetTag (TagTypes.TiffIFD) as IFDTag;
 				var structure = tag.Structure;
@@ -119,7 +124,6 @@ namespace FSpot.Imaging.Tiff {
 			} catch (Exception e) {
 				Log.DebugException (e);
 			}
-			base.ExtractMetadata (metadata);
 		}
 
 		public override System.IO.Stream PixbufStream ()
@@ -132,7 +136,7 @@ namespace FSpot.Imaging.Tiff {
 				return DCRawFile.RawPixbufStream (Uri);
 			}
 		}
-	}	
+	}
 	
 	public class NefFile : BaseImageFile {
 		byte [] jpeg_data;
@@ -143,6 +147,11 @@ namespace FSpot.Imaging.Tiff {
 
 		protected override void ExtractMetadata (TagLib.Image.File metadata)
 		{
+            base.ExtractMetadata (metadata);
+
+            if (metadata == null)
+                return;
+
 			try {
 				var tag = metadata.GetTag (TagTypes.TiffIFD) as IFDTag;
 				var structure = tag.Structure;
@@ -153,7 +162,6 @@ namespace FSpot.Imaging.Tiff {
 				Log.DebugException (e);
 				jpeg_data = null;
 			}
-			base.ExtractMetadata (metadata);
 		}
 
 		public override System.IO.Stream PixbufStream ()
@@ -176,6 +184,11 @@ namespace FSpot.Imaging.Tiff {
 
 		protected override void ExtractMetadata (TagLib.Image.File metadata)
 		{
+            base.ExtractMetadata (metadata);
+
+            if (metadata == null)
+                return;
+
 			try {
 				var tag = metadata.GetTag (TagTypes.TiffIFD) as IFDTag;
 				var structure = tag.Structure;
@@ -184,7 +197,6 @@ namespace FSpot.Imaging.Tiff {
 			} catch (Exception e) {
 				Log.DebugException (e);
 			}
-			base.ExtractMetadata (metadata);
 		}
 
 		public override System.IO.Stream PixbufStream ()
diff --git a/src/Utils/Metadata.cs b/src/Utils/Metadata.cs
index 882b818..325f311 100644
--- a/src/Utils/Metadata.cs
+++ b/src/Utils/Metadata.cs
@@ -18,7 +18,14 @@ namespace FSpot.Utils
             var res = new GIOTagLibFileAbstraction () { Uri = uri };
             var sidecar_uri = uri.ReplaceExtension (".xmp");
             var sidecar_res = new GIOTagLibFileAbstraction () { Uri = sidecar_uri };
-            var file = TagLib.File.Create (res, mime, ReadStyle.Average) as TagLib.Image.File;
+
+            TagLib.Image.File file = null;
+            try {
+                file = TagLib.File.Create (res, mime, ReadStyle.Average) as TagLib.Image.File;
+            } catch (Exception e) {
+                Hyena.Log.Exception (String.Format ("Loading of Metadata failed for file: {0}", uri.ToString ()), e);
+                return null;
+            }
 
             // Load XMP sidecar
             var sidecar_file = GLib.FileFactory.NewForUri (sidecar_uri);
diff --git a/src/Utils/SidecarXmpExtensions.cs b/src/Utils/SidecarXmpExtensions.cs
index aac3f11..a094585 100644
--- a/src/Utils/SidecarXmpExtensions.cs
+++ b/src/Utils/SidecarXmpExtensions.cs
@@ -13,35 +13,58 @@ namespace FSpot.Utils
         ///    Parses the XMP file identified by resource and replaces the XMP
         ///    tag of file by the parsed data.
         /// </summary>
-        public static void ParseXmpSidecar (this TagLib.Image.File file, TagLib.File.IFileAbstraction resource)
+        public static bool ParseXmpSidecar (this TagLib.Image.File file, TagLib.File.IFileAbstraction resource)
         {
             string xmp;
-            using (var stream = resource.ReadStream) {
-                using (var reader = new StreamReader (stream)) {
-                    xmp = reader.ReadToEnd ();
+
+            try {
+                using (var stream = resource.ReadStream) {
+                    using (var reader = new StreamReader (stream)) {
+                        xmp = reader.ReadToEnd ();
+                    }
                 }
+            } catch (Exception e) {
+                Hyena.Log.Exception (String.Format ("Sidecar cannot be read for file {0}", file.Name), e);
+                return false;
+            }
+
+            XmpTag tag = null;
+            try {
+                tag = new XmpTag (xmp);
+            } catch (Exception e) {
+                Hyena.Log.Exception (String.Format ("Metadata of Sidecar cannot be parsed for file {0}", file.Name), e);
+                return false;
             }
 
-            var tag = new XmpTag (xmp);
             var xmp_tag = file.GetTag (TagLib.TagTypes.XMP, true) as XmpTag;
             xmp_tag.ReplaceFrom (tag);
+            return true;
         }
 
-        public static void SaveXmpSidecar (this TagLib.Image.File file, TagLib.File.IFileAbstraction resource)
+        public static bool SaveXmpSidecar (this TagLib.Image.File file, TagLib.File.IFileAbstraction resource)
         {
             var xmp_tag = file.GetTag (TagLib.TagTypes.XMP, false) as XmpTag;
-            if (xmp_tag == null)
-                return;
+            if (xmp_tag == null) {
+                // TODO: Delete File
+                return true;
+            }
 
             var xmp = xmp_tag.Render ();
 
-            using (var stream = resource.WriteStream) {
-                stream.SetLength (0);
-                using (var writer = new StreamWriter (stream)) {
-                    writer.Write (xmp);
+            try {
+                using (var stream = resource.WriteStream) {
+                    stream.SetLength (0);
+                    using (var writer = new StreamWriter (stream)) {
+                        writer.Write (xmp);
+                    }
+                    resource.CloseStream (stream);
                 }
-                resource.CloseStream (stream);
+            } catch (Exception e) {
+                Hyena.Log.Exception (String.Format ("Sidecar cannot be saved: {0}", resource.Name), e);
+                return false;
             }
+
+            return true;
         }
     }
 }



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