public string GetCleanFileName(NamingStrategy strategy, string calcedAlbumArtist, int trackWidth) { System.Diagnostics.Debug.Assert(strategy == NamingStrategy.Manual || trackWidth > 0); string dirtyName; bool isVarious = calcedAlbumArtist == null || (calcedAlbumArtist.ToLower() + ' ').StartsWith("various "); var track = GetTag("TRACKNUMBER"); var padding = trackWidth - track.Length; if (padding > 0) { track = new String('0', padding) + track; } var artist = GetTag("ARTIST"); if (String.IsNullOrEmpty(artist)) { artist = GetTag("COMPOSER"); if (String.IsNullOrEmpty(artist)) { artist = "(NoArtist)"; } } var title = GetTag("TITLE"); if (String.IsNullOrEmpty(title)) { title = "(NoTitle)"; } switch (strategy) { case NamingStrategy.ArtistTitle: dirtyName = track + " - " + artist + " - " + title + ".flac"; break; case NamingStrategy.ShortTitle: case NamingStrategy.UnloadedAlbum: if (isVarious || artist != calcedAlbumArtist) { dirtyName = track + " - " + artist + " - " + title + ".flac"; } else { dirtyName = track + " - " + title + ".flac"; } break; default: dirtyName = Name; break; } return(Map1252.ToClean1252FileName(dirtyName)); }
string IDataErrorInfo.this[string propName] { get { if (propName == "UserSig") { string cleanSig = Map1252.ToClean1252FileName(UserSig); if (cleanSig != null && (cleanSig != UserSig || cleanSig.Any(Char.IsWhiteSpace))) { return("cannot have spaces or characters not allowed in file names"); } } return(null); } }
public void Test_ToClean1252FileName1() { var aZ = Map1252.ToClean1252FileName("aZ"); var c1ControlSpots = Map1252.ToClean1252FileName("€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ"); var quote = Map1252.ToClean1252FileName("\""); var heart = Map1252.ToClean1252FileName("❤"); var question = Map1252.ToClean1252FileName("?"); var strokedH = Map1252.ToClean1252FileName("Ħ"); var backslash = Map1252.ToClean1252FileName("\\"); var spacedSlash = Map1252.ToClean1252FileName("foo / bar.mp3"); var notFileChars = Map1252.ToClean1252FileName("<>"); var fullwidth = Map1252.ToClean1252FileName("Azfullwidth\U0001F175astral"); Assert.AreEqual("aZ", aZ); Assert.AreEqual("€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ", c1ControlSpots); Assert.AreEqual("'", quote); Assert.AreEqual("-", heart); Assert.AreEqual("", question); Assert.AreEqual("H", strokedH); Assert.AreEqual("-", backslash); Assert.AreEqual("foo; bar.mp3", spacedSlash); Assert.AreEqual("‹›", notFileChars); Assert.AreEqual("AzfullwidthFastral", fullwidth); }
public Severity ValidateLameRip(string arg, string signature, bool doLogTag) { string err = null; string newPath = null; if (signature != null) { if (signature.Length == 0) { signature = null; } else if (Map1252.ToClean1252FileName(signature.Trim(null)) != signature || signature.Any(Char.IsWhiteSpace)) { ReportLine($"Invalid signature '{signature}'.", Severity.Error, false); return(Severity.Fatal); } } try { var fInfo = new FileInfo(arg); if (fInfo.Attributes.HasFlag(FileAttributes.Directory)) { newPath = fInfo.FullName; } else { err = "Not a directory."; } } catch (ArgumentException ex) { err = ex.Message.Trim(null); } catch (DirectoryNotFoundException ex) { err = ex.Message.Trim(null); } catch (IOException ex) { err = ex.Message.Trim(null); } catch (NotSupportedException) { err = "Path is not valid."; } RipModel = new LameRip.Model(this, newPath, signature, doLogTag); if (err != null) { ReportLine(err, Severity.Error, false); SetCurrentFile(null); RipModel.SetStatus(Severity.Error); return(Severity.Error); } RipModel.Bind.IsWip = true; try { RipModel.Validate(); } catch (IOException ex) { err = ex.Message.Trim(null); ReportLine(err); SetCurrentFile(null); RipModel.SetStatus(Severity.Fatal); return(Severity.Fatal); } SetCurrentFile(null); if (RipModel.Bind.Status == Severity.NoIssue && RipModel.Bind.Log == null) { if (RipModel.Bind.Signature != null) { try { var errPath = newPath + Path.DirectorySeparatorChar + Data.NoncompliantName; if (File.Exists(errPath)) { File.Delete(errPath); } } catch (Exception) { /* discard all */ } } RipModel.Bind.IsWip = false; ReportLine(RipModel.Bind.Trailer, Severity.Trivia, false); return(Severity.Trivia); } if (RipModel.Bind.Status >= Severity.Error) { RipModel.CloseFiles(); var baseName = Path.GetFileName(newPath); if (RipModel.Bind.Signature != null && !baseName.StartsWith("!!") && newPath.Length < 235) { var errPath = Directory.GetParent(newPath).FullName; if (errPath.Length > 0 && errPath[errPath.Length - 1] != Path.DirectorySeparatorChar) { errPath += Path.DirectorySeparatorChar; } errPath += Data.FailPrefix + baseName; try { Directory.Move(newPath, errPath); } catch (Exception) { /* discard all */ } ++consecutiveInvalidations; } } if (!RipModel.Bind.IsWip) { ReportLine(RipModel.Bind.Trailer, RipModel.Bind.Status, false); } return(RipModel.Bind.Status); }