2019-01-06 20:50:43 +00:00
|
|
|
using System;
|
2016-10-29 05:40:15 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
using MediaBrowser.Model.IO;
|
2018-12-13 13:18:25 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2016-10-29 05:40:15 +00:00
|
|
|
|
2017-08-16 06:43:41 +00:00
|
|
|
namespace Emby.Server.Implementations.Devices
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
public class DeviceId
|
|
|
|
{
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
2019-01-07 23:24:34 +00:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2016-10-29 05:40:15 +00:00
|
|
|
|
|
|
|
private readonly object _syncLock = new object();
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
private string CachePath => Path.Combine(_appPaths.DataPath, "device.txt");
|
2016-10-29 05:40:15 +00:00
|
|
|
|
|
|
|
private string GetCachedId()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
lock (_syncLock)
|
|
|
|
{
|
2019-01-07 23:24:34 +00:00
|
|
|
var value = File.ReadAllText(CachePath, Encoding.UTF8);
|
2016-10-29 05:40:15 +00:00
|
|
|
|
2019-01-13 20:46:33 +00:00
|
|
|
if (Guid.TryParse(value, out var guid))
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogError("Invalid value found in device id file");
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
_logger.LogError(ex, "Error reading file");
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SaveId(string id)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var path = CachePath;
|
|
|
|
|
2019-01-07 23:24:34 +00:00
|
|
|
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
|
2016-10-29 05:40:15 +00:00
|
|
|
|
|
|
|
lock (_syncLock)
|
|
|
|
{
|
|
|
|
_fileSystem.WriteAllText(path, id, Encoding.UTF8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
_logger.LogError(ex, "Error writing to file");
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
private static string GetNewId()
|
2016-10-29 05:40:15 +00:00
|
|
|
{
|
|
|
|
return Guid.NewGuid().ToString("N");
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetDeviceId()
|
|
|
|
{
|
|
|
|
var id = GetCachedId();
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(id))
|
|
|
|
{
|
|
|
|
id = GetNewId();
|
|
|
|
SaveId(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string _id;
|
|
|
|
|
|
|
|
public DeviceId(IApplicationPaths appPaths, ILogger logger, IFileSystem fileSystem)
|
|
|
|
{
|
2019-01-12 20:41:08 +00:00
|
|
|
if (fileSystem == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(fileSystem));
|
|
|
|
}
|
2016-10-29 05:40:15 +00:00
|
|
|
|
|
|
|
_appPaths = appPaths;
|
|
|
|
_logger = logger;
|
2019-01-07 23:24:34 +00:00
|
|
|
_fileSystem = fileSystem;
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
public string Value => _id ?? (_id = GetDeviceId());
|
2016-10-29 05:40:15 +00:00
|
|
|
}
|
|
|
|
}
|