diff --git a/Damselfly.Core.DbModels/Models/Entities/ImageMetaData.cs b/Damselfly.Core.DbModels/Models/Entities/ImageMetaData.cs index 20681c57..9cbe0e5f 100644 --- a/Damselfly.Core.DbModels/Models/Entities/ImageMetaData.cs +++ b/Damselfly.Core.DbModels/Models/Entities/ImageMetaData.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel.DataAnnotations; +using System.Runtime.CompilerServices; namespace Damselfly.Core.Models; @@ -51,4 +52,28 @@ public class ImageMetaData // Date we last performed face/object/image recognition // If this is null, AI will be reprocessed public DateTime? AILastUpdated { get; set; } + + public void Clear() + { + this.DateTaken = DateTime.MinValue; + this.Height = 0; + this.Width = 0; + this.Description = null; + this.Caption = null; + this.DominantColor = null; + this.AspectRatio = 1; + this.Rating = 0; + this.Credit = null; + this.ISO = null; + this.FNum = null; + this.Exposure = null; + this.FNum = null; + this.FlashFired = false; + this.Latitude = null; + this.Longitude = null; + this.CameraId = 0; + this.Camera = null; + this.LensId = 0; + this.Lens = null; + } } \ No newline at end of file diff --git a/Damselfly.Core/Services/MetaDataService.cs b/Damselfly.Core/Services/MetaDataService.cs index 414c3032..5ac4a02f 100644 --- a/Damselfly.Core/Services/MetaDataService.cs +++ b/Damselfly.Core/Services/MetaDataService.cs @@ -185,7 +185,7 @@ public void StartService() _workService.AddJobSource(this); } - + /// /// Read the metadata, and handle any exceptions. /// @@ -216,6 +216,20 @@ private IReadOnlyList SafeReadImageMetadata(string imagePath) return metadata; } + private string? GetXMPFieldValue( Dictionary kvps, string prefix ) + { + if( kvps.TryGetValue( prefix, out var result ) && ! string.IsNullOrEmpty(result)) + return result; + + var possibilities = Enumerable.Range(1, 5).Select( n => $"{prefix}[{n}]"); + + foreach( var key in possibilities ) + if( kvps.TryGetValue( key, out var indexed ) && ! string.IsNullOrEmpty(indexed) ) + return indexed; + + return null; + } + /// /// Pull out the XMP attributes /// @@ -227,18 +241,19 @@ private void ReadXMPData(XmpDirectory xmpDirectory, Image image) { var nvps = xmpDirectory.XmpMeta.Properties .Where(x => !string.IsNullOrEmpty(x.Path)) - .ToDictionary(x => x.Path, y => y.Value); + .ToDictionary(x => x.Path, y => y.Value, StringComparer.OrdinalIgnoreCase); if( image.MetaData.DateTaken == DateTime.MinValue ) { if( nvps.ContainsKey("exif:DateTimeOriginal") && DateTime.TryParse(nvps["exif:DateTimeOriginal"], out var dateTime) ) image.MetaData.DateTaken = dateTime; } - if( string.IsNullOrEmpty(image.MetaData.Description) && nvps.ContainsKey("exif:Description")) - image.MetaData.Description = nvps["exif:Description"]; - - if( string.IsNullOrEmpty(image.MetaData.Caption) && nvps.ContainsKey("exif:Caption") ) - image.MetaData.Caption = nvps["exif:Caption"]; + + if( string.IsNullOrEmpty(image.MetaData.Description) ) + image.MetaData.Description = GetXMPFieldValue( nvps, "dc:Description" ); + + if( string.IsNullOrEmpty(image.MetaData.Caption) ) + image.MetaData.Caption = GetXMPFieldValue( nvps, "dc:Caption" ); } catch ( Exception ex ) { @@ -359,6 +374,9 @@ private bool GetImageMetaData(ref ImageMetaData imgMetaData, out string[] keywor if ( metadata != null ) { + // We've read some EXIF data, so clear out the existing metadata to replace it + imgMetaData.Clear(); + metaDataReadSuccess = true; var subIfdDirectory = metadata.OfType().FirstOrDefault();