| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | using System.Linq; |
| | | 4 | | using Emby.Naming.Common; |
| | | 5 | | using Jellyfin.Extensions; |
| | | 6 | | using MediaBrowser.Model.Dlna; |
| | | 7 | | using MediaBrowser.Model.Globalization; |
| | | 8 | | |
| | | 9 | | namespace Emby.Naming.ExternalFiles |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// External media file parser class. |
| | | 13 | | /// </summary> |
| | | 14 | | public class ExternalPathParser |
| | | 15 | | { |
| | | 16 | | private readonly NamingOptions _namingOptions; |
| | | 17 | | private readonly DlnaProfileType _type; |
| | | 18 | | private readonly ILocalizationManager _localizationManager; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="ExternalPathParser"/> class. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="localizationManager">The localization manager.</param> |
| | | 24 | | /// <param name="namingOptions">The <see cref="NamingOptions"/> object containing FileExtensions, MediaDefaultFl |
| | | 25 | | /// <param name="type">The <see cref="DlnaProfileType"/> of the parsed file.</param> |
| | | 26 | | public ExternalPathParser(NamingOptions namingOptions, ILocalizationManager localizationManager, DlnaProfileType |
| | | 27 | | { |
| | 285 | 28 | | _localizationManager = localizationManager; |
| | 285 | 29 | | _namingOptions = namingOptions; |
| | 285 | 30 | | _type = type; |
| | 285 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Parse filename and extract information. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="path">Path to file.</param> |
| | | 37 | | /// <param name="extraString">Part of the filename only containing the extra information.</param> |
| | | 38 | | /// <returns>Returns null or an <see cref="ExternalPathParserResult"/> object if parsing is successful.</returns |
| | | 39 | | public ExternalPathParserResult? ParseFile(string path, string? extraString) |
| | | 40 | | { |
| | 77 | 41 | | if (path.Length == 0) |
| | | 42 | | { |
| | 2 | 43 | | return null; |
| | | 44 | | } |
| | | 45 | | |
| | 75 | 46 | | var extension = Path.GetExtension(path.AsSpan()); |
| | | 47 | | |
| | | 48 | | // .idx carries VobSub per-track language metadata. Recognize it here rather |
| | | 49 | | // than adding it to NamingOptions.SubtitleFileExtensions, which also gates |
| | | 50 | | // subtitle uploads/saves. |
| | 75 | 51 | | var isVobSubIndex = _type == DlnaProfileType.Subtitle && extension.Equals(".idx", StringComparison.OrdinalIg |
| | | 52 | | |
| | 75 | 53 | | if (!isVobSubIndex |
| | 75 | 54 | | && !(_type == DlnaProfileType.Subtitle && _namingOptions.SubtitleFileExtensions.Contains(extension, Stri |
| | 75 | 55 | | && !(_type == DlnaProfileType.Audio && _namingOptions.AudioFileExtensions.Contains(extension, StringComp |
| | 75 | 56 | | && !(_type == DlnaProfileType.Lyric && _namingOptions.LyricFileExtensions.Contains(extension, StringComp |
| | | 57 | | { |
| | 17 | 58 | | return null; |
| | | 59 | | } |
| | | 60 | | |
| | 58 | 61 | | var pathInfo = new ExternalPathParserResult(path); |
| | | 62 | | |
| | 58 | 63 | | if (string.IsNullOrEmpty(extraString)) |
| | | 64 | | { |
| | 22 | 65 | | return pathInfo; |
| | | 66 | | } |
| | | 67 | | |
| | 144 | 68 | | foreach (var separator in _namingOptions.MediaFlagDelimiters) |
| | | 69 | | { |
| | 36 | 70 | | var languageString = extraString; |
| | 36 | 71 | | var titleString = string.Empty; |
| | | 72 | | const int SeparatorLength = 1; |
| | | 73 | | |
| | 107 | 74 | | while (languageString.Length > 0) |
| | | 75 | | { |
| | 71 | 76 | | int lastSeparator = languageString.LastIndexOf(separator); |
| | | 77 | | |
| | 71 | 78 | | if (lastSeparator == -1) |
| | | 79 | | { |
| | | 80 | | break; |
| | | 81 | | } |
| | | 82 | | |
| | 71 | 83 | | string currentSlice = languageString[lastSeparator..]; |
| | 71 | 84 | | string currentSliceWithoutSeparator = currentSlice[SeparatorLength..]; |
| | | 85 | | |
| | 71 | 86 | | if (_namingOptions.MediaDefaultFlags.Any(s => currentSliceWithoutSeparator.Contains(s, StringCompari |
| | | 87 | | { |
| | 9 | 88 | | pathInfo.IsDefault = true; |
| | 9 | 89 | | extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase |
| | 9 | 90 | | languageString = languageString[..lastSeparator]; |
| | 9 | 91 | | continue; |
| | | 92 | | } |
| | | 93 | | |
| | 62 | 94 | | if (_namingOptions.MediaForcedFlags.Any(s => currentSliceWithoutSeparator.Contains(s, StringComparis |
| | | 95 | | { |
| | 10 | 96 | | pathInfo.IsForced = true; |
| | 10 | 97 | | extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase |
| | 10 | 98 | | languageString = languageString[..lastSeparator]; |
| | 10 | 99 | | continue; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | // Try to translate to three character code |
| | 52 | 103 | | var culture = _localizationManager.FindLanguageInfo(currentSliceWithoutSeparator); |
| | | 104 | | |
| | 52 | 105 | | if (culture is not null && pathInfo.Language is null) |
| | | 106 | | { |
| | 22 | 107 | | pathInfo.Language = culture.Name.Contains('-', StringComparison.OrdinalIgnoreCase) |
| | 22 | 108 | | ? culture.Name |
| | 22 | 109 | | : culture.ThreeLetterISOLanguageName; |
| | 22 | 110 | | extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase |
| | | 111 | | } |
| | 30 | 112 | | else if (culture is not null && pathInfo.Language == "hin") |
| | | 113 | | { |
| | | 114 | | // Hindi language code "hi" collides with a hearing impaired flag - use as Hindi only if no othe |
| | 1 | 115 | | pathInfo.IsHearingImpaired = true; |
| | 1 | 116 | | pathInfo.Language = culture.Name.Contains('-', StringComparison.OrdinalIgnoreCase) |
| | 1 | 117 | | ? culture.Name |
| | 1 | 118 | | : culture.ThreeLetterISOLanguageName; |
| | 1 | 119 | | extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase |
| | | 120 | | } |
| | 29 | 121 | | else if (_namingOptions.MediaHearingImpairedFlags.Any(s => currentSliceWithoutSeparator.Equals(s, St |
| | | 122 | | { |
| | 5 | 123 | | pathInfo.IsHearingImpaired = true; |
| | 5 | 124 | | extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase |
| | | 125 | | } |
| | | 126 | | else |
| | | 127 | | { |
| | 24 | 128 | | titleString = currentSlice + titleString; |
| | | 129 | | } |
| | | 130 | | |
| | 52 | 131 | | languageString = languageString[..lastSeparator]; |
| | | 132 | | } |
| | | 133 | | |
| | 36 | 134 | | pathInfo.Title = titleString.Length >= SeparatorLength ? titleString[SeparatorLength..] : null; |
| | | 135 | | } |
| | | 136 | | |
| | 36 | 137 | | return pathInfo; |
| | | 138 | | } |
| | | 139 | | } |
| | | 140 | | } |