| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Text; |
| | | 5 | | using System.Threading; |
| | | 6 | | using MediaBrowser.Common.Configuration; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | |
| | | 9 | | namespace 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; |
| | 22 | 18 | | 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 | | { |
| | 22 | 29 | | _appPaths = appPaths; |
| | 22 | 30 | | _logger = logger; |
| | 22 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets the device id, loading it from disk or generating and persisting a new one if none exists. |
| | | 35 | | /// </summary> |
| | 126 | 36 | | public string Value => _id ??= GetDeviceId(); |
| | | 37 | | |
| | 44 | 38 | | private string CachePath => Path.Combine(_appPaths.DataPath, "device.txt"); |
| | | 39 | | |
| | | 40 | | private string? GetCachedId() |
| | | 41 | | { |
| | | 42 | | try |
| | 22 | 43 | | { |
| | | 44 | | lock (_syncLock) |
| | | 45 | | { |
| | 22 | 46 | | var value = File.ReadAllText(CachePath, Encoding.UTF8); |
| | | 47 | | |
| | 0 | 48 | | if (Guid.TryParse(value, out _)) |
| | | 49 | | { |
| | 0 | 50 | | return value; |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | _logger.LogError("Invalid value found in device id file"); |
| | 0 | 54 | | } |
| | 0 | 55 | | } |
| | 0 | 56 | | catch (DirectoryNotFoundException) |
| | | 57 | | { |
| | 0 | 58 | | } |
| | 22 | 59 | | catch (FileNotFoundException) |
| | | 60 | | { |
| | 22 | 61 | | } |
| | 0 | 62 | | catch (Exception ex) |
| | | 63 | | { |
| | 0 | 64 | | _logger.LogError(ex, "Error reading file"); |
| | 0 | 65 | | } |
| | | 66 | | |
| | 22 | 67 | | return null; |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | private void SaveId(string id) |
| | | 71 | | { |
| | | 72 | | try |
| | | 73 | | { |
| | 22 | 74 | | var path = CachePath; |
| | | 75 | | |
| | 22 | 76 | | Directory.CreateDirectory(Path.GetDirectoryName(path) ?? throw new InvalidOperationException("Path can't |
| | | 77 | | |
| | | 78 | | lock (_syncLock) |
| | | 79 | | { |
| | 22 | 80 | | File.WriteAllText(path, id, Encoding.UTF8); |
| | 22 | 81 | | } |
| | 22 | 82 | | } |
| | 0 | 83 | | catch (Exception ex) |
| | | 84 | | { |
| | 0 | 85 | | _logger.LogError(ex, "Error writing to file"); |
| | 0 | 86 | | } |
| | 22 | 87 | | } |
| | | 88 | | |
| | | 89 | | private static string GetNewId() |
| | | 90 | | { |
| | 22 | 91 | | return Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | private string GetDeviceId() |
| | | 95 | | { |
| | 22 | 96 | | var id = GetCachedId(); |
| | | 97 | | |
| | 22 | 98 | | if (string.IsNullOrWhiteSpace(id)) |
| | | 99 | | { |
| | 22 | 100 | | id = GetNewId(); |
| | 22 | 101 | | SaveId(id); |
| | | 102 | | } |
| | | 103 | | |
| | 22 | 104 | | return id; |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | } |