|
@@ -1,5 +1,4 @@
|
|
|
#pragma warning disable CS1591
|
|
|
-
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Globalization;
|
|
@@ -7,10 +6,12 @@ using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Reflection;
|
|
|
using System.Text;
|
|
|
+using System.Text.Json;
|
|
|
using System.Text.RegularExpressions;
|
|
|
using System.Threading.Tasks;
|
|
|
using Emby.Dlna.Profiles;
|
|
|
using Emby.Dlna.Server;
|
|
|
+using Jellyfin.Extensions.Json;
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
using MediaBrowser.Controller;
|
|
@@ -32,9 +33,9 @@ namespace Emby.Dlna
|
|
|
private readonly IXmlSerializer _xmlSerializer;
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
private readonly ILogger<DlnaManager> _logger;
|
|
|
- private readonly IJsonSerializer _jsonSerializer;
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
private static readonly Assembly _assembly = typeof(DlnaManager).Assembly;
|
|
|
+ private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
|
|
|
|
|
|
private readonly Dictionary<string, Tuple<InternalProfileInfo, DeviceProfile>> _profiles = new Dictionary<string, Tuple<InternalProfileInfo, DeviceProfile>>(StringComparer.Ordinal);
|
|
|
|
|
@@ -43,14 +44,12 @@ namespace Emby.Dlna
|
|
|
IFileSystem fileSystem,
|
|
|
IApplicationPaths appPaths,
|
|
|
ILoggerFactory loggerFactory,
|
|
|
- IJsonSerializer jsonSerializer,
|
|
|
IServerApplicationHost appHost)
|
|
|
{
|
|
|
_xmlSerializer = xmlSerializer;
|
|
|
_fileSystem = fileSystem;
|
|
|
_appPaths = appPaths;
|
|
|
_logger = loggerFactory.CreateLogger<DlnaManager>();
|
|
|
- _jsonSerializer = jsonSerializer;
|
|
|
_appHost = appHost;
|
|
|
}
|
|
|
|
|
@@ -94,12 +93,14 @@ namespace Emby.Dlna
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public DeviceProfile GetDefaultProfile()
|
|
|
{
|
|
|
return new DefaultProfile();
|
|
|
}
|
|
|
|
|
|
- public DeviceProfile GetProfile(DeviceIdentification deviceInfo)
|
|
|
+ /// <inheritdoc />
|
|
|
+ public DeviceProfile? GetProfile(DeviceIdentification deviceInfo)
|
|
|
{
|
|
|
if (deviceInfo == null)
|
|
|
{
|
|
@@ -109,13 +110,13 @@ namespace Emby.Dlna
|
|
|
var profile = GetProfiles()
|
|
|
.FirstOrDefault(i => i.Identification != null && IsMatch(deviceInfo, i.Identification));
|
|
|
|
|
|
- if (profile != null)
|
|
|
+ if (profile == null)
|
|
|
{
|
|
|
- _logger.LogDebug("Found matching device profile: {0}", profile.Name);
|
|
|
+ LogUnmatchedProfile(deviceInfo);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- LogUnmatchedProfile(deviceInfo);
|
|
|
+ _logger.LogDebug("Found matching device profile: {ProfileName}", profile.Name);
|
|
|
}
|
|
|
|
|
|
return profile;
|
|
@@ -126,92 +127,57 @@ namespace Emby.Dlna
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
|
builder.AppendLine("No matching device profile found. The default will need to be used.");
|
|
|
- builder.Append("FriendlyName:").AppendLine(profile.FriendlyName);
|
|
|
- builder.Append("Manufacturer:").AppendLine(profile.Manufacturer);
|
|
|
- builder.Append("ManufacturerUrl:").AppendLine(profile.ManufacturerUrl);
|
|
|
- builder.Append("ModelDescription:").AppendLine(profile.ModelDescription);
|
|
|
- builder.Append("ModelName:").AppendLine(profile.ModelName);
|
|
|
- builder.Append("ModelNumber:").AppendLine(profile.ModelNumber);
|
|
|
- builder.Append("ModelUrl:").AppendLine(profile.ModelUrl);
|
|
|
- builder.Append("SerialNumber:").AppendLine(profile.SerialNumber);
|
|
|
+ builder.Append("FriendlyName: ").AppendLine(profile.FriendlyName);
|
|
|
+ builder.Append("Manufacturer: ").AppendLine(profile.Manufacturer);
|
|
|
+ builder.Append("ManufacturerUrl: ").AppendLine(profile.ManufacturerUrl);
|
|
|
+ builder.Append("ModelDescription: ").AppendLine(profile.ModelDescription);
|
|
|
+ builder.Append("ModelName: ").AppendLine(profile.ModelName);
|
|
|
+ builder.Append("ModelNumber: ").AppendLine(profile.ModelNumber);
|
|
|
+ builder.Append("ModelUrl: ").AppendLine(profile.ModelUrl);
|
|
|
+ builder.Append("SerialNumber: ").AppendLine(profile.SerialNumber);
|
|
|
|
|
|
_logger.LogInformation(builder.ToString());
|
|
|
}
|
|
|
|
|
|
- private bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo)
|
|
|
+ /// <summary>
|
|
|
+ /// Attempts to match a device with a profile.
|
|
|
+ /// Rules:
|
|
|
+ /// - If the profile field has no value, the field matches irregardless of its contents.
|
|
|
+ /// - the profile field can be an exact match, or a reg exp.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="deviceInfo">The <see cref="DeviceIdentification"/> of the device.</param>
|
|
|
+ /// <param name="profileInfo">The <see cref="DeviceIdentification"/> of the profile.</param>
|
|
|
+ /// <returns><b>True</b> if they match.</returns>
|
|
|
+ public bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo)
|
|
|
{
|
|
|
- if (!string.IsNullOrEmpty(profileInfo.FriendlyName))
|
|
|
- {
|
|
|
- if (deviceInfo.FriendlyName == null || !IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName))
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!string.IsNullOrEmpty(profileInfo.Manufacturer))
|
|
|
- {
|
|
|
- if (deviceInfo.Manufacturer == null || !IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer))
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!string.IsNullOrEmpty(profileInfo.ManufacturerUrl))
|
|
|
- {
|
|
|
- if (deviceInfo.ManufacturerUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl))
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!string.IsNullOrEmpty(profileInfo.ModelDescription))
|
|
|
- {
|
|
|
- if (deviceInfo.ModelDescription == null || !IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription))
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!string.IsNullOrEmpty(profileInfo.ModelName))
|
|
|
- {
|
|
|
- if (deviceInfo.ModelName == null || !IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName))
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!string.IsNullOrEmpty(profileInfo.ModelNumber))
|
|
|
- {
|
|
|
- if (deviceInfo.ModelNumber == null || !IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber))
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
+ return IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName)
|
|
|
+ && IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer)
|
|
|
+ && IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl)
|
|
|
+ && IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription)
|
|
|
+ && IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName)
|
|
|
+ && IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber)
|
|
|
+ && IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl)
|
|
|
+ && IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber);
|
|
|
+ }
|
|
|
|
|
|
- if (!string.IsNullOrEmpty(profileInfo.ModelUrl))
|
|
|
+ private bool IsRegexOrSubstringMatch(string input, string pattern)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrEmpty(pattern))
|
|
|
{
|
|
|
- if (deviceInfo.ModelUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl))
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ // In profile identification: An empty pattern matches anything.
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
- if (!string.IsNullOrEmpty(profileInfo.SerialNumber))
|
|
|
+ if (string.IsNullOrEmpty(input))
|
|
|
{
|
|
|
- if (deviceInfo.SerialNumber == null || !IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber))
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ // The profile contains a value, and the device doesn't.
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- private bool IsRegexOrSubstringMatch(string input, string pattern)
|
|
|
- {
|
|
|
try
|
|
|
{
|
|
|
- return input.Contains(pattern, StringComparison.OrdinalIgnoreCase) || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
|
|
|
+ return input.Equals(pattern, StringComparison.OrdinalIgnoreCase)
|
|
|
+ || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
|
|
|
}
|
|
|
catch (ArgumentException ex)
|
|
|
{
|
|
@@ -220,7 +186,8 @@ namespace Emby.Dlna
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public DeviceProfile GetProfile(IHeaderDictionary headers)
|
|
|
+ /// <inheritdoc />
|
|
|
+ public DeviceProfile? GetProfile(IHeaderDictionary headers)
|
|
|
{
|
|
|
if (headers == null)
|
|
|
{
|
|
@@ -228,15 +195,13 @@ namespace Emby.Dlna
|
|
|
}
|
|
|
|
|
|
var profile = GetProfiles().FirstOrDefault(i => i.Identification != null && IsMatch(headers, i.Identification));
|
|
|
-
|
|
|
- if (profile != null)
|
|
|
+ if (profile == null)
|
|
|
{
|
|
|
- _logger.LogDebug("Found matching device profile: {0}", profile.Name);
|
|
|
+ _logger.LogDebug("No matching device profile found. {@Headers}", headers);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- var headerString = string.Join(", ", headers.Select(i => string.Format(CultureInfo.InvariantCulture, "{0}={1}", i.Key, i.Value)));
|
|
|
- _logger.LogDebug("No matching device profile found. {0}", headerString);
|
|
|
+ _logger.LogDebug("Found matching device profile: {0}", profile.Name);
|
|
|
}
|
|
|
|
|
|
return profile;
|
|
@@ -286,19 +251,19 @@ namespace Emby.Dlna
|
|
|
return xmlFies
|
|
|
.Select(i => ParseProfileFile(i, type))
|
|
|
.Where(i => i != null)
|
|
|
- .ToList();
|
|
|
+ .ToList()!; // We just filtered out all the nulls
|
|
|
}
|
|
|
catch (IOException)
|
|
|
{
|
|
|
- return new List<DeviceProfile>();
|
|
|
+ return Array.Empty<DeviceProfile>();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private DeviceProfile ParseProfileFile(string path, DeviceProfileType type)
|
|
|
+ private DeviceProfile? ParseProfileFile(string path, DeviceProfileType type)
|
|
|
{
|
|
|
lock (_profiles)
|
|
|
{
|
|
|
- if (_profiles.TryGetValue(path, out Tuple<InternalProfileInfo, DeviceProfile> profileTuple))
|
|
|
+ if (_profiles.TryGetValue(path, out Tuple<InternalProfileInfo, DeviceProfile>? profileTuple))
|
|
|
{
|
|
|
return profileTuple.Item2;
|
|
|
}
|
|
@@ -326,14 +291,20 @@ namespace Emby.Dlna
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public DeviceProfile GetProfile(string id)
|
|
|
+ /// <inheritdoc />
|
|
|
+ public DeviceProfile? GetProfile(string id)
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(id))
|
|
|
{
|
|
|
throw new ArgumentNullException(nameof(id));
|
|
|
}
|
|
|
|
|
|
- var info = GetProfileInfosInternal().First(i => string.Equals(i.Info.Id, id, StringComparison.OrdinalIgnoreCase));
|
|
|
+ var info = GetProfileInfosInternal().FirstOrDefault(i => string.Equals(i.Info.Id, id, StringComparison.OrdinalIgnoreCase));
|
|
|
+
|
|
|
+ if (info == null)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
return ParseProfileFile(info.Path, info.Info.Type);
|
|
|
}
|
|
@@ -350,6 +321,7 @@ namespace Emby.Dlna
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public IEnumerable<DeviceProfileInfo> GetProfileInfos()
|
|
|
{
|
|
|
return GetProfileInfosInternal().Select(i => i.Info);
|
|
@@ -357,17 +329,14 @@ namespace Emby.Dlna
|
|
|
|
|
|
private InternalProfileInfo GetInternalProfileInfo(FileSystemMetadata file, DeviceProfileType type)
|
|
|
{
|
|
|
- return new InternalProfileInfo
|
|
|
- {
|
|
|
- Path = file.FullName,
|
|
|
-
|
|
|
- Info = new DeviceProfileInfo
|
|
|
+ return new InternalProfileInfo(
|
|
|
+ new DeviceProfileInfo
|
|
|
{
|
|
|
Id = file.FullName.ToLowerInvariant().GetMD5().ToString("N", CultureInfo.InvariantCulture),
|
|
|
Name = _fileSystem.GetFileNameWithoutExtension(file),
|
|
|
Type = type
|
|
|
- }
|
|
|
- };
|
|
|
+ },
|
|
|
+ file.FullName);
|
|
|
}
|
|
|
|
|
|
private async Task ExtractSystemProfilesAsync()
|
|
@@ -383,11 +352,12 @@ namespace Emby.Dlna
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- var filename = Path.GetFileName(name).Substring(namespaceName.Length);
|
|
|
-
|
|
|
- var path = Path.Combine(systemProfilesPath, filename);
|
|
|
+ var path = Path.Join(
|
|
|
+ systemProfilesPath,
|
|
|
+ Path.GetFileName(name.AsSpan()).Slice(namespaceName.Length));
|
|
|
|
|
|
- using (var stream = _assembly.GetManifestResourceStream(name))
|
|
|
+ // The stream should exist as we just got its name from GetManifestResourceNames
|
|
|
+ using (var stream = _assembly.GetManifestResourceStream(name)!)
|
|
|
{
|
|
|
var fileInfo = _fileSystem.GetFileInfo(path);
|
|
|
|
|
@@ -395,7 +365,8 @@ namespace Emby.Dlna
|
|
|
{
|
|
|
Directory.CreateDirectory(systemProfilesPath);
|
|
|
|
|
|
- using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
|
|
|
+ // use FileShare.None as this bypasses dotnet bug dotnet/runtime#42790 .
|
|
|
+ using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
|
|
|
{
|
|
|
await stream.CopyToAsync(fileStream).ConfigureAwait(false);
|
|
|
}
|
|
@@ -407,6 +378,7 @@ namespace Emby.Dlna
|
|
|
Directory.CreateDirectory(UserProfilesPath);
|
|
|
}
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public void DeleteProfile(string id)
|
|
|
{
|
|
|
var info = GetProfileInfosInternal().First(i => string.Equals(id, i.Info.Id, StringComparison.OrdinalIgnoreCase));
|
|
@@ -424,6 +396,7 @@ namespace Emby.Dlna
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public void CreateProfile(DeviceProfile profile)
|
|
|
{
|
|
|
profile = ReserializeProfile(profile);
|
|
@@ -439,6 +412,7 @@ namespace Emby.Dlna
|
|
|
SaveProfile(profile, path, DeviceProfileType.User);
|
|
|
}
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public void UpdateProfile(DeviceProfile profile)
|
|
|
{
|
|
|
profile = ReserializeProfile(profile);
|
|
@@ -484,10 +458,10 @@ namespace Emby.Dlna
|
|
|
|
|
|
/// <summary>
|
|
|
/// Recreates the object using serialization, to ensure it's not a subclass.
|
|
|
- /// If it's a subclass it may not serlialize properly to xml (different root element tag name).
|
|
|
+ /// If it's a subclass it may not serialize properly to xml (different root element tag name).
|
|
|
/// </summary>
|
|
|
/// <param name="profile">The device profile.</param>
|
|
|
- /// <returns>The reserialized device profile.</returns>
|
|
|
+ /// <returns>The re-serialized device profile.</returns>
|
|
|
private DeviceProfile ReserializeProfile(DeviceProfile profile)
|
|
|
{
|
|
|
if (profile.GetType() == typeof(DeviceProfile))
|
|
@@ -495,11 +469,13 @@ namespace Emby.Dlna
|
|
|
return profile;
|
|
|
}
|
|
|
|
|
|
- var json = _jsonSerializer.SerializeToString(profile);
|
|
|
+ var json = JsonSerializer.Serialize(profile, _jsonOptions);
|
|
|
|
|
|
- return _jsonSerializer.DeserializeFromString<DeviceProfile>(json);
|
|
|
+ // Output can't be null if the input isn't null
|
|
|
+ return JsonSerializer.Deserialize<DeviceProfile>(json, _jsonOptions)!;
|
|
|
}
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public string GetServerDescriptionXml(IHeaderDictionary headers, string serverUuId, string serverAddress)
|
|
|
{
|
|
|
var profile = GetDefaultProfile();
|
|
@@ -509,6 +485,7 @@ namespace Emby.Dlna
|
|
|
return new DescriptionXmlBuilder(profile, serverUuId, serverAddress, _appHost.FriendlyName, serverId).GetXml();
|
|
|
}
|
|
|
|
|
|
+ /// <inheritdoc />
|
|
|
public ImageStream GetIcon(string filename)
|
|
|
{
|
|
|
var format = filename.EndsWith(".png", StringComparison.OrdinalIgnoreCase)
|
|
@@ -526,9 +503,15 @@ namespace Emby.Dlna
|
|
|
|
|
|
private class InternalProfileInfo
|
|
|
{
|
|
|
- internal DeviceProfileInfo Info { get; set; }
|
|
|
+ internal InternalProfileInfo(DeviceProfileInfo info, string path)
|
|
|
+ {
|
|
|
+ Info = info;
|
|
|
+ Path = path;
|
|
|
+ }
|
|
|
+
|
|
|
+ internal DeviceProfileInfo Info { get; }
|
|
|
|
|
|
- internal string Path { get; set; }
|
|
|
+ internal string Path { get; }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -553,7 +536,7 @@ namespace Emby.Dlna
|
|
|
|
|
|
private void DumpProfiles()
|
|
|
{
|
|
|
- DeviceProfile[] list = new []
|
|
|
+ DeviceProfile[] list = new[]
|
|
|
{
|
|
|
new SamsungSmartTvProfile(),
|
|
|
new XboxOneProfile(),
|