2019-11-01 10:38:54 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
using System;
|
2019-02-28 15:22:57 -07:00
|
|
|
using System.Globalization;
|
2016-10-28 22:40:15 -07:00
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2018-12-13 06:18:25 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2016-10-28 22:40:15 -07:00
|
|
|
|
2017-08-15 23:43:41 -07:00
|
|
|
namespace Emby.Server.Implementations.Devices
|
2016-10-28 22:40:15 -07:00
|
|
|
{
|
|
|
|
public class DeviceId
|
|
|
|
{
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
2020-06-05 17:15:56 -07:00
|
|
|
private readonly ILogger<DeviceId> _logger;
|
2016-10-28 22:40:15 -07:00
|
|
|
private readonly object _syncLock = new object();
|
|
|
|
|
2024-03-13 06:56:51 -07:00
|
|
|
private string? _id;
|
2021-10-02 11:06:00 -07:00
|
|
|
|
2024-03-13 06:56:51 -07:00
|
|
|
public DeviceId(IApplicationPaths appPaths, ILogger<DeviceId> logger)
|
2021-10-02 11:06:00 -07:00
|
|
|
{
|
|
|
|
_appPaths = appPaths;
|
2024-03-13 06:56:51 -07:00
|
|
|
_logger = logger;
|
2021-10-02 11:06:00 -07:00
|
|
|
}
|
|
|
|
|
2024-03-13 06:56:51 -07:00
|
|
|
public string Value => _id ??= GetDeviceId();
|
2021-10-02 11:06:00 -07:00
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private string CachePath => Path.Combine(_appPaths.DataPath, "device.txt");
|
2016-10-28 22:40:15 -07:00
|
|
|
|
2024-03-13 06:56:51 -07:00
|
|
|
private string? GetCachedId()
|
2016-10-28 22:40:15 -07:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
lock (_syncLock)
|
|
|
|
{
|
2019-01-07 16:24:34 -07:00
|
|
|
var value = File.ReadAllText(CachePath, Encoding.UTF8);
|
2016-10-28 22:40:15 -07:00
|
|
|
|
2021-12-15 10:25:36 -07:00
|
|
|
if (Guid.TryParse(value, out _))
|
2016-10-28 22:40:15 -07:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2018-12-13 06:18:25 -07:00
|
|
|
_logger.LogError("Invalid value found in device id file");
|
2016-10-28 22:40:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
_logger.LogError(ex, "Error reading file");
|
2016-10-28 22:40:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SaveId(string id)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var path = CachePath;
|
|
|
|
|
2024-03-13 06:56:51 -07:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path) ?? throw new InvalidOperationException("Path can't be a root directory."));
|
2016-10-28 22:40:15 -07:00
|
|
|
|
|
|
|
lock (_syncLock)
|
|
|
|
{
|
2019-01-26 15:09:07 -07:00
|
|
|
File.WriteAllText(path, id, Encoding.UTF8);
|
2016-10-28 22:40:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
_logger.LogError(ex, "Error writing to file");
|
2016-10-28 22:40:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static string GetNewId()
|
2016-10-28 22:40:15 -07:00
|
|
|
{
|
2019-02-28 15:22:57 -07:00
|
|
|
return Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
|
2016-10-28 22:40:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private string GetDeviceId()
|
|
|
|
{
|
|
|
|
var id = GetCachedId();
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(id))
|
|
|
|
{
|
|
|
|
id = GetNewId();
|
|
|
|
SaveId(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|