Async core refactor
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using AsyncEvent;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -8,15 +9,19 @@ namespace FileTime.Providers.Local
|
||||
public class LocalContentProvider : IContentProvider
|
||||
{
|
||||
private readonly ILogger<LocalContentProvider> _logger;
|
||||
private IContainer _parent = null;
|
||||
private IContainer? _parent;
|
||||
|
||||
public IReadOnlyList<IContainer> RootContainers { get; }
|
||||
private readonly IReadOnlyList<IContainer> _rootContainers;
|
||||
private readonly IReadOnlyList<IItem>? _items;
|
||||
private readonly IReadOnlyList<IElement>? _elements = new List<IElement>().AsReadOnly();
|
||||
|
||||
/* public IReadOnlyList<IContainer> RootContainers { get; }
|
||||
|
||||
public IReadOnlyList<IItem> Items => RootContainers;
|
||||
|
||||
public IReadOnlyList<IContainer> Containers => RootContainers;
|
||||
|
||||
public IReadOnlyList<IElement> Elements { get; } = new List<IElement>();
|
||||
public IReadOnlyList<IElement> Elements { get; } = new List<IElement>(); */
|
||||
|
||||
public string Name { get; } = "local";
|
||||
|
||||
@@ -25,7 +30,7 @@ namespace FileTime.Providers.Local
|
||||
|
||||
public IContentProvider Provider => this;
|
||||
|
||||
public event EventHandler? Refreshed;
|
||||
public AsyncEventHandler Refreshed { get; } = new();
|
||||
|
||||
public bool IsCaseInsensitive { get; }
|
||||
|
||||
@@ -41,13 +46,14 @@ namespace FileTime.Providers.Local
|
||||
|
||||
FullName = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "" : null;
|
||||
|
||||
RootContainers = rootDirectories.Select(d => new LocalFolder(d, this, this)).OrderBy(d => d.Name).ToList().AsReadOnly();
|
||||
_rootContainers = rootDirectories.Select(d => new LocalFolder(d, this, this)).OrderBy(d => d.Name).ToList().AsReadOnly();
|
||||
_items = _rootContainers.Cast<IItem>().ToList().AsReadOnly();
|
||||
}
|
||||
|
||||
public IItem? GetByPath(string path)
|
||||
public async Task<IItem?> GetByPath(string path)
|
||||
{
|
||||
var pathParts = (IsCaseInsensitive ? path.ToLower() : path).TrimStart(Constants.SeparatorChar).Split(Constants.SeparatorChar);
|
||||
var rootContainer = RootContainers.FirstOrDefault(c => NormalizePath(c.Name) == NormalizePath(pathParts[0]));
|
||||
var rootContainer = _rootContainers.FirstOrDefault(c => NormalizePath(c.Name) == NormalizePath(pathParts[0]));
|
||||
|
||||
if (rootContainer == null)
|
||||
{
|
||||
@@ -55,27 +61,43 @@ namespace FileTime.Providers.Local
|
||||
return null;
|
||||
}
|
||||
|
||||
return rootContainer.GetByPath(string.Join(Constants.SeparatorChar, pathParts.Skip(1)));
|
||||
return await rootContainer.GetByPath(string.Join(Constants.SeparatorChar, pathParts.Skip(1)));
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
}
|
||||
public Task Refresh() => Task.CompletedTask;
|
||||
|
||||
|
||||
public IContainer? GetParent() => _parent;
|
||||
public IContainer CreateContainer(string name) => throw new NotSupportedException();
|
||||
public IElement CreateElement(string name) => throw new NotSupportedException();
|
||||
public bool IsExists(string name) => Items.Any(i => i.Name == name);
|
||||
public Task<IContainer> CreateContainer(string name) => throw new NotSupportedException();
|
||||
public Task<IElement> CreateElement(string name) => throw new NotSupportedException();
|
||||
public Task<bool> IsExists(string name) => Task.FromResult(_rootContainers.Any(i => i.Name == name));
|
||||
|
||||
public void Delete() => throw new NotSupportedException();
|
||||
public Task Delete() => throw new NotSupportedException();
|
||||
|
||||
internal string NormalizePath(string path) => IsCaseInsensitive ? path.ToLower() : path;
|
||||
|
||||
public bool CanHandlePath(string path) => RootContainers.Any(r => path.StartsWith(r.Name));
|
||||
public bool CanHandlePath(string path) => _rootContainers.Any(r => path.StartsWith(r.Name));
|
||||
|
||||
public void SetParent(IContainer container)
|
||||
{
|
||||
_parent = container;
|
||||
}
|
||||
public Task<IReadOnlyList<IContainer>?> GetRootContainers(CancellationToken token = default)
|
||||
{
|
||||
return Task.FromResult(_rootContainers);
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
|
||||
{
|
||||
return Task.FromResult(_items);
|
||||
}
|
||||
public Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
|
||||
{
|
||||
return Task.FromResult(_rootContainers);
|
||||
}
|
||||
public Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
|
||||
{
|
||||
return Task.FromResult(_elements);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,9 +34,10 @@ namespace FileTime.Providers.Local
|
||||
|
||||
public string GetPrimaryAttributeText() => _file.Length.ToSizeString();
|
||||
|
||||
public void Delete()
|
||||
public Task Delete()
|
||||
{
|
||||
_file.Delete();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using AsyncEvent;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
|
||||
@@ -15,44 +16,11 @@ namespace FileTime.Providers.Local
|
||||
public LocalContentProvider Provider { get; }
|
||||
IContentProvider IItem.Provider => Provider;
|
||||
|
||||
public IReadOnlyList<IItem> Items
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_items == null) Refresh();
|
||||
return _items!;
|
||||
}
|
||||
|
||||
private set => _items = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IContainer> Containers
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_containers == null) Refresh();
|
||||
return _containers!;
|
||||
}
|
||||
|
||||
private set => _containers = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IElement> Elements
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_elements == null) Refresh();
|
||||
return _elements!;
|
||||
}
|
||||
|
||||
private set => _elements = value;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string FullName { get; }
|
||||
|
||||
public event EventHandler? Refreshed;
|
||||
public AsyncEventHandler Refreshed { get; } = new();
|
||||
|
||||
public LocalFolder(DirectoryInfo directory, LocalContentProvider contentProvider, IContainer? parent)
|
||||
{
|
||||
@@ -66,7 +34,7 @@ namespace FileTime.Providers.Local
|
||||
|
||||
public IContainer? GetParent() => _parent;
|
||||
|
||||
public void Refresh()
|
||||
public Task Refresh()
|
||||
{
|
||||
_containers = new List<IContainer>();
|
||||
_elements = new List<IElement>();
|
||||
@@ -79,14 +47,32 @@ namespace FileTime.Providers.Local
|
||||
catch { }
|
||||
|
||||
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
|
||||
Refreshed?.Invoke(this, EventArgs.Empty);
|
||||
Refreshed?.InvokeAsync(this, AsyncEventArgs.Empty);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public IItem? GetByPath(string path)
|
||||
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
|
||||
{
|
||||
if (_items == null) await Refresh();
|
||||
return _items;
|
||||
}
|
||||
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
|
||||
{
|
||||
if (_containers == null) await Refresh();
|
||||
return _containers;
|
||||
}
|
||||
public async Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
|
||||
{
|
||||
if (_elements == null) await Refresh();
|
||||
return _elements;
|
||||
}
|
||||
|
||||
public async Task<IItem?> GetByPath(string path)
|
||||
{
|
||||
var paths = path.Split(Constants.SeparatorChar);
|
||||
|
||||
var item = Items.FirstOrDefault(i => Provider.NormalizePath(i.Name) == Provider.NormalizePath(paths[0]));
|
||||
var item = (await GetItems())!.FirstOrDefault(i => Provider.NormalizePath(i.Name) == Provider.NormalizePath(paths[0]));
|
||||
|
||||
if (paths.Length == 1)
|
||||
{
|
||||
@@ -95,29 +81,33 @@ namespace FileTime.Providers.Local
|
||||
|
||||
if (item is IContainer container)
|
||||
{
|
||||
return container.GetByPath(string.Join(Constants.SeparatorChar, paths.Skip(1)));
|
||||
return await container.GetByPath(string.Join(Constants.SeparatorChar, paths.Skip(1)));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
public IContainer CreateContainer(string name)
|
||||
public async Task<IContainer> CreateContainer(string name)
|
||||
{
|
||||
Directory.CreateSubdirectory(name);
|
||||
Refresh();
|
||||
await Refresh();
|
||||
|
||||
return _containers!.FirstOrDefault(c => Provider.NormalizePath(c.Name) == Provider.NormalizePath(name))!;
|
||||
}
|
||||
|
||||
public IElement CreateElement(string name)
|
||||
public async Task<IElement> CreateElement(string name)
|
||||
{
|
||||
using (File.Create(Path.Combine(Directory.FullName, name))) { }
|
||||
Refresh();
|
||||
await Refresh();
|
||||
|
||||
return _elements!.FirstOrDefault(e => Provider.NormalizePath(e.Name) == Provider.NormalizePath(name))!;
|
||||
}
|
||||
|
||||
public bool IsExists(string name) => Items.Any(i => Provider.NormalizePath(i.Name) == Provider.NormalizePath(name));
|
||||
public async Task<bool> IsExists(string name) => (await GetItems())?.Any(i => Provider.NormalizePath(i.Name) == Provider.NormalizePath(name)) ?? false;
|
||||
|
||||
public void Delete() => Directory.Delete(true);
|
||||
public Task Delete()
|
||||
{
|
||||
Directory.Delete(true);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using AsyncEvent;
|
||||
using FileTime.Core.Interactions;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
@@ -7,16 +8,11 @@ namespace FileTime.Providers.Smb
|
||||
public class SmbContentProvider : IContentProvider
|
||||
{
|
||||
private IContainer _parent;
|
||||
private readonly List<IContainer> _rootContainers;
|
||||
private readonly IInputInterface _inputInterface;
|
||||
|
||||
public IReadOnlyList<IContainer> RootContainers { get; }
|
||||
|
||||
public IReadOnlyList<IItem> Items => RootContainers;
|
||||
|
||||
public IReadOnlyList<IContainer> Containers => RootContainers;
|
||||
|
||||
public IReadOnlyList<IElement> Elements { get; } = new List<IElement>();
|
||||
private readonly List<IContainer> _rootContainers;
|
||||
private readonly IReadOnlyList<IContainer> _rootContainersReadOnly;
|
||||
private readonly IReadOnlyList<IItem>? _items;
|
||||
private readonly IReadOnlyList<IElement>? _elements = new List<IElement>().AsReadOnly();
|
||||
|
||||
public string Name { get; } = "smb";
|
||||
|
||||
@@ -26,16 +22,16 @@ namespace FileTime.Providers.Smb
|
||||
|
||||
public IContentProvider Provider => this;
|
||||
|
||||
public event EventHandler? Refreshed;
|
||||
public AsyncEventHandler Refreshed { get; } = new();
|
||||
|
||||
public SmbContentProvider(IInputInterface inputInterface)
|
||||
{
|
||||
_rootContainers = new List<IContainer>();
|
||||
RootContainers = _rootContainers.AsReadOnly();
|
||||
_rootContainersReadOnly = _rootContainers.AsReadOnly();
|
||||
_inputInterface = inputInterface;
|
||||
}
|
||||
|
||||
public IContainer CreateContainer(string name)
|
||||
public async Task<IContainer> CreateContainer(string name)
|
||||
{
|
||||
var fullName = "\\\\" + name;
|
||||
var container = _rootContainers.Find(c => c.Name == name);
|
||||
@@ -46,37 +42,54 @@ namespace FileTime.Providers.Smb
|
||||
_rootContainers.Add(container);
|
||||
}
|
||||
|
||||
Refresh();
|
||||
await Refresh();
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
public IElement CreateElement(string name)
|
||||
public Task<IElement> CreateElement(string name)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
public Task Delete()
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public IItem? GetByPath(string path)
|
||||
public Task<IItem?> GetByPath(string path)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IContainer? GetParent() => _parent;
|
||||
|
||||
public bool IsExists(string name) => Items.Any(i => i.Name == name);
|
||||
public async Task<bool> IsExists(string name) => (await GetItems()).Any(i => i.Name == name);
|
||||
|
||||
public void Refresh()
|
||||
public async Task Refresh()
|
||||
{
|
||||
Refreshed?.Invoke(this, EventArgs.Empty);
|
||||
await Refreshed?.InvokeAsync(this, AsyncEventArgs.Empty);
|
||||
}
|
||||
|
||||
public bool CanHandlePath(string path) => path.StartsWith("smb://") || path.StartsWith(@"\\");
|
||||
|
||||
public void SetParent(IContainer container) => _parent = container;
|
||||
public Task<IReadOnlyList<IContainer>?> GetRootContainers(CancellationToken token = default)
|
||||
{
|
||||
return Task.FromResult(_rootContainersReadOnly);
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
|
||||
{
|
||||
return Task.FromResult(_items);
|
||||
}
|
||||
public Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
|
||||
{
|
||||
return Task.FromResult(_rootContainersReadOnly);
|
||||
}
|
||||
public Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
|
||||
{
|
||||
return Task.FromResult(_elements);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,18 +16,15 @@ namespace FileTime.Providers.Smb
|
||||
|
||||
public IContentProvider Provider { get; }
|
||||
|
||||
private readonly Func<ISMBClient> _getSmbClient;
|
||||
|
||||
public SmbFile(string name, SmbContentProvider provider, IContainer parent, Func<ISMBClient> getSmbClient)
|
||||
public SmbFile(string name, SmbContentProvider provider, IContainer parent)
|
||||
{
|
||||
Name = name;
|
||||
FullName = parent.FullName + Constants.SeparatorChar + Name;
|
||||
|
||||
Provider = provider;
|
||||
_getSmbClient = getSmbClient;
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
public Task Delete()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using AsyncEvent;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
using SMBLibrary;
|
||||
@@ -10,43 +11,9 @@ namespace FileTime.Providers.Smb
|
||||
private IReadOnlyList<IItem>? _items;
|
||||
private IReadOnlyList<IContainer>? _containers;
|
||||
private IReadOnlyList<IElement>? _elements;
|
||||
private Func<ISMBClient> _getSmbClient;
|
||||
private readonly SmbShare _smbShare;
|
||||
private readonly IContainer? _parent;
|
||||
|
||||
public IReadOnlyList<IItem> Items
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_items == null) Refresh();
|
||||
return _items!;
|
||||
}
|
||||
|
||||
private set => _items = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IContainer> Containers
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_containers == null) Refresh();
|
||||
return _containers!;
|
||||
}
|
||||
|
||||
private set => _containers = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IElement> Elements
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_elements == null) Refresh();
|
||||
return _elements!;
|
||||
}
|
||||
|
||||
private set => _elements = value;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string? FullName { get; }
|
||||
@@ -56,39 +23,33 @@ namespace FileTime.Providers.Smb
|
||||
public SmbContentProvider Provider { get; }
|
||||
IContentProvider IItem.Provider => Provider;
|
||||
|
||||
public event EventHandler? Refreshed;
|
||||
public AsyncEventHandler Refreshed { get; } = new();
|
||||
|
||||
public SmbFolder(string name, SmbContentProvider contentProvider, SmbShare smbShare, IContainer parent, Func<ISMBClient> getSmbClient)
|
||||
public SmbFolder(string name, SmbContentProvider contentProvider, SmbShare smbShare, IContainer parent)
|
||||
{
|
||||
_parent = parent;
|
||||
_getSmbClient = getSmbClient;
|
||||
_smbShare = smbShare;
|
||||
|
||||
Name = name;
|
||||
FullName = parent?.FullName == null ? Name : parent.FullName + Constants.SeparatorChar + Name;
|
||||
Provider = contentProvider;
|
||||
_smbShare = smbShare;
|
||||
}
|
||||
|
||||
public IContainer CreateContainer(string name)
|
||||
public Task<IContainer> CreateContainer(string name)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IElement CreateElement(string name)
|
||||
public Task<IElement> CreateElement(string name)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IItem? GetByPath(string path)
|
||||
public async Task<IItem?> GetByPath(string path)
|
||||
{
|
||||
var paths = path.Split(Constants.SeparatorChar);
|
||||
|
||||
var item = Items.FirstOrDefault(i => i.Name == paths[0]);
|
||||
var item = (await GetItems())?.FirstOrDefault(i => i.Name == paths[0]);
|
||||
|
||||
if (paths.Length == 1)
|
||||
{
|
||||
@@ -97,7 +58,7 @@ namespace FileTime.Providers.Smb
|
||||
|
||||
if (item is IContainer container)
|
||||
{
|
||||
return container.GetByPath(string.Join(Constants.SeparatorChar, paths.Skip(1)));
|
||||
return await container.GetByPath(string.Join(Constants.SeparatorChar, paths.Skip(1)));
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -105,12 +66,17 @@ namespace FileTime.Providers.Smb
|
||||
|
||||
public IContainer? GetParent() => _parent;
|
||||
|
||||
public bool IsExists(string name)
|
||||
public Task<bool> IsExists(string name)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
public Task Delete()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task Refresh()
|
||||
{
|
||||
var containers = new List<IContainer>();
|
||||
var elements = new List<IElement>();
|
||||
@@ -118,7 +84,7 @@ namespace FileTime.Providers.Smb
|
||||
try
|
||||
{
|
||||
var path = FullName![(_smbShare.FullName!.Length + 1)..];
|
||||
(containers, elements) = _smbShare.ListFolder(this, _smbShare.Name, path);
|
||||
(containers, elements) = await _smbShare.ListFolder(this, _smbShare.Name, path);
|
||||
}
|
||||
catch { }
|
||||
|
||||
@@ -126,7 +92,23 @@ namespace FileTime.Providers.Smb
|
||||
_elements = elements.AsReadOnly();
|
||||
|
||||
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
|
||||
Refreshed?.Invoke(this, EventArgs.Empty);
|
||||
await Refreshed?.InvokeAsync(this, AsyncEventArgs.Empty);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
|
||||
{
|
||||
if (_items == null) await Refresh();
|
||||
return _items;
|
||||
}
|
||||
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
|
||||
{
|
||||
if (_containers == null) await Refresh();
|
||||
return _containers;
|
||||
}
|
||||
public async Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
|
||||
{
|
||||
if (_elements == null) await Refresh();
|
||||
return _elements;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Net;
|
||||
using AsyncEvent;
|
||||
using FileTime.Core.Interactions;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
@@ -13,31 +14,11 @@ namespace FileTime.Providers.Smb
|
||||
private string? _password;
|
||||
|
||||
private IReadOnlyList<IContainer>? _shares;
|
||||
private IReadOnlyList<IItem>? _items;
|
||||
private readonly IReadOnlyList<IElement>? _elements = new List<IElement>().AsReadOnly();
|
||||
private ISMBClient? _client;
|
||||
private readonly IInputInterface _inputInterface;
|
||||
|
||||
public IReadOnlyList<IItem> Items
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_shares == null) Refresh();
|
||||
return _shares!;
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<IContainer> Containers
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_shares == null) Refresh();
|
||||
return _shares!;
|
||||
}
|
||||
|
||||
private set => _shares = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IElement> Elements { get; } = new List<IElement>().AsReadOnly();
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string? FullName { get; }
|
||||
@@ -48,7 +29,7 @@ namespace FileTime.Providers.Smb
|
||||
|
||||
IContentProvider IItem.Provider => Provider;
|
||||
|
||||
public event EventHandler? Refreshed;
|
||||
public AsyncEventHandler Refreshed { get; } = new();
|
||||
|
||||
public SmbServer(string path, SmbContentProvider contentProvider, IInputInterface inputInterface)
|
||||
{
|
||||
@@ -58,43 +39,60 @@ namespace FileTime.Providers.Smb
|
||||
FullName = Name = path;
|
||||
}
|
||||
|
||||
public IContainer CreateContainer(string name)
|
||||
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
|
||||
{
|
||||
if (_shares == null) await Refresh();
|
||||
return _shares;
|
||||
}
|
||||
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
|
||||
{
|
||||
if (_shares == null) await Refresh();
|
||||
return _shares;
|
||||
}
|
||||
public Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
|
||||
{
|
||||
return Task.FromResult(_elements);
|
||||
}
|
||||
|
||||
public Task<IContainer> CreateContainer(string name)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public IElement CreateElement(string name)
|
||||
public Task<IElement> CreateElement(string name)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
public Task Delete()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public IItem? GetByPath(string path)
|
||||
public Task<IItem?> GetByPath(string path)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IContainer? GetParent() => Provider;
|
||||
|
||||
public bool IsExists(string name)
|
||||
public Task<bool> IsExists(string name)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
public async Task Refresh()
|
||||
{
|
||||
ISMBClient client = GetSmbClient();
|
||||
ISMBClient client = await GetSmbClient();
|
||||
|
||||
List<string> shares = client.ListShares(out var status);
|
||||
|
||||
_shares = shares.ConvertAll(s => new SmbShare(s, Provider, this, GetSmbClient)).AsReadOnly();
|
||||
Refreshed?.Invoke(this, EventArgs.Empty);
|
||||
_items = _shares.Cast<IItem>().ToList().AsReadOnly();
|
||||
await Refreshed?.InvokeAsync(this, AsyncEventArgs.Empty);
|
||||
}
|
||||
|
||||
private ISMBClient GetSmbClient()
|
||||
private async Task<ISMBClient> GetSmbClient()
|
||||
{
|
||||
if (_client == null)
|
||||
{
|
||||
@@ -108,7 +106,7 @@ namespace FileTime.Providers.Smb
|
||||
{
|
||||
if (_username == null && _password == null)
|
||||
{
|
||||
var inputs = _inputInterface.ReadInputs(
|
||||
var inputs = await _inputInterface.ReadInputs(
|
||||
new InputElement[]
|
||||
{
|
||||
new InputElement($"Username for '{Name}'", InputType.Text),
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using AsyncEvent;
|
||||
using FileTime.Core.Interactions;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
@@ -11,42 +12,9 @@ namespace FileTime.Providers.Smb
|
||||
private IReadOnlyList<IItem>? _items;
|
||||
private IReadOnlyList<IContainer>? _containers;
|
||||
private IReadOnlyList<IElement>? _elements;
|
||||
private Func<ISMBClient> _getSmbClient;
|
||||
private Func<Task<ISMBClient>> _getSmbClient;
|
||||
private readonly IContainer? _parent;
|
||||
|
||||
public IReadOnlyList<IItem> Items
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_items == null) Refresh();
|
||||
return _items!;
|
||||
}
|
||||
|
||||
private set => _items = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IContainer> Containers
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_containers == null) Refresh();
|
||||
return _containers!;
|
||||
}
|
||||
|
||||
private set => _containers = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IElement> Elements
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_elements == null) Refresh();
|
||||
return _elements!;
|
||||
}
|
||||
|
||||
private set => _elements = value;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string? FullName { get; }
|
||||
@@ -56,9 +24,9 @@ namespace FileTime.Providers.Smb
|
||||
public SmbContentProvider Provider { get; }
|
||||
IContentProvider IItem.Provider => Provider;
|
||||
|
||||
public event EventHandler? Refreshed;
|
||||
public AsyncEventHandler Refreshed { get; } = new();
|
||||
|
||||
public SmbShare(string name, SmbContentProvider contentProvider, IContainer parent, Func<ISMBClient> getSmbClient)
|
||||
public SmbShare(string name, SmbContentProvider contentProvider, IContainer parent, Func<Task<ISMBClient>> getSmbClient)
|
||||
{
|
||||
_parent = parent;
|
||||
_getSmbClient = getSmbClient;
|
||||
@@ -68,26 +36,42 @@ namespace FileTime.Providers.Smb
|
||||
Provider = contentProvider;
|
||||
}
|
||||
|
||||
public IContainer CreateContainer(string name)
|
||||
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
|
||||
{
|
||||
if (_items == null) await Refresh();
|
||||
return _items;
|
||||
}
|
||||
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
|
||||
{
|
||||
if (_containers == null) await Refresh();
|
||||
return _containers;
|
||||
}
|
||||
public async Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
|
||||
{
|
||||
if (_elements == null) await Refresh();
|
||||
return _elements;
|
||||
}
|
||||
|
||||
public Task<IContainer> CreateContainer(string name)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IElement CreateElement(string name)
|
||||
public Task<IElement> CreateElement(string name)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
public Task Delete()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IItem? GetByPath(string path)
|
||||
public async Task<IItem?> GetByPath(string path)
|
||||
{
|
||||
var paths = path.Split(Constants.SeparatorChar);
|
||||
|
||||
var item = Items.FirstOrDefault(i => i.Name == paths[0]);
|
||||
var item = (await GetItems())?.FirstOrDefault(i => i.Name == paths[0]);
|
||||
|
||||
if (paths.Length == 1)
|
||||
{
|
||||
@@ -96,7 +80,7 @@ namespace FileTime.Providers.Smb
|
||||
|
||||
if (item is IContainer container)
|
||||
{
|
||||
return container.GetByPath(string.Join(Constants.SeparatorChar, paths.Skip(1)));
|
||||
return await container.GetByPath(string.Join(Constants.SeparatorChar, paths.Skip(1)));
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -104,19 +88,19 @@ namespace FileTime.Providers.Smb
|
||||
|
||||
public IContainer? GetParent() => _parent;
|
||||
|
||||
public bool IsExists(string name)
|
||||
public Task<bool> IsExists(string name)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
public async Task Refresh()
|
||||
{
|
||||
var containers = new List<IContainer>();
|
||||
var elements = new List<IElement>();
|
||||
|
||||
try
|
||||
{
|
||||
(containers, elements) = ListFolder(this, Name, string.Empty);
|
||||
(containers, elements) = await ListFolder(this, Name, string.Empty);
|
||||
}
|
||||
catch { }
|
||||
|
||||
@@ -124,15 +108,15 @@ namespace FileTime.Providers.Smb
|
||||
_elements = elements.AsReadOnly();
|
||||
|
||||
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
|
||||
Refreshed?.Invoke(this, EventArgs.Empty);
|
||||
await Refreshed?.InvokeAsync(this, AsyncEventArgs.Empty);
|
||||
}
|
||||
|
||||
public (List<IContainer> containers, List<IElement> elements) ListFolder(IContainer parent, string shareName, string folderName)
|
||||
public async Task<(List<IContainer> containers, List<IElement> elements)> ListFolder(IContainer parent, string shareName, string folderName)
|
||||
{
|
||||
var containers = new List<IContainer>();
|
||||
var elements = new List<IElement>();
|
||||
|
||||
var client = _getSmbClient();
|
||||
var client = await _getSmbClient();
|
||||
ISMBFileStore fileStore = client.TreeConnect(shareName, out var status);
|
||||
if (status == NTStatus.STATUS_SUCCESS)
|
||||
{
|
||||
@@ -148,11 +132,11 @@ namespace FileTime.Providers.Smb
|
||||
{
|
||||
if ((fileDirectoryInformation.FileAttributes & SMBLibrary.FileAttributes.Directory) == SMBLibrary.FileAttributes.Directory)
|
||||
{
|
||||
containers.Add(new SmbFolder(fileDirectoryInformation.FileName, Provider, this, parent, _getSmbClient));
|
||||
containers.Add(new SmbFolder(fileDirectoryInformation.FileName, Provider, this, parent));
|
||||
}
|
||||
else
|
||||
{
|
||||
elements.Add(new SmbFile(fileDirectoryInformation.FileName, Provider, parent, _getSmbClient));
|
||||
elements.Add(new SmbFile(fileDirectoryInformation.FileName, Provider, parent));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user