Skip to content

Commit

Permalink
Fix for metadata #507
Browse files Browse the repository at this point in the history
  • Loading branch information
Webreaper committed Mar 12, 2024
1 parent 8a74541 commit 9dd4b44
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
25 changes: 25 additions & 0 deletions Damselfly.Core.DbModels/Models/Entities/ImageMetaData.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;

namespace Damselfly.Core.Models;

Expand Down Expand Up @@ -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;

Check warning on line 75 in Damselfly.Core.DbModels/Models/Entities/ImageMetaData.cs

View workflow job for this annotation

GitHub Actions / Analyze (javascript)

Cannot convert null literal to non-nullable reference type.

Check warning on line 75 in Damselfly.Core.DbModels/Models/Entities/ImageMetaData.cs

View workflow job for this annotation

GitHub Actions / build-server (windows)

Cannot convert null literal to non-nullable reference type.

Check warning on line 75 in Damselfly.Core.DbModels/Models/Entities/ImageMetaData.cs

View workflow job for this annotation

GitHub Actions / build-server (linux)

Cannot convert null literal to non-nullable reference type.

Check warning on line 75 in Damselfly.Core.DbModels/Models/Entities/ImageMetaData.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Cannot convert null literal to non-nullable reference type.
this.LensId = 0;
this.Lens = null;

Check warning on line 77 in Damselfly.Core.DbModels/Models/Entities/ImageMetaData.cs

View workflow job for this annotation

GitHub Actions / Analyze (javascript)

Cannot convert null literal to non-nullable reference type.

Check warning on line 77 in Damselfly.Core.DbModels/Models/Entities/ImageMetaData.cs

View workflow job for this annotation

GitHub Actions / build-server (windows)

Cannot convert null literal to non-nullable reference type.

Check warning on line 77 in Damselfly.Core.DbModels/Models/Entities/ImageMetaData.cs

View workflow job for this annotation

GitHub Actions / build-server (linux)

Cannot convert null literal to non-nullable reference type.

Check warning on line 77 in Damselfly.Core.DbModels/Models/Entities/ImageMetaData.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Cannot convert null literal to non-nullable reference type.
}
}
32 changes: 25 additions & 7 deletions Damselfly.Core/Services/MetaDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void StartService()

_workService.AddJobSource(this);
}

/// <summary>
/// Read the metadata, and handle any exceptions.
/// </summary>
Expand Down Expand Up @@ -216,6 +216,20 @@ private IReadOnlyList<Directory> SafeReadImageMetadata(string imagePath)
return metadata;
}

private string? GetXMPFieldValue( Dictionary<string, string> 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;
}

/// <summary>
/// Pull out the XMP attributes
/// </summary>
Expand All @@ -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 )
{
Expand Down Expand Up @@ -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<ExifSubIfdDirectory>().FirstOrDefault();
Expand Down

0 comments on commit 9dd4b44

Please sign in to comment.