diff options
| author | Bond-009 <bond.009@outlook.com> | 2019-12-06 12:06:13 +0100 |
|---|---|---|
| committer | Bond-009 <bond.009@outlook.com> | 2019-12-06 12:06:13 +0100 |
| commit | a2c35e6dba02f068a3f06e5a4e4964e6539069d1 (patch) | |
| tree | e75984ab85fedceaf96150ad9d5241cf88230a60 /MediaBrowser.Common | |
| parent | 94edb5b9f98cf3b06144255eccc988712332f0a8 (diff) | |
| parent | 935525e77a18061195dea786be71d38fffe82a10 (diff) | |
Merge remote-tracking branch 'upstream/master' into random
Diffstat (limited to 'MediaBrowser.Common')
| -rw-r--r-- | MediaBrowser.Common/Configuration/EncodingConfigurationExtensions.cs | 35 | ||||
| -rw-r--r-- | MediaBrowser.Common/Cryptography/PasswordHash.cs | 37 | ||||
| -rw-r--r-- | MediaBrowser.Common/Extensions/CopyToExtensions.cs (renamed from MediaBrowser.Common/Extensions/CollectionExtensions.cs) | 33 | ||||
| -rw-r--r-- | MediaBrowser.Common/Extensions/ShuffleExtensions.cs | 31 | ||||
| -rw-r--r-- | MediaBrowser.Common/Hex.cs | 94 | ||||
| -rw-r--r-- | MediaBrowser.Common/HexHelper.cs | 24 | ||||
| -rw-r--r-- | MediaBrowser.Common/IApplicationHost.cs | 8 | ||||
| -rw-r--r-- | MediaBrowser.Common/MediaBrowser.Common.csproj | 5 | ||||
| -rw-r--r-- | MediaBrowser.Common/Net/INetworkManager.cs | 15 | ||||
| -rw-r--r-- | MediaBrowser.Common/Updates/IInstallationManager.cs | 85 |
10 files changed, 231 insertions, 136 deletions
diff --git a/MediaBrowser.Common/Configuration/EncodingConfigurationExtensions.cs b/MediaBrowser.Common/Configuration/EncodingConfigurationExtensions.cs new file mode 100644 index 000000000..ccf965898 --- /dev/null +++ b/MediaBrowser.Common/Configuration/EncodingConfigurationExtensions.cs @@ -0,0 +1,35 @@ +using System.IO; +using MediaBrowser.Model.Configuration; + +namespace MediaBrowser.Common.Configuration +{ + /// <summary> + /// Class containing extension methods for working with the encoding configuration. + /// </summary> + public static class EncodingConfigurationExtensions + { + /// <summary> + /// Gets the encoding options. + /// </summary> + /// <param name="configurationManager">The configuration manager.</param> + /// <returns>The encoding options.</returns> + public static EncodingOptions GetEncodingOptions(this IConfigurationManager configurationManager) + => configurationManager.GetConfiguration<EncodingOptions>("encoding"); + + /// <summary> + /// Retrieves the transcoding temp path from the encoding configuration. + /// </summary> + /// <param name="configurationManager">The Configuration manager.</param> + /// <returns>The transcoding temp path.</returns> + public static string GetTranscodePath(this IConfigurationManager configurationManager) + { + var transcodingTempPath = configurationManager.GetEncodingOptions().TranscodingTempPath; + if (string.IsNullOrEmpty(transcodingTempPath)) + { + return Path.Combine(configurationManager.CommonApplicationPaths.ProgramDataPath, "transcodes"); + } + + return transcodingTempPath; + } + } +} diff --git a/MediaBrowser.Common/Cryptography/PasswordHash.cs b/MediaBrowser.Common/Cryptography/PasswordHash.cs index 7741571db..19b8be47a 100644 --- a/MediaBrowser.Common/Cryptography/PasswordHash.cs +++ b/MediaBrowser.Common/Cryptography/PasswordHash.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.IO; using System.Text; -using static MediaBrowser.Common.HexHelper; namespace MediaBrowser.Common.Cryptography { @@ -61,13 +60,13 @@ namespace MediaBrowser.Common.Cryptography /// <value>Return the hashed password.</value> public byte[] Hash { get; } - public static PasswordHash Parse(string storageString) + public static PasswordHash Parse(string hashString) { - string[] splitted = storageString.Split('$'); + string[] splitted = hashString.Split('$'); // The string should at least contain the hash function and the hash itself if (splitted.Length < 3) { - throw new ArgumentException("String doesn't contain enough segments", nameof(storageString)); + throw new ArgumentException("String doesn't contain enough segments", nameof(hashString)); } // Start at 1, the first index shouldn't contain any data @@ -102,13 +101,13 @@ namespace MediaBrowser.Common.Cryptography // Check if the string also contains a salt if (splitted.Length - index == 2) { - salt = FromHexString(splitted[index++]); - hash = FromHexString(splitted[index++]); + salt = Hex.Decode(splitted[index++]); + hash = Hex.Decode(splitted[index++]); } else { salt = Array.Empty<byte>(); - hash = FromHexString(splitted[index++]); + hash = Hex.Decode(splitted[index++]); } return new PasswordHash(id, hash, salt, parameters); @@ -124,10 +123,10 @@ namespace MediaBrowser.Common.Cryptography stringBuilder.Append('$'); foreach (var pair in _parameters) { - stringBuilder.Append(pair.Key); - stringBuilder.Append('='); - stringBuilder.Append(pair.Value); - stringBuilder.Append(','); + stringBuilder.Append(pair.Key) + .Append('=') + .Append(pair.Value) + .Append(','); } // Remove last ',' @@ -137,21 +136,19 @@ namespace MediaBrowser.Common.Cryptography /// <inheritdoc /> public override string ToString() { - var str = new StringBuilder(); - str.Append('$'); - str.Append(Id); + var str = new StringBuilder() + .Append('$') + .Append(Id); SerializeParameters(str); if (Salt.Length != 0) { - str.Append('$'); - str.Append(ToHexString(Salt)); + str.Append('$') + .Append(Hex.Encode(Salt, false)); } - str.Append('$'); - str.Append(ToHexString(Hash)); - - return str.ToString(); + return str.Append('$') + .Append(Hex.Encode(Hash, false)).ToString(); } } } diff --git a/MediaBrowser.Common/Extensions/CollectionExtensions.cs b/MediaBrowser.Common/Extensions/CopyToExtensions.cs index a12b2833f..78a73f07e 100644 --- a/MediaBrowser.Common/Extensions/CollectionExtensions.cs +++ b/MediaBrowser.Common/Extensions/CopyToExtensions.cs @@ -1,39 +1,12 @@ -#pragma warning disable CS1591 - -using System; using System.Collections.Generic; namespace MediaBrowser.Common.Extensions { - // The MS CollectionExtensions are only available in netcoreapp + /// <summary> + /// Provides <c>CopyTo</c> extensions methods for <see cref="IReadOnlyList{T}" />. + /// </summary> public static class CollectionExtensions { - private static readonly Random _rng = new Random(); - - /// <summary> - /// Shuffles the items in a list. - /// </summary> - /// <param name="list">The list that should get shuffled.</param> - /// <typeparam name="T">The type.</typeparam> - public static void Shuffle<T>(this IList<T> list) - { - int n = list.Count; - while (n > 1) - { - n--; - int k = _rng.Next(n + 1); - T value = list[k]; - list[k] = list[n]; - list[n] = value; - } - } - - public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key) - { - dictionary.TryGetValue(key, out var ret); - return ret; - } - /// <summary> /// Copies all the elements of the current collection to the specified list /// starting at the specified destination array index. The index is specified as a 32-bit integer. diff --git a/MediaBrowser.Common/Extensions/ShuffleExtensions.cs b/MediaBrowser.Common/Extensions/ShuffleExtensions.cs new file mode 100644 index 000000000..5889d09c4 --- /dev/null +++ b/MediaBrowser.Common/Extensions/ShuffleExtensions.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; + +namespace MediaBrowser.Common.Extensions +{ + /// <summary> + /// Provides <c>Shuffle</c> extensions methods for <see cref="IList{T}" />. + /// </summary> + public static class ShuffleExtensions + { + private static readonly Random _rng = new Random(); + + /// <summary> + /// Shuffles the items in a list. + /// </summary> + /// <param name="list">The list that should get shuffled.</param> + /// <typeparam name="T">The type.</typeparam> + public static void Shuffle<T>(this IList<T> list) + { + int n = list.Count; + while (n > 1) + { + n--; + int k = _rng.Next(n + 1); + T value = list[k]; + list[k] = list[n]; + list[n] = value; + } + } + } +} diff --git a/MediaBrowser.Common/Hex.cs b/MediaBrowser.Common/Hex.cs new file mode 100644 index 000000000..b2d9aea3a --- /dev/null +++ b/MediaBrowser.Common/Hex.cs @@ -0,0 +1,94 @@ +using System; +using System.Diagnostics.CodeAnalysis; + +namespace MediaBrowser.Common +{ + /// <summary> + /// Encoding and decoding hex strings. + /// </summary> + public static class Hex + { + internal const string HexCharsLower = "0123456789abcdef"; + internal const string HexCharsUpper = "0123456789ABCDEF"; + + internal const int LastHexSymbol = 0x66; // 102: f + + /// <summary> + /// Map from an ASCII char to its hex value shifted, + /// e.g. <c>b</c> -> 11. 0xFF means it's not a hex symbol. + /// </summary> + /// <value></value> + internal static ReadOnlySpan<byte> HexLookup => new byte[] { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + }; + + /// <summary> + /// Encodes <c>bytes</c> as a hex string. + /// </summary> + /// <param name="bytes"></param> + /// <param name="lowercase"></param> + /// <returns><c>bytes</c> as a hex string.</returns> + public static string Encode(ReadOnlySpan<byte> bytes, bool lowercase = true) + { + var hexChars = lowercase ? HexCharsLower : HexCharsUpper; + + // TODO: use string.Create when it's supports spans + // Ref: https://github.com/dotnet/corefx/issues/29120 + char[] s = new char[bytes.Length * 2]; + int j = 0; + for (int i = 0; i < bytes.Length; i++) + { + s[j++] = hexChars[bytes[i] >> 4]; + s[j++] = hexChars[bytes[i] & 0x0f]; + } + + return new string(s); + } + + /// <summary> + /// Decodes a hex string into bytes. + /// </summary> + /// <param name="str">The <see cref="string" />.</param> + /// <returns>The decoded bytes.</returns> + public static byte[] Decode(ReadOnlySpan<char> str) + { + if (str.Length == 0) + { + return Array.Empty<byte>(); + } + + var unHex = HexLookup; + + int byteLen = str.Length / 2; + byte[] bytes = new byte[byteLen]; + int i = 0; + for (int j = 0; j < byteLen; j++) + { + byte a; + byte b; + if (str[i] > LastHexSymbol + || (a = unHex[str[i++]]) == 0xFF + || str[i] > LastHexSymbol + || (b = unHex[str[i++]]) == 0xFF) + { + ThrowArgumentException(nameof(str)); + break; // Unreachable + } + + bytes[j] = (byte)((a * 16) | b); + } + + return bytes; + } + + [DoesNotReturn] + private static void ThrowArgumentException(string paramName) + => throw new ArgumentException("Character is not a hex symbol.", paramName); + } +} diff --git a/MediaBrowser.Common/HexHelper.cs b/MediaBrowser.Common/HexHelper.cs deleted file mode 100644 index 61007b5b2..000000000 --- a/MediaBrowser.Common/HexHelper.cs +++ /dev/null @@ -1,24 +0,0 @@ -#pragma warning disable CS1591 - -using System; -using System.Globalization; - -namespace MediaBrowser.Common -{ - public static class HexHelper - { - public static byte[] FromHexString(string str) - { - byte[] bytes = new byte[str.Length / 2]; - for (int i = 0; i < str.Length; i += 2) - { - bytes[i / 2] = byte.Parse(str.Substring(i, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture); - } - - return bytes; - } - - public static string ToHexString(byte[] bytes) - => BitConverter.ToString(bytes).Replace("-", ""); - } -} diff --git a/MediaBrowser.Common/IApplicationHost.cs b/MediaBrowser.Common/IApplicationHost.cs index c8da100f6..6668e98aa 100644 --- a/MediaBrowser.Common/IApplicationHost.cs +++ b/MediaBrowser.Common/IApplicationHost.cs @@ -67,7 +67,13 @@ namespace MediaBrowser.Common /// Gets the application version. /// </summary> /// <value>The application version.</value> - string ApplicationVersion { get; } + Version ApplicationVersion { get; } + + /// <summary> + /// Gets the application version. + /// </summary> + /// <value>The application version.</value> + string ApplicationVersionString { get; } /// <summary> /// Gets the application user agent. diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj index 263cc7581..889fbfa5a 100644 --- a/MediaBrowser.Common/MediaBrowser.Common.csproj +++ b/MediaBrowser.Common/MediaBrowser.Common.csproj @@ -12,9 +12,8 @@ </ItemGroup> <ItemGroup> - <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" /> + <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" /> <PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.0" /> - <PackageReference Include="System.Text.Json" Version="4.6.0" /> </ItemGroup> <ItemGroup> @@ -22,7 +21,7 @@ </ItemGroup> <PropertyGroup> - <TargetFramework>netstandard2.0</TargetFramework> + <TargetFramework>netstandard2.1</TargetFramework> <GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateDocumentationFile>true</GenerateDocumentationFile> <TreatWarningsAsErrors>true</TreatWarningsAsErrors> diff --git a/MediaBrowser.Common/Net/INetworkManager.cs b/MediaBrowser.Common/Net/INetworkManager.cs index 97504a471..0b99dc910 100644 --- a/MediaBrowser.Common/Net/INetworkManager.cs +++ b/MediaBrowser.Common/Net/INetworkManager.cs @@ -4,8 +4,6 @@ using System; using System.Collections.Generic; using System.Net; using System.Net.NetworkInformation; -using MediaBrowser.Model.IO; -using MediaBrowser.Model.Net; namespace MediaBrowser.Common.Net { @@ -37,19 +35,6 @@ namespace MediaBrowser.Common.Net bool IsInPrivateAddressSpace(string endpoint); /// <summary> - /// Gets the network shares. - /// </summary> - /// <param name="path">The path.</param> - /// <returns>IEnumerable{NetworkShare}.</returns> - IEnumerable<NetworkShare> GetNetworkShares(string path); - - /// <summary> - /// Gets available devices within the domain - /// </summary> - /// <returns>PC's in the Domain</returns> - IEnumerable<FileSystemEntryInfo> GetNetworkDevices(); - - /// <summary> /// Determines whether [is in local network] [the specified endpoint]. /// </summary> /// <param name="endpoint">The endpoint.</param> diff --git a/MediaBrowser.Common/Updates/IInstallationManager.cs b/MediaBrowser.Common/Updates/IInstallationManager.cs index b3367f71d..e49812f15 100644 --- a/MediaBrowser.Common/Updates/IInstallationManager.cs +++ b/MediaBrowser.Common/Updates/IInstallationManager.cs @@ -13,87 +13,86 @@ namespace MediaBrowser.Common.Updates public interface IInstallationManager : IDisposable { event EventHandler<InstallationEventArgs> PackageInstalling; + event EventHandler<InstallationEventArgs> PackageInstallationCompleted; + event EventHandler<InstallationFailedEventArgs> PackageInstallationFailed; - event EventHandler<InstallationEventArgs> PackageInstallationCancelled; - /// <summary> - /// The completed installations - /// </summary> - IEnumerable<InstallationInfo> CompletedInstallations { get; } + event EventHandler<InstallationEventArgs> PackageInstallationCancelled; /// <summary> - /// Occurs when [plugin uninstalled]. + /// Occurs when a plugin is uninstalled. /// </summary> event EventHandler<GenericEventArgs<IPlugin>> PluginUninstalled; /// <summary> - /// Occurs when [plugin updated]. + /// Occurs when a plugin is updated. /// </summary> event EventHandler<GenericEventArgs<(IPlugin, PackageVersionInfo)>> PluginUpdated; /// <summary> - /// Occurs when [plugin updated]. + /// Occurs when a plugin is installed. /// </summary> event EventHandler<GenericEventArgs<PackageVersionInfo>> PluginInstalled; /// <summary> - /// Gets all available packages. + /// Gets the completed installations. /// </summary> - /// <param name="cancellationToken">The cancellation token.</param> - /// <param name="withRegistration">if set to <c>true</c> [with registration].</param> - /// <param name="packageType">Type of the package.</param> - /// <param name="applicationVersion">The application version.</param> - /// <returns>Task{List{PackageInfo}}.</returns> - Task<List<PackageInfo>> GetAvailablePackages(CancellationToken cancellationToken, - bool withRegistration = true, string packageType = null, Version applicationVersion = null); + IEnumerable<InstallationInfo> CompletedInstallations { get; } /// <summary> - /// Gets all available packages from a static resource. + /// Gets all available packages. /// </summary> /// <param name="cancellationToken">The cancellation token.</param> - /// <returns>Task{List{PackageInfo}}.</returns> - Task<List<PackageInfo>> GetAvailablePackagesWithoutRegistrationInfo(CancellationToken cancellationToken); + /// <returns>Task{IReadOnlyList{PackageInfo}}.</returns> + Task<IReadOnlyList<PackageInfo>> GetAvailablePackages(CancellationToken cancellationToken = default); /// <summary> - /// Gets the package. + /// Returns all plugins matching the requirements. /// </summary> - /// <param name="name">The name.</param> - /// <param name="guid">The assembly guid</param> - /// <param name="classification">The classification.</param> - /// <param name="version">The version.</param> - /// <returns>Task{PackageVersionInfo}.</returns> - Task<PackageVersionInfo> GetPackage(string name, string guid, PackageVersionClass classification, Version version); + /// <param name="availablePackages">The available packages.</param> + /// <param name="name">The name of the plugin.</param> + /// <param name="guid">The id of the plugin.</param> + /// <returns>All plugins matching the requirements.</returns> + IEnumerable<PackageInfo> FilterPackages( + IEnumerable<PackageInfo> availablePackages, + string name = null, + Guid guid = default); /// <summary> - /// Gets the latest compatible version. + /// Returns all compatible versions ordered from newest to oldest. /// </summary> - /// <param name="name">The name.</param> - /// <param name="guid">The assembly guid</param> - /// <param name="currentServerVersion">The current server version.</param> - /// <param name="classification">The classification.</param> - /// <returns>Task{PackageVersionInfo}.</returns> - Task<PackageVersionInfo> GetLatestCompatibleVersion(string name, string guid, Version currentServerVersion, PackageVersionClass classification = PackageVersionClass.Release); + /// <param name="availableVersions">The available version of the plugin.</param> + /// <param name="minVersion">The minimum required version of the plugin.</param> + /// <param name="classification">The classification of updates.</param> + /// <returns>All compatible versions ordered from newest to oldest.</returns> + IEnumerable<PackageVersionInfo> GetCompatibleVersions( + IEnumerable<PackageVersionInfo> availableVersions, + Version minVersion = null, + PackageVersionClass classification = PackageVersionClass.Release); /// <summary> - /// Gets the latest compatible version. + /// Returns all compatible versions ordered from newest to oldest. /// </summary> /// <param name="availablePackages">The available packages.</param> /// <param name="name">The name.</param> - /// <param name="guid">The assembly guid</param> - /// <param name="currentServerVersion">The current server version.</param> + /// <param name="guid">The guid of the plugin.</param> + /// <param name="minVersion">The minimum required version of the plugin.</param> /// <param name="classification">The classification.</param> - /// <returns>PackageVersionInfo.</returns> - PackageVersionInfo GetLatestCompatibleVersion(IEnumerable<PackageInfo> availablePackages, string name, string guid, Version currentServerVersion, PackageVersionClass classification = PackageVersionClass.Release); + /// <returns>All compatible versions ordered from newest to oldest.</returns> + IEnumerable<PackageVersionInfo> GetCompatibleVersions( + IEnumerable<PackageInfo> availablePackages, + string name = null, + Guid guid = default, + Version minVersion = null, + PackageVersionClass classification = PackageVersionClass.Release); /// <summary> - /// Gets the available plugin updates. + /// Returns the available plugin updates. /// </summary> - /// <param name="applicationVersion">The current server version.</param> - /// <param name="withAutoUpdateEnabled">if set to <c>true</c> [with auto update enabled].</param> /// <param name="cancellationToken">The cancellation token.</param> - /// <returns>Task{IEnumerable{PackageVersionInfo}}.</returns> - Task<IEnumerable<PackageVersionInfo>> GetAvailablePluginUpdates(Version applicationVersion, bool withAutoUpdateEnabled, CancellationToken cancellationToken); + /// <returns>The available plugin updates.</returns> + IAsyncEnumerable<PackageVersionInfo> GetAvailablePluginUpdates(CancellationToken cancellationToken = default); /// <summary> /// Installs the package. |
