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