private string GetNewName(Id3Tag tag, string originalName, out string missingFrameName) { missingFrameName = null; string missingFrame = null; //Make two passes of the patterns. //In the first pass, we try to find the perfect match without resorting to the //ResolveMissingData event. //If we still don't have a match, in the second pass, we use the ResolveMissingData event. for (var i = 0; i < 2; i++) { foreach (string pattern in _patterns) { var hasMissingFrames = false; int iteration = i; string newName = FramePlaceholderPattern.Replace(pattern, match => { //If this pattern already has missing frames, don't proces anything if (hasMissingFrames) { return(string.Empty); } string frameName = match.Groups[1].Value; PropertyInfo frameProperty = _mapping[frameName]; //Because all frame properties in Id3Tag are lazily-loaded, this will never be null var frame = (Id3Frame)frameProperty.GetValue(tag, null); if (frame.IsAssigned) { return(frame.ToString()); } if (iteration == 1) { string frameValue = FireResolveMissingDataEvent(tag, frame, originalName); if (!string.IsNullOrWhiteSpace(frameValue)) { return(frameValue); } } hasMissingFrames = true; missingFrame = frameName; return(string.Empty); }); if (!hasMissingFrames) { return(newName); } } } missingFrameName = missingFrame; return(null); }
private IEnumerable <T> RenameOrSuggest <T>(IEnumerable <string> filePaths) where T : RenameAction, new() { foreach (string filePath in filePaths) { if (string.IsNullOrWhiteSpace(filePath)) { yield return(new T { Directory = filePath, OriginalName = filePath, Status = RenameStatus.Error, ErrorMessage = Id3FileMessages.InvalidFilePath }); continue; } if (!File.Exists(filePath)) { yield return(new T { Directory = filePath, OriginalName = filePath, Status = RenameStatus.Error, ErrorMessage = Id3FileMessages.MissingFile }); continue; } var result = new T { Directory = Path.GetDirectoryName(filePath), OriginalName = Path.GetFileName(filePath) }; using (var mp3 = new Mp3File(filePath)) { Id3Tag tag = mp3.GetTag(2, 3); if (tag == null) { result.Status = RenameStatus.Error; result.ErrorMessage = Id3FileMessages.MissingId3v23TagInFile; yield return(result); continue; } string missingFrameName = null; string newName = FramePlaceholderPattern.Replace(_pattern, match => { string frameName = match.Groups[1].Value; string frameNameKey = frameName.ToLowerInvariant(); PropertyInfo frameProperty = _mapping[frameNameKey]; var frame = (Id3Frame)frameProperty.GetValue(tag, null); string frameValue = frame.IsAssigned ? frame.ToString() : FireResolveMissingDataEvent(tag, frame, result.OriginalName); if (string.IsNullOrEmpty(frameValue)) { missingFrameName = frameName; } return(frameValue); }); if (missingFrameName != null) { result.Status = RenameStatus.Error; result.ErrorMessage = string.Format(Id3FileMessages.MissingDataForFrame, missingFrameName); yield return(result); continue; } result.NewName = newName + ".mp3"; RenamingEventArgs renamingEventResult = FireRenamingEvent(tag, result.OriginalName, result.NewName); if (renamingEventResult.Cancel) { result.Status = RenameStatus.Cancelled; } else { result.NewName = renamingEventResult.NewName; } if (result.OriginalName.Equals(result.NewName, StringComparison.Ordinal)) { result.Status = RenameStatus.CorrectlyNamed; } yield return(result); } } }