Skip to content

Commit

Permalink
Make MetadataParser.TryGetFileMetadata take in a stream instead of a …
Browse files Browse the repository at this point in the history
…file path
  • Loading branch information
DrSmugleaf committed Nov 2, 2023
1 parent da670af commit 209b43d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
11 changes: 0 additions & 11 deletions SpaceWizards.RsiLib/DMI/Metadata/IMetadataParser.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public static class MetadataParseErrorsExtensions
{
public static ParseError WithMessage(this MetadataErrors error, string message)
{
return new(error, message);
return new ParseError(error, message);
}
}
}
17 changes: 9 additions & 8 deletions SpaceWizards.RsiLib/DMI/Metadata/MetadataParser.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using MetadataExtractor.Formats.Png;
using static SpaceWizards.RsiLib.DMI.Metadata.MetadataErrors;

namespace SpaceWizards.RsiLib.DMI.Metadata;

public class MetadataParser : IMetadataParser
public class MetadataParser
{
private const string Header = "BEGIN DMI";

private bool TryGetFileDmiTag(string filePath, [NotNullWhen(true)] out RawMetadata? rawData)
private bool TryGetFileDmiTag(Stream stream, [NotNullWhen(true)] out RawMetadata? rawData)
{
var data = PngMetadataReader.ReadMetadata(filePath);
var data = PngMetadataReader.ReadMetadata(stream);

foreach (var datum in data)
{
Expand All @@ -32,21 +33,21 @@ private bool TryGetFileDmiTag(string filePath, [NotNullWhen(true)] out RawMetada
}

public bool TryGetFileMetadata(
string filePath,
Stream stream,
[NotNullWhen(true)] out IMetadata? metadata,
[NotNullWhen(false)] out ParseError? error)
{
if (!TryGetFileDmiTag(filePath, out var raw))
if (!TryGetFileDmiTag(stream, out var raw))
{
metadata = null;
error = NoDmiTag.WithMessage($"No dmi tag found in file {filePath}");
error = NoDmiTag.WithMessage("No dmi tag found");
return false;
}

if (!raw.Next() || !raw.TryVersion(out var version))
{
metadata = null;
error = NoVersion.WithMessage($"No version found in file {filePath}");
error = NoVersion.WithMessage("No version found");
return false;
}

Expand All @@ -64,4 +65,4 @@ public bool TryGetFileMetadata(
error = null;
return true;
}
}
}

0 comments on commit 209b43d

Please sign in to comment.