< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.AppBase.ConfigurationHelper
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/AppBase/ConfigurationHelper.cs
Line coverage
88%
Covered lines: 16
Uncovered lines: 2
Coverable lines: 18
Total lines: 62
Line coverage: 88.8%
Branch coverage
62%
Covered branches: 5
Total branches: 8
Branch coverage: 62.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetXmlConfiguration(...)62.5%8.09888.88%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/AppBase/ConfigurationHelper.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using MediaBrowser.Model.Serialization;
 4
 5namespace Emby.Server.Implementations.AppBase
 6{
 7    /// <summary>
 8    /// Class ConfigurationHelper.
 9    /// </summary>
 10    public static class ConfigurationHelper
 11    {
 12        /// <summary>
 13        /// Reads an xml configuration file from the file system
 14        /// It will immediately re-serialize and save if new serialization data is available due to property changes.
 15        /// </summary>
 16        /// <param name="type">The type.</param>
 17        /// <param name="path">The path.</param>
 18        /// <param name="xmlSerializer">The XML serializer.</param>
 19        /// <returns>System.Object.</returns>
 20        public static object GetXmlConfiguration(Type type, string path, IXmlSerializer xmlSerializer)
 21        {
 22            object configuration;
 23
 2224            byte[]? buffer = null;
 25
 26            // Use try/catch to avoid the extra file system lookup using File.Exists
 27            try
 28            {
 2229                buffer = File.ReadAllBytes(path);
 30
 031                configuration = xmlSerializer.DeserializeFromBytes(type, buffer);
 032            }
 2233            catch (Exception)
 34            {
 35                // Note: CreateInstance returns null for Nullable<T>, e.g. CreateInstance(typeof(int?)) returns null.
 2236                configuration = Activator.CreateInstance(type)!;
 2237            }
 38
 2239            using var stream = new MemoryStream(buffer?.Length ?? 0);
 2240            xmlSerializer.SerializeToStream(configuration, stream);
 41
 42            // Take the object we just got and serialize it back to bytes
 2243            Span<byte> newBytes = stream.GetBuffer().AsSpan(0, (int)stream.Length);
 44
 45            // If the file didn't exist before, or if something has changed, re-save
 2246            if (buffer is null || !newBytes.SequenceEqual(buffer))
 47            {
 2248                var directory = Path.GetDirectoryName(path) ?? throw new ArgumentException($"Provided path ({path}) is n
 49
 2250                Directory.CreateDirectory(directory);
 51
 52                // Save it after load in case we got new items
 2253                using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
 54                {
 2255                    fs.Write(newBytes);
 2256                }
 57            }
 58
 2259            return configuration;
 2260        }
 61    }
 62}