| | 1 | | using System; |
| | 2 | | using System.Globalization; |
| | 3 | | using System.IO; |
| | 4 | | using System.Text.RegularExpressions; |
| | 5 | | using Emby.Naming.Common; |
| | 6 | | using Jellyfin.Extensions; |
| | 7 | |
|
| | 8 | | namespace Emby.Naming.Audio |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Helper class to determine if Album is multipart. |
| | 12 | | /// </summary> |
| | 13 | | public partial class AlbumParser |
| | 14 | | { |
| | 15 | | private readonly NamingOptions _options; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Initializes a new instance of the <see cref="AlbumParser"/> class. |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="options">Naming options containing AlbumStackingPrefixes.</param> |
| | 21 | | public AlbumParser(NamingOptions options) |
| | 22 | | { |
| 31 | 23 | | _options = options; |
| 31 | 24 | | } |
| | 25 | |
|
| | 26 | | [GeneratedRegex(@"[-\.\(\)\s]+")] |
| | 27 | | private static partial Regex CleanRegex(); |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Function that determines if album is multipart. |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="path">Path to file.</param> |
| | 33 | | /// <returns>True if album is multipart.</returns> |
| | 34 | | public bool IsMultiPart(string path) |
| | 35 | | { |
| 31 | 36 | | var filename = Path.GetFileName(path); |
| 31 | 37 | | if (filename.Length == 0) |
| | 38 | | { |
| 3 | 39 | | return false; |
| | 40 | | } |
| | 41 | |
|
| | 42 | | // TODO: Move this logic into options object |
| | 43 | | // Even better, remove the prefixes and come up with regexes |
| | 44 | | // But Kodi documentation seems to be weak for audio |
| | 45 | |
|
| | 46 | | // Normalize |
| | 47 | | // Remove whitespace |
| 28 | 48 | | filename = CleanRegex().Replace(filename, " "); |
| | 49 | |
|
| 28 | 50 | | ReadOnlySpan<char> trimmedFilename = filename.AsSpan().TrimStart(); |
| | 51 | |
|
| 299 | 52 | | foreach (var prefix in _options.AlbumStackingPrefixes) |
| | 53 | | { |
| 130 | 54 | | if (!trimmedFilename.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) |
| | 55 | | { |
| | 56 | | continue; |
| | 57 | | } |
| | 58 | |
|
| 24 | 59 | | var tmp = trimmedFilename.Slice(prefix.Length).Trim(); |
| | 60 | |
|
| 24 | 61 | | if (int.TryParse(tmp.LeftPart(' '), CultureInfo.InvariantCulture, out _)) |
| | 62 | | { |
| 17 | 63 | | return true; |
| | 64 | | } |
| | 65 | | } |
| | 66 | |
|
| 11 | 67 | | return false; |
| | 68 | | } |
| | 69 | | } |
| | 70 | | } |