The Free Software Media System - Server Backend & API
reworked code
BaronGreenback95b733ad4cbb
diff --git a/Emby.Dlna/DlnaManager.cs b/Emby.Dlna/DlnaManager.csindex 88b0e11951..4a50136cf7 100644--- a/Emby.Dlna/DlnaManager.cs+++ b/Emby.Dlna/DlnaManager.cs@@ -106,19 +106,28 @@ namespace Emby.Dlnathrow new ArgumentNullException(nameof(deviceInfo));}- var profile = GetProfiles()- .FirstOrDefault(i => i.Identification != null && IsMatch(i.Identification, deviceInfo));-- if (profile != null)+ try{- _logger.LogDebug("Found matching device profile: {0}", profile.Name);+ var profile = GetProfiles()+ .FirstOrDefault(i => i.Identification != null && IsMatch(deviceInfo, i.Identification));++ if (profile != null)+ {+ _logger.LogDebug("Found matching device profile: {0}", profile.Name);+ }+ else+ {+ LogUnmatchedProfile(deviceInfo);+ }++ return profile;}- else+ catch (ArgumentException ex){- LogUnmatchedProfile(deviceInfo);+ _logger.LogError(ex, "Error in profile comparison.");}- return profile;+ return null;}private void LogUnmatchedProfile(DeviceIdentification profile)@@ -138,85 +147,82 @@ namespace Emby.Dlna_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 static bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo){- if (!string.IsNullOrEmpty(profileInfo.FriendlyName))+ if (!IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName)){- if (deviceInfo.FriendlyName == null || !IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName))- {- return false;- }+ return false;}- if (!string.IsNullOrEmpty(profileInfo.Manufacturer))+ if (!IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer)){- if (deviceInfo.Manufacturer == null || !IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer))- {- return false;- }+ return false;}- if (!string.IsNullOrEmpty(profileInfo.ManufacturerUrl))+ if (!IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl)){- if (deviceInfo.ManufacturerUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl))- {- return false;- }+ return false;}- if (!string.IsNullOrEmpty(profileInfo.ModelDescription))+ if (!IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription)){- if (deviceInfo.ModelDescription == null || !IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription))- {- return false;- }+ return false;}- if (!string.IsNullOrEmpty(profileInfo.ModelName))+ if (!IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName)){- if (deviceInfo.ModelName == null || !IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName))- {- return false;- }+ return false;}- if (!string.IsNullOrEmpty(profileInfo.ModelNumber))+ if (!IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber)){- if (deviceInfo.ModelNumber == null || !IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber))- {- return false;- }+ return false;}- if (!string.IsNullOrEmpty(profileInfo.ModelUrl))+ if (!IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl)){- if (deviceInfo.ModelUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl))- {- return false;- }+ return false;}- if (!string.IsNullOrEmpty(profileInfo.SerialNumber))+ if (!IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber)){- if (deviceInfo.SerialNumber == null || !IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber))- {- return false;- }+ return false;}return true;}- private bool IsRegexOrSubstringMatch(string input, string pattern)+ public static bool IsRegexOrSubstringMatch(string input, string pattern){+ if (string.IsNullOrEmpty(pattern))+ {+ // In profile identification: An empty pattern matches anything.+ return true;+ }++ if (string.IsNullOrEmpty(input))+ {+ // The profile contains a value, and the device doesn't.+ return false;+ }+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){- _logger.LogError(ex, "Error evaluating regex pattern {Pattern}", pattern);- return false;+ throw new ArgumentException("Error evaluating regex pattern " + pattern, ex);}}diff --git a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs b/tests/Jellyfin.Dlna.Tests/ProfileTester.csindex 83638c7c44..cc7cf92e70 100644--- a/tests/Jellyfin.Dlna.Tests/ProfileTester.cs+++ b/tests/Jellyfin.Dlna.Tests/ProfileTester.cs@@ -4,6 +4,7 @@ using System.Linq;using System.Text;using System.Text.RegularExpressions;using System.Threading.Tasks;+using Emby.Dlna;using Emby.Dlna.PlayTo;using MediaBrowser.Model.Dlna;using Xunit;@@ -12,92 +13,67 @@ namespace Jellyfin.Dlna.Tests{public class ProfileTester{- private bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo)+ [Fact]+ public void Test_Profile_Matches(){- 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))+ var device = new DeviceInfo(){- if (deviceInfo.ModelName == null || !IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName))- {- return false;- }- }+ Name = "My Device",+ Manufacturer = "LG Electronics",+ ManufacturerUrl = "http://www.lge.com",+ ModelDescription = "LG WebOSTV DMRplus",+ ModelName = "LG TV",+ ModelNumber = "1.0",+ };- if (!string.IsNullOrEmpty(profileInfo.ModelNumber))+ var profile = new DeviceProfile(){- if (deviceInfo.ModelNumber == null || !IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber))+ Name = "Test Profile",+ FriendlyName = "My Device",+ Manufacturer = "LG Electronics",+ ManufacturerUrl = "http://www.lge.com",+ ModelDescription = "LG WebOSTV DMRplus",+ ModelName = "LG TV",+ ModelNumber = "1.0",+ Identification = new DeviceIdentification(){- return false;+ FriendlyName = "My Device",+ Manufacturer = "LG Electronics",+ ManufacturerUrl = "http://www.lge.com",+ ModelDescription = "LG WebOSTV DMRplus",+ ModelName = "LG TV",+ ModelNumber = "1.0",}- }+ };- if (!string.IsNullOrEmpty(profileInfo.ModelUrl))- {- if (deviceInfo.ModelUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl))- {- return false;- }- }+ Assert.True(DlnaManager.IsMatch(device.ToDeviceIdentification(), profile.Identification));- if (!string.IsNullOrEmpty(profileInfo.SerialNumber))+ var profile2 = new DeviceProfile(){- if (deviceInfo.SerialNumber == null || !IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber))+ Name = "Test Profile",+ FriendlyName = "My Device",+ Identification = new DeviceIdentification(){- return false;+ FriendlyName = "My Device",}- }+ };- return true;- }-- private bool IsRegexOrSubstringMatch(string input, string pattern)- {- return input.Contains(pattern, StringComparison.OrdinalIgnoreCase) || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);+ Assert.True(DlnaManager.IsMatch(device.ToDeviceIdentification(), profile2.Identification));}[Fact]- public void Test_Profile_Matches()+ public void Test_Profile_NoMatch(){- var source = new DeviceInfo()+ var device = new DeviceInfo(){- Name = "HelloWorld"+ Name = "My Device",+ Manufacturer = "JVC"};- var dest = new DeviceProfile()+ var profile = new DeviceProfile(){- Name = "Test Subject 1",- FriendlyName = "HelloWorld",+ Name = "Test Profile",+ FriendlyName = "My Device",Manufacturer = "LG Electronics",ManufacturerUrl = "http://www.lge.com",ModelDescription = "LG WebOSTV DMRplus",@@ -105,7 +81,7 @@ namespace Jellyfin.Dlna.TestsModelNumber = "1.0",Identification = new DeviceIdentification(){- FriendlyName = "HelloWorld",+ FriendlyName = "My Device",Manufacturer = "LG Electronics",ManufacturerUrl = "http://www.lge.com",ModelDescription = "LG WebOSTV DMRplus",@@ -114,7 +90,7 @@ namespace Jellyfin.Dlna.Tests}};- Assert.True(IsMatch(dest.Identification, source.ToDeviceIdentification()));+ Assert.False(DlnaManager.IsMatch(device.ToDeviceIdentification(), profile.Identification));}}}