Smb save servers, auth

This commit is contained in:
2022-02-10 11:24:04 +01:00
parent 70180b9f12
commit ec03067af1
19 changed files with 463 additions and 132 deletions

View File

@@ -3,17 +3,23 @@ using AsyncEvent;
using FileTime.Core.Interactions;
using FileTime.Core.Models;
using FileTime.Core.Providers;
using Microsoft.Extensions.Logging;
namespace FileTime.Providers.Smb
{
public class SmbContentProvider : IContentProvider
{
private readonly object _initializationGuard = new object();
private bool _initialized;
private bool _initializing;
private IContainer? _parent;
private readonly IInputInterface _inputInterface;
private readonly List<IContainer> _rootContainers;
private readonly IReadOnlyList<IContainer> _rootContainersReadOnly;
private IReadOnlyList<IItem>? _items;
private readonly IReadOnlyList<IElement>? _elements = new List<IElement>().AsReadOnly();
private IReadOnlyList<IItem> _items;
private readonly IReadOnlyList<IElement> _elements = new List<IElement>().AsReadOnly();
private readonly Persistence.PersistenceService _persistenceService;
private readonly ILogger<SmbContentProvider> _logger;
public string Name { get; } = "smb";
@@ -33,12 +39,14 @@ namespace FileTime.Providers.Smb
public bool IsDestroyed => false;
public SmbContentProvider(IInputInterface inputInterface)
public SmbContentProvider(IInputInterface inputInterface, Persistence.PersistenceService persistenceService, ILogger<SmbContentProvider> logger)
{
_rootContainers = new List<IContainer>();
_items = new List<IItem>();
_rootContainersReadOnly = _rootContainers.AsReadOnly();
_inputInterface = inputInterface;
_persistenceService = persistenceService;
_logger = logger;
}
public async Task<IContainer> CreateContainer(string name)
@@ -55,6 +63,8 @@ namespace FileTime.Providers.Smb
await RefreshAsync();
await SaveServers();
return container;
}
@@ -74,7 +84,7 @@ namespace FileTime.Providers.Smb
var pathParts = path.TrimStart(Constants.SeparatorChar).Split(Constants.SeparatorChar);
var rootContainer = _rootContainers.Find(c => c.Name == pathParts[0]);
var rootContainer = (await GetContainers())?.FirstOrDefault(c => c.Name == pathParts[0]);
if (rootContainer == null)
{
@@ -98,9 +108,19 @@ namespace FileTime.Providers.Smb
public void SetParent(IContainer container) => _parent = container;
public Task<IReadOnlyList<IContainer>> GetRootContainers(CancellationToken token = default) => Task.FromResult(_rootContainersReadOnly);
public Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default) => Task.FromResult(_items);
public Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default) => Task.FromResult((IReadOnlyList<IContainer>?)_rootContainersReadOnly);
public Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default) => Task.FromResult(_elements);
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
{
await Init();
return _items;
}
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
{
await Init();
return _rootContainersReadOnly;
}
public Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default) => Task.FromResult((IReadOnlyList<IElement>?)_elements);
public Task Rename(string newName) => throw new NotSupportedException();
public Task<bool> CanOpen() => Task.FromResult(true);
@@ -108,5 +128,54 @@ namespace FileTime.Providers.Smb
public void Destroy() { }
public void Unload() { }
public async Task SaveServers()
{
try
{
await _persistenceService.SaveServers(_rootContainers.OfType<SmbServer>());
}
catch (Exception e)
{
_logger.LogError(e, "Unkown error while saving smb server states.");
}
}
private async Task Init()
{
while (true)
{
lock (_initializationGuard)
{
if (!_initializing)
{
_initializing = true;
break;
}
}
await Task.Delay(1);
}
try
{
if (_initialized) return;
if (_items.Count > 0) return;
_initialized = true;
var servers = await _persistenceService.LoadServers();
foreach (var server in servers)
{
var smbServer = new SmbServer(server.Path, this, _inputInterface, server.UserName, server.Password);
_rootContainers.Add(smbServer);
}
_items = _rootContainers.OrderBy(c => c.Name).ToList().AsReadOnly();
}
finally
{
lock (_initializationGuard)
{
_initializing = false;
}
}
}
}
}