< 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
63%
Covered lines: 24
Uncovered lines: 14
Coverable lines: 38
Total lines: 97
Line coverage: 63.1%
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
.ctor(...)100%11100%
get_Value()100%22100%
get_CachePath()100%11100%
GetCachedId()0%3.3231.25%
SaveId(...)50%2.11270%
GetNewId()100%11100%
GetDeviceId()100%22100%

File(s)

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

#LineLine coverage
 1#pragma warning disable CS1591
 2
 3using System;
 4using System.Globalization;
 5using System.IO;
 6using System.Text;
 7using MediaBrowser.Common.Configuration;
 8using Microsoft.Extensions.Logging;
 9
 10namespace Emby.Server.Implementations.Devices
 11{
 12    public class DeviceId
 13    {
 14        private readonly IApplicationPaths _appPaths;
 15        private readonly ILogger<DeviceId> _logger;
 2216        private readonly object _syncLock = new object();
 17
 18        private string? _id;
 19
 20        public DeviceId(IApplicationPaths appPaths, ILogger<DeviceId> logger)
 21        {
 2222            _appPaths = appPaths;
 2223            _logger = logger;
 2224        }
 25
 11126        public string Value => _id ??= GetDeviceId();
 27
 4428        private string CachePath => Path.Combine(_appPaths.DataPath, "device.txt");
 29
 30        private string? GetCachedId()
 31        {
 32            try
 33            {
 2234                lock (_syncLock)
 35                {
 2236                    var value = File.ReadAllText(CachePath, Encoding.UTF8);
 37
 038                    if (Guid.TryParse(value, out _))
 39                    {
 040                        return value;
 41                    }
 42
 043                    _logger.LogError("Invalid value found in device id file");
 044                }
 045            }
 046            catch (DirectoryNotFoundException)
 47            {
 048            }
 2249            catch (FileNotFoundException)
 50            {
 2251            }
 052            catch (Exception ex)
 53            {
 054                _logger.LogError(ex, "Error reading file");
 055            }
 56
 2257            return null;
 058        }
 59
 60        private void SaveId(string id)
 61        {
 62            try
 63            {
 2264                var path = CachePath;
 65
 2266                Directory.CreateDirectory(Path.GetDirectoryName(path) ?? throw new InvalidOperationException("Path can't
 67
 2268                lock (_syncLock)
 69                {
 2270                    File.WriteAllText(path, id, Encoding.UTF8);
 2271                }
 2272            }
 073            catch (Exception ex)
 74            {
 075                _logger.LogError(ex, "Error writing to file");
 076            }
 2277        }
 78
 79        private static string GetNewId()
 80        {
 2281            return Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
 82        }
 83
 84        private string GetDeviceId()
 85        {
 2286            var id = GetCachedId();
 87
 2288            if (string.IsNullOrWhiteSpace(id))
 89            {
 2290                id = GetNewId();
 2291                SaveId(id);
 92            }
 93
 2294            return id;
 95        }
 96    }
 97}