< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Devices.DeviceId
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Devices/DeviceId.cs
Line coverage
62%
Covered lines: 23
Uncovered lines: 14
Coverable lines: 37
Total lines: 107
Line coverage: 62.1%
Branch coverage
50%
Covered branches: 4
Total branches: 8
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 4/15/2026 - 12:14:34 AM Line coverage: 62.1% (23/37) Branch coverage: 62.5% (5/8) Total lines: 985/20/2026 - 12:15:44 AM Line coverage: 62.1% (23/37) Branch coverage: 50% (4/8) Total lines: 987/18/2026 - 12:15:19 AM Line coverage: 62.1% (23/37) Branch coverage: 50% (4/8) Total lines: 107 4/15/2026 - 12:14:34 AM Line coverage: 62.1% (23/37) Branch coverage: 62.5% (5/8) Total lines: 985/20/2026 - 12:15:44 AM Line coverage: 62.1% (23/37) Branch coverage: 50% (4/8) Total lines: 987/18/2026 - 12:15:19 AM Line coverage: 62.1% (23/37) Branch coverage: 50% (4/8) Total lines: 107

Coverage delta

Coverage delta 13 -13

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Value()100%22100%
get_CachePath()100%11100%
GetCachedId()0%3231.25%
SaveId(...)50%2266.66%
GetNewId()100%11100%
GetDeviceId()50%22100%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Devices/DeviceId.cs

#LineLine coverage
 1using System;
 2using System.Globalization;
 3using System.IO;
 4using System.Text;
 5using System.Threading;
 6using MediaBrowser.Common.Configuration;
 7using Microsoft.Extensions.Logging;
 8
 9namespace Emby.Server.Implementations.Devices
 10{
 11    /// <summary>
 12    /// Provides the persistent unique identifier of this server installation.
 13    /// </summary>
 14    public class DeviceId
 15    {
 16        private readonly IApplicationPaths _appPaths;
 17        private readonly ILogger<DeviceId> _logger;
 2218        private readonly Lock _syncLock = new();
 19
 20        private string? _id;
 21
 22        /// <summary>
 23        /// Initializes a new instance of the <see cref="DeviceId"/> class.
 24        /// </summary>
 25        /// <param name="appPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
 26        /// <param name="logger">Instance of the <see cref="ILogger{DeviceId}"/> interface.</param>
 27        public DeviceId(IApplicationPaths appPaths, ILogger<DeviceId> logger)
 28        {
 2229            _appPaths = appPaths;
 2230            _logger = logger;
 2231        }
 32
 33        /// <summary>
 34        /// Gets the device id, loading it from disk or generating and persisting a new one if none exists.
 35        /// </summary>
 12636        public string Value => _id ??= GetDeviceId();
 37
 4438        private string CachePath => Path.Combine(_appPaths.DataPath, "device.txt");
 39
 40        private string? GetCachedId()
 41        {
 42            try
 2243            {
 44                lock (_syncLock)
 45                {
 2246                    var value = File.ReadAllText(CachePath, Encoding.UTF8);
 47
 048                    if (Guid.TryParse(value, out _))
 49                    {
 050                        return value;
 51                    }
 52
 053                    _logger.LogError("Invalid value found in device id file");
 054                }
 055            }
 056            catch (DirectoryNotFoundException)
 57            {
 058            }
 2259            catch (FileNotFoundException)
 60            {
 2261            }
 062            catch (Exception ex)
 63            {
 064                _logger.LogError(ex, "Error reading file");
 065            }
 66
 2267            return null;
 068        }
 69
 70        private void SaveId(string id)
 71        {
 72            try
 73            {
 2274                var path = CachePath;
 75
 2276                Directory.CreateDirectory(Path.GetDirectoryName(path) ?? throw new InvalidOperationException("Path can't
 77
 78                lock (_syncLock)
 79                {
 2280                    File.WriteAllText(path, id, Encoding.UTF8);
 2281                }
 2282            }
 083            catch (Exception ex)
 84            {
 085                _logger.LogError(ex, "Error writing to file");
 086            }
 2287        }
 88
 89        private static string GetNewId()
 90        {
 2291            return Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
 92        }
 93
 94        private string GetDeviceId()
 95        {
 2296            var id = GetCachedId();
 97
 2298            if (string.IsNullOrWhiteSpace(id))
 99            {
 22100                id = GetNewId();
 22101                SaveId(id);
 102            }
 103
 22104            return id;
 105        }
 106    }
 107}