Skip to content
Merged
9 changes: 4 additions & 5 deletions src/AudioTagger.Console/OperationLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,14 @@ internal static class OperationLibrary
["-p", "--parse"],
"Get a single tag value by parsing the data of another (generally Comments).",
new TagParser()),
new(
["--cache-tags"],
"Cache files' tag data to a local JSON file whose path is specified in the settings.",
new TagCacher()),
new(
["--scan"],
"Ad-hoc maintenance scanning work. (Not intended for normal use.)",
new TagScanner(),
isHidden: true),
new(
["--cache-tags"],
"Cache files' tag data locally to a JSON file whose path is specified in the settings. (Eventually, this will be helpful in speeding up certain operations.)",
new TagCacher(),
isHidden: true)
];

Expand Down
41 changes: 17 additions & 24 deletions src/AudioTagger.Console/Operations/TagCacher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ namespace AudioTagger.Console.Operations;

public sealed class TagCacher : IPathOperation
{
private record TagSummary(
string[] Artists,
string Album,
uint TrackNo,
string Title,
uint Year,
string[] Genres,
TimeSpan Duration
);

public void Start(
IReadOnlyCollection<MediaFile> mediaFiles,
DirectoryInfo workingDirectory,
Expand All @@ -25,35 +15,38 @@ public void Start(
{
if (settings.TagCacheFilePath is null)
{
printer.Error("You must specify the save file path in the settings file.");
printer.Error("You must specify the save file path in the settings.");
return;
}

Watch watch = new();

var summaries = mediaFiles.Select(m => {
return new TagSummary(
m.Artists,
m.Album,
m.TrackNo,
m.Title,
m.Year,
m.Genres,
m.Duration
);
var summaries =
mediaFiles.Select(m => new
{
m.FileNameOnly,
m.FileInfo.DirectoryName,
m.Artists,
m.AlbumArtists,
m.Album,
m.TrackNo,
m.Title,
m.Year,
m.Genres,
m.Duration,
m.FileInfo.LastWriteTime,
});

printer.Print("Serializing the tags to JSON...");
printer.Print("Serializing tag data to JSON...");
JsonSerializerOptions options = new()
{
WriteIndented = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
var json = JsonSerializer.Serialize(summaries, options);
var unescapedJson = System.Text.RegularExpressions.Regex.Unescape(json); // Avoids `\0027`, etc.

printer.Print($"Saving cached tag data to \"{settings.TagCacheFilePath}\"...");
File.WriteAllText(settings.TagCacheFilePath, unescapedJson);
File.WriteAllText(settings.TagCacheFilePath, json);
printer.Print($"Saved in {watch.ElapsedFriendly}.");
}
}
10 changes: 5 additions & 5 deletions src/AudioTagger.Library/UpdatableFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public Dictionary<string, string> GetUpdateKeyValuePairs(MediaFile fileData)
{
var updateOutput = new Dictionary<string, string>();

if (Title != null && Title != fileData.Title)
{
updateOutput.Add("Title", Title);
}

if (AlbumArtists?.All(a => fileData.AlbumArtists.Contains(a)) == false)
{
updateOutput.Add("Album Artists", string.Join("; ", AlbumArtists));
Expand All @@ -141,11 +146,6 @@ public Dictionary<string, string> GetUpdateKeyValuePairs(MediaFile fileData)
updateOutput.Add("Artists", string.Join("; ", Artists));
}

if (Title != null && Title != fileData.Title)
{
updateOutput.Add("Title", Title);
}

if (Album != null && Album != fileData.Album)
{
updateOutput.Add("Album", Album);
Expand Down