2022-02-07 17:36:07 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using Emby.Naming.Common;
|
|
|
|
using Jellyfin.Extensions;
|
|
|
|
using MediaBrowser.Model.Dlna;
|
|
|
|
using MediaBrowser.Model.Globalization;
|
|
|
|
|
|
|
|
namespace Emby.Naming.ExternalFiles
|
|
|
|
{
|
|
|
|
/// <summary>
|
2022-02-17 08:03:08 +00:00
|
|
|
/// External media file parser class.
|
2022-02-07 17:36:07 +00:00
|
|
|
/// </summary>
|
|
|
|
public class ExternalPathParser
|
|
|
|
{
|
|
|
|
private readonly NamingOptions _namingOptions;
|
|
|
|
private readonly DlnaProfileType _type;
|
|
|
|
private readonly ILocalizationManager _localizationManager;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ExternalPathParser"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="localizationManager">The localization manager.</param>
|
|
|
|
/// <param name="namingOptions">The <see cref="NamingOptions"/> object containing FileExtensions, MediaDefaultFlags, MediaForcedFlags and MediaFlagDelimiters.</param>
|
|
|
|
/// <param name="type">The <see cref="DlnaProfileType"/> of the parsed file.</param>
|
|
|
|
public ExternalPathParser(NamingOptions namingOptions, ILocalizationManager localizationManager, DlnaProfileType type)
|
|
|
|
{
|
|
|
|
_localizationManager = localizationManager;
|
|
|
|
_namingOptions = namingOptions;
|
|
|
|
_type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Parse filename and extract information.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">Path to file.</param>
|
|
|
|
/// <param name="extraString">Part of the filename only containing the extra information.</param>
|
|
|
|
/// <returns>Returns null or an <see cref="ExternalPathParserResult"/> object if parsing is successful.</returns>
|
|
|
|
public ExternalPathParserResult? ParseFile(string path, string? extraString)
|
|
|
|
{
|
|
|
|
if (path.Length == 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var extension = Path.GetExtension(path);
|
2022-02-17 08:03:08 +00:00
|
|
|
if (!(_type == DlnaProfileType.Subtitle && _namingOptions.SubtitleFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase))
|
|
|
|
&& !(_type == DlnaProfileType.Audio && _namingOptions.AudioFileExtensions.Contains(extension, StringComparison.OrdinalIgnoreCase)))
|
2022-02-07 17:36:07 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var pathInfo = new ExternalPathParserResult(path);
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(extraString))
|
|
|
|
{
|
|
|
|
return pathInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var separator in _namingOptions.MediaFlagDelimiters)
|
|
|
|
{
|
|
|
|
var languageString = extraString;
|
|
|
|
var titleString = string.Empty;
|
2022-03-04 09:52:15 +00:00
|
|
|
const int SeparatorLength = 1;
|
2022-02-07 17:36:07 +00:00
|
|
|
|
|
|
|
while (languageString.Length > 0)
|
|
|
|
{
|
2022-03-04 09:52:15 +00:00
|
|
|
int lastSeparator = languageString.LastIndexOf(separator);
|
2022-02-07 17:36:07 +00:00
|
|
|
|
|
|
|
if (lastSeparator == -1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
string currentSlice = languageString[lastSeparator..];
|
2022-03-04 09:52:15 +00:00
|
|
|
string currentSliceWithoutSeparator = currentSlice[SeparatorLength..];
|
2022-02-07 17:36:07 +00:00
|
|
|
|
2022-02-17 08:03:08 +00:00
|
|
|
if (_namingOptions.MediaDefaultFlags.Any(s => currentSliceWithoutSeparator.Contains(s, StringComparison.OrdinalIgnoreCase)))
|
2022-02-07 17:36:07 +00:00
|
|
|
{
|
|
|
|
pathInfo.IsDefault = true;
|
|
|
|
extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase);
|
|
|
|
languageString = languageString[..lastSeparator];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-17 08:03:08 +00:00
|
|
|
if (_namingOptions.MediaForcedFlags.Any(s => currentSliceWithoutSeparator.Contains(s, StringComparison.OrdinalIgnoreCase)))
|
2022-02-07 17:36:07 +00:00
|
|
|
{
|
|
|
|
pathInfo.IsForced = true;
|
|
|
|
extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase);
|
|
|
|
languageString = languageString[..lastSeparator];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to translate to three character code
|
2022-02-17 08:03:08 +00:00
|
|
|
var culture = _localizationManager.FindLanguageInfo(currentSliceWithoutSeparator);
|
2022-02-07 17:36:07 +00:00
|
|
|
|
2022-12-05 14:01:13 +00:00
|
|
|
if (culture is not null && pathInfo.Language is null)
|
2022-02-07 17:36:07 +00:00
|
|
|
{
|
|
|
|
pathInfo.Language = culture.ThreeLetterISOLanguageName;
|
|
|
|
extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
2022-12-05 14:01:13 +00:00
|
|
|
else if (culture is not null && pathInfo.Language == "hin")
|
2022-10-07 10:38:05 +00:00
|
|
|
{
|
|
|
|
// Hindi language code "hi" collides with a hearing impaired flag - use as Hindi only if no other language is set
|
|
|
|
pathInfo.IsHearingImpaired = true;
|
|
|
|
pathInfo.Language = culture.ThreeLetterISOLanguageName;
|
|
|
|
extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
else if (_namingOptions.MediaHearingImpairedFlags.Any(s => currentSliceWithoutSeparator.Contains(s, StringComparison.OrdinalIgnoreCase)))
|
|
|
|
{
|
|
|
|
pathInfo.IsHearingImpaired = true;
|
|
|
|
extraString = extraString.Replace(currentSlice, string.Empty, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
2022-02-07 17:36:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
titleString = currentSlice + titleString;
|
|
|
|
}
|
|
|
|
|
|
|
|
languageString = languageString[..lastSeparator];
|
|
|
|
}
|
|
|
|
|
2022-03-04 09:52:15 +00:00
|
|
|
pathInfo.Title = titleString.Length >= SeparatorLength ? titleString[SeparatorLength..] : null;
|
2022-02-07 17:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pathInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|