Skip to content

Commit

Permalink
Add better message when files are rejected, improves #11
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMoolenaar committed Mar 7, 2024
1 parent 9149a37 commit c96a1ad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
7 changes: 4 additions & 3 deletions MatroskaLib/MatroskaLib/MkvFilesContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ namespace MatroskaLib;
public class MkvFilesContainer
{
public readonly List<MkvFile> MkvFiles = new();
public readonly List<MkvFile> MkFilesRejected = new();
public readonly List<(MkvFile file, string error)> MkFilesRejected = new();

public MkvFilesContainer(string[] filePaths)
{
var files = MatroskaReader.ReadMkvFiles(filePaths);
MkvFiles.Add(files[0]);
for (int i = 1; i < files.Count; i++)
{
if (files[0].CompareTo(files[i]) == 0)
string? error = files[0].CompareToGetError(files[i]);
if (error is null)
MkvFiles.Add(files[i]);
else
MkFilesRejected.Add(files[i]);
MkFilesRejected.Add((files[i], error));
}
}

Expand Down
15 changes: 10 additions & 5 deletions MatroskaLib/MatroskaLib/Types/MkvFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace MatroskaLib.Types;

public record MkvFile : IComparable<MkvFile>
public record MkvFile
{
public required string filePath { get; init; }
public required List<Track> tracks { get; init; }
Expand All @@ -18,7 +18,7 @@ public record MkvFile : IComparable<MkvFile>
public required int tracksPosition { get; init; }
public required int beginHeaderPosition { get; init; }

public int CompareTo(MkvFile? other)
public string? CompareToGetError(MkvFile? other)
{
if (other is null)
throw new ArgumentNullException(nameof(other));
Expand All @@ -27,11 +27,16 @@ public int CompareTo(MkvFile? other)
{
var track = tracks[i];
var trackOther = other.tracks.ElementAtOrDefault(i);
if (trackOther is null || track.number != trackOther.number || track.language != trackOther.language)
return -1;

if (trackOther is null)
return $"Track at index {i} does not exist, expected {track.type}{track.language}.";
if (track.number != trackOther.number)
return $"Track number {i} does not match. Expected {track.number}, got {trackOther.number}.";
if (track.language != trackOther.language)
return $"Track language {i} does not match. Expected {track.language}, got {trackOther.language}.";
}

return 0;
return null;
}

public override string ToString() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ private void LoadFiles()
mkvContainer = new MkvFilesContainer(filePaths);
if (mkvContainer.MkFilesRejected.Count > 0)
{
var sourceFile = Path.GetFileName(filePaths[0]);

string rejectedFiles = Environment.NewLine + Environment.NewLine;
mkvContainer.MkFilesRejected.ForEach((x) =>
{
rejectedFiles += Path.GetFileName(x.filePath) + Environment.NewLine + Environment.NewLine;
rejectedFiles += $"- {Path.GetFileName(x.file.filePath)}: {x.error} {Environment.NewLine}{Environment.NewLine}";
});
MessageBox.Show("The following files were rejected: " + rejectedFiles, MessageBoxType.Warning);
MessageBox.Show($"The following files have different tracks or the order is different than {sourceFile}: {rejectedFiles}These files cannot be processed.",
MessageBoxType.Warning);
}

var lsSubtitleTracks = mkvContainer.GetSubtitleTracks();
Expand Down

0 comments on commit c96a1ad

Please sign in to comment.