AbstractFolder refactor

This commit is contained in:
2022-02-18 16:31:13 +01:00
parent 5072d828e6
commit 83b86f2b50
9 changed files with 84 additions and 364 deletions

View File

@@ -2,8 +2,8 @@ namespace FileTime.Core.Models
{ {
public enum SupportsDelete public enum SupportsDelete
{ {
False,
True, True,
HardDeleteOnly, HardDeleteOnly,
False
} }
} }

View File

@@ -46,7 +46,7 @@ namespace FileTime.Core.Providers
Exceptions = _exceptions.AsReadOnly(); Exceptions = _exceptions.AsReadOnly();
} }
public abstract Task<bool> CanOpenAsync(); public virtual Task<bool> CanOpenAsync() => Task.FromResult(_exceptions.Count == 0);
public abstract Task<IContainer> CloneAsync(); public abstract Task<IContainer> CloneAsync();
@@ -61,6 +61,7 @@ namespace FileTime.Core.Providers
_items = null; _items = null;
_containers = null; _containers = null;
_elements = null; _elements = null;
IsLoaded = false;
IsDestroyed = true; IsDestroyed = true;
Refreshed = new AsyncEventHandler(); Refreshed = new AsyncEventHandler();
} }
@@ -93,6 +94,7 @@ namespace FileTime.Core.Providers
public virtual async Task RefreshAsync(CancellationToken token = default) public virtual async Task RefreshAsync(CancellationToken token = default)
{ {
_exceptions.Clear();
var containers = new List<IContainer>(); var containers = new List<IContainer>();
var elements = new List<IElement>(); var elements = new List<IElement>();
foreach (var item in await RefreshItems(token)) foreach (var item in await RefreshItems(token))
@@ -118,8 +120,10 @@ namespace FileTime.Core.Providers
_containers = containers.OrderBy(c => c.Name).ToList().AsReadOnly(); _containers = containers.OrderBy(c => c.Name).ToList().AsReadOnly();
_elements = elements.OrderBy(e => e.Name).ToList().AsReadOnly(); _elements = elements.OrderBy(e => e.Name).ToList().AsReadOnly();
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly(); _items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
IsLoaded = true;
if (Refreshed != null) await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty, token); if (Refreshed != null) await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty, token);
} }
public abstract Task<IEnumerable<IItem>> RefreshItems(CancellationToken token = default); public abstract Task<IEnumerable<IItem>> RefreshItems(CancellationToken token = default);
public abstract Task Rename(string newName); public abstract Task Rename(string newName);
@@ -129,6 +133,9 @@ namespace FileTime.Core.Providers
_items = null; _items = null;
_containers = null; _containers = null;
_elements = null; _elements = null;
IsLoaded = false;
} }
protected void AddException(Exception e) => _exceptions.Add(e);
} }
} }

View File

@@ -1,5 +1,3 @@
using System;
using AsyncEvent; using AsyncEvent;
using FileTime.Core.Models; using FileTime.Core.Models;

View File

@@ -1,55 +1,25 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using AsyncEvent;
using FileTime.Core.Models; using FileTime.Core.Models;
using FileTime.Core.Providers; using FileTime.Core.Providers;
using FileTime.Providers.Local.Interop; using FileTime.Providers.Local.Interop;
namespace FileTime.Providers.Local namespace FileTime.Providers.Local
{ {
public class LocalFolder : IContainer public class LocalFolder : AbstractContainer<LocalContentProvider>
{ {
private IReadOnlyList<IItem>? _items;
private IReadOnlyList<IContainer>? _containers;
private IReadOnlyList<IElement>? _elements;
private readonly List<Exception> _exceptions;
private readonly IContainer? _parent;
public bool IsHidden => (Directory.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
public DirectoryInfo Directory { get; } public DirectoryInfo Directory { get; }
public LocalContentProvider Provider { get; }
IContentProvider IItem.Provider => Provider;
public string Name { get; }
public string FullName { get; }
public string? NativePath => Directory.FullName;
public bool IsLoaded => _items != null;
public SupportsDelete CanDelete { get; }
public bool CanRename => true;
public AsyncEventHandler Refreshed { get; private set; } = new();
public string Attributes => GetAttributes(); public string Attributes => GetAttributes();
public DateTime CreatedAt => Directory.CreationTime; public DateTime CreatedAt => Directory.CreationTime;
public IReadOnlyList<Exception> Exceptions { get; }
public bool IsDestroyed { get; private set; } public LocalFolder(DirectoryInfo directory, LocalContentProvider contentProvider, IContainer parent)
: base(contentProvider, parent, directory.Name.TrimEnd(Path.DirectorySeparatorChar))
public bool SupportsDirectoryLevelSoftDelete { get; }
public LocalFolder(DirectoryInfo directory, LocalContentProvider contentProvider, IContainer? parent)
{ {
Directory = directory; Directory = directory;
_parent = parent; NativePath = Directory.FullName;
IsHidden = (Directory.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
_exceptions = new List<Exception>(); CanRename = true;
Exceptions = _exceptions.AsReadOnly();
Name = directory.Name.TrimEnd(Path.DirectorySeparatorChar);
FullName = parent?.FullName == null ? Name : parent.FullName + Constants.SeparatorChar + Name;
Provider = contentProvider;
//TODO: Linux soft delete //TODO: Linux soft delete
SupportsDirectoryLevelSoftDelete = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); SupportsDirectoryLevelSoftDelete = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
@@ -58,58 +28,28 @@ namespace FileTime.Providers.Local
: SupportsDelete.HardDeleteOnly; : SupportsDelete.HardDeleteOnly;
} }
public IContainer? GetParent() => _parent; public override Task<IContainer> CloneAsync() => Task.FromResult((IContainer)new LocalFolder(Directory, Provider, GetParent()!));
public Task<IContainer> CloneAsync() => Task.FromResult((IContainer)new LocalFolder(Directory, Provider, _parent)); public override Task<IEnumerable<IItem>> RefreshItems(CancellationToken token = default)
public async Task RefreshAsync(CancellationToken token = default)
{ {
if (_items != null)
{
foreach (var item in _items)
{
item.Destroy();
}
}
_containers = new List<IContainer>();
_elements = new List<IElement>();
_items = new List<IItem>();
_exceptions.Clear();
try try
{ {
if (token.IsCancellationRequested) return; if (token.IsCancellationRequested) return Task.FromResult(Enumerable.Empty<IItem>());
_containers = Directory.GetDirectories().Select(d => new LocalFolder(d, Provider, this)).OrderBy(d => d.Name).ToList().AsReadOnly(); var containers = Directory.GetDirectories().Select(d => new LocalFolder(d, Provider, this)).OrderBy(d => d.Name).ToList().AsReadOnly();
if (token.IsCancellationRequested) return; if (token.IsCancellationRequested) return Task.FromResult(Enumerable.Empty<IItem>());
_elements = Directory.GetFiles().Select(f => new LocalFile(f, this, Provider)).OrderBy(f => f.Name).ToList().AsReadOnly(); var elements = Directory.GetFiles().Select(f => new LocalFile(f, this, Provider)).OrderBy(f => f.Name).ToList().AsReadOnly();
if (token.IsCancellationRequested) return; if (token.IsCancellationRequested) return Task.FromResult(Enumerable.Empty<IItem>());
return Task.FromResult(containers.Cast<IItem>().Concat(elements));
} }
catch (Exception e) catch (Exception e)
{ {
_exceptions.Add(e); AddException(e);
} }
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly(); return Task.FromResult(Enumerable.Empty<IItem>());
if (Refreshed != null) await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty, token);
}
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
{
if (_items == null) await RefreshAsync(token);
return _items;
}
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
{
if (_containers == null) await RefreshAsync(token);
return _containers;
}
public async Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
{
if (_elements == null) await RefreshAsync(token);
return _elements;
} }
public async Task<IItem?> GetByPath(string path, bool acceptDeepestMatch = false) public async Task<IItem?> GetByPath(string path, bool acceptDeepestMatch = false)
@@ -131,25 +71,25 @@ namespace FileTime.Providers.Local
return null; return null;
} }
public async Task<IContainer> CreateContainerAsync(string name) public override async Task<IContainer> CreateContainerAsync(string name)
{ {
Directory.CreateSubdirectory(name); Directory.CreateSubdirectory(name);
await RefreshAsync(); await RefreshAsync();
return _containers!.FirstOrDefault(c => Provider.NormalizePath(c.Name) == Provider.NormalizePath(name))!; return (await GetContainers())!.FirstOrDefault(c => Provider.NormalizePath(c.Name) == Provider.NormalizePath(name))!;
} }
public async Task<IElement> CreateElementAsync(string name) public override async Task<IElement> CreateElementAsync(string name)
{ {
using (File.Create(Path.Combine(Directory.FullName, name))) { } using (File.Create(Path.Combine(Directory.FullName, name))) { }
await RefreshAsync(); await RefreshAsync();
return _elements!.FirstOrDefault(e => Provider.NormalizePath(e.Name) == Provider.NormalizePath(name))!; return (await GetElements())!.FirstOrDefault(e => Provider.NormalizePath(e.Name) == Provider.NormalizePath(name))!;
} }
public async Task<bool> IsExistsAsync(string name) => (await GetItems())?.Any(i => Provider.NormalizePath(i.Name) == Provider.NormalizePath(name)) ?? false; public override async Task<bool> IsExistsAsync(string name) => (await GetItems())?.Any(i => Provider.NormalizePath(i.Name) == Provider.NormalizePath(name)) ?? false;
public Task Delete(bool hardDelete = false) public override Task Delete(bool hardDelete = false)
{ {
if (hardDelete) if (hardDelete)
{ {
@@ -161,12 +101,12 @@ namespace FileTime.Providers.Local
} }
return Task.CompletedTask; return Task.CompletedTask;
} }
public async Task Rename(string newName) public override async Task Rename(string newName)
{ {
if (_parent is LocalFolder parentFolder) if (GetParent() is LocalFolder parentFolder)
{ {
System.IO.Directory.Move(Directory.FullName, Path.Combine(parentFolder.Directory.FullName, newName)); System.IO.Directory.Move(Directory.FullName, Path.Combine(parentFolder.Directory.FullName, newName));
await _parent.RefreshAsync(); await parentFolder.RefreshAsync();
} }
} }
@@ -185,22 +125,5 @@ namespace FileTime.Providers.Local
+ ((Directory.Attributes & FileAttributes.System) == FileAttributes.System ? "s" : "-"); + ((Directory.Attributes & FileAttributes.System) == FileAttributes.System ? "s" : "-");
} }
} }
public Task<bool> CanOpenAsync() => Task.FromResult(true);
public void Destroy()
{
_items = null;
_containers = null;
_elements = null;
IsDestroyed = true;
Refreshed = new AsyncEventHandler();
}
public void Unload()
{
_items = null;
_containers = null;
_elements = null;
}
} }
} }

View File

@@ -12,7 +12,6 @@ namespace FileTime.Providers.Sftp
_server = server; _server = server;
NativePath = FullName; NativePath = FullName;
} }
public override Task<bool> CanOpenAsync() => Task.FromResult(true);
public override Task<IContainer> CloneAsync() public override Task<IContainer> CloneAsync()
{ {

View File

@@ -35,8 +35,6 @@ namespace FileTime.Providers.Sftp
public override async Task<IEnumerable<IItem>> RefreshItems(CancellationToken token = default) => await ListDirectory(""); public override async Task<IEnumerable<IItem>> RefreshItems(CancellationToken token = default) => await ListDirectory("");
public override Task<bool> CanOpenAsync() => Task.FromResult(true);
public override Task<IContainer> CloneAsync() => Task.FromResult((IContainer)this); public override Task<IContainer> CloneAsync() => Task.FromResult((IContainer)this);
public override Task<IContainer> CreateContainerAsync(string name) public override Task<IContainer> CreateContainerAsync(string name)
@@ -78,8 +76,7 @@ namespace FileTime.Providers.Sftp
isClientNull = _client == null; isClientNull = _client == null;
} }
int reTries = 0; for (int reTries = 0; isClientNull; reTries++)
while (isClientNull)
{ {
if (!await RefreshSftpClient()) if (!await RefreshSftpClient())
{ {
@@ -95,7 +92,6 @@ namespace FileTime.Providers.Sftp
{ {
throw new Exception($"Could not connect to server {Name} after {reTries} retry"); throw new Exception($"Could not connect to server {Name} after {reTries} retry");
} }
reTries++;
} }
return _client!; return _client!;
} }

View File

@@ -1,80 +1,47 @@
using AsyncEvent;
using FileTime.Core.Models; using FileTime.Core.Models;
using FileTime.Core.Providers; using FileTime.Core.Providers;
using SMBLibrary; using SMBLibrary;
namespace FileTime.Providers.Smb namespace FileTime.Providers.Smb
{ {
public class SmbFolder : IContainer public class SmbFolder : AbstractContainer<SmbContentProvider>
{ {
private IReadOnlyList<IItem>? _items;
private IReadOnlyList<IContainer>? _containers;
private IReadOnlyList<IElement>? _elements;
private readonly IContainer? _parent;
private readonly SmbClientContext _smbClientContext; private readonly SmbClientContext _smbClientContext;
public string Name { get; }
public string? FullName { get; }
public string? NativePath { get; }
public bool IsHidden => false;
public bool IsLoaded => _items != null;
public SmbContentProvider Provider { get; }
IContentProvider IItem.Provider => Provider;
public SmbShare SmbShare { get; } public SmbShare SmbShare { get; }
public SupportsDelete CanDelete => SupportsDelete.True;
public bool CanRename => true;
public AsyncEventHandler Refreshed { get; } = new();
public IReadOnlyList<Exception> Exceptions { get; } = new List<Exception>().AsReadOnly();
public bool IsDestroyed { get; private set; }
public bool SupportsDirectoryLevelSoftDelete => false;
public SmbFolder(string name, SmbContentProvider contentProvider, SmbShare smbShare, IContainer parent, SmbClientContext smbClientContext) public SmbFolder(string name, SmbContentProvider contentProvider, SmbShare smbShare, IContainer parent, SmbClientContext smbClientContext)
: base(contentProvider, parent, name)
{ {
_parent = parent;
_smbClientContext = smbClientContext; _smbClientContext = smbClientContext;
SmbShare = smbShare; SmbShare = smbShare;
Name = name;
FullName = parent.FullName! + Constants.SeparatorChar + Name;
NativePath = parent.NativePath + SmbContentProvider.GetNativePathSeparator() + name; NativePath = parent.NativePath + SmbContentProvider.GetNativePathSeparator() + name;
Provider = contentProvider; CanDelete = SupportsDelete.True;
CanRename = true;
} }
public async Task<IContainer> CreateContainerAsync(string name) public override async Task<IContainer> CreateContainerAsync(string name)
{ {
var path = FullName![(SmbShare.FullName!.Length + 1)..] + Constants.SeparatorChar + name; var path = FullName![(SmbShare.FullName!.Length + 1)..] + Constants.SeparatorChar + name;
await SmbShare.CreateContainerWithPathAsync(path.Replace("/", "\\")); await SmbShare.CreateContainerWithPathAsync(path.Replace("/", "\\"));
await RefreshAsync(); await RefreshAsync();
return _containers!.FirstOrDefault(e => e.Name == name)!; return (await GetContainers())!.FirstOrDefault(e => e.Name == name)!;
} }
public async Task<IElement> CreateElementAsync(string name) public override async Task<IElement> CreateElementAsync(string name)
{ {
var path = FullName![(SmbShare.FullName!.Length + 1)..] + Constants.SeparatorChar + name; var path = FullName![(SmbShare.FullName!.Length + 1)..] + Constants.SeparatorChar + name;
await SmbShare.CreateElementWithPathAsync(path.Replace("/", "\\")); await SmbShare.CreateElementWithPathAsync(path.Replace("/", "\\"));
await RefreshAsync(); await RefreshAsync();
return _elements!.FirstOrDefault(e => e.Name == name)!; return (await GetElements())!.FirstOrDefault(e => e.Name == name)!;
} }
public Task<IContainer> CloneAsync() => Task.FromResult((IContainer)this); public override Task<IContainer> CloneAsync() => Task.FromResult((IContainer)new SmbFolder(Name, Provider, SmbShare, GetParent(), _smbClientContext));
public IContainer? GetParent() => _parent; public override async Task Delete(bool hardDelete = false)
public async Task<bool> IsExistsAsync(string name)
{
var items = await GetItems();
return items?.Any(i => i.Name == name) ?? false;
}
public async Task Delete(bool hardDelete = false)
{ {
await _smbClientContext.RunWithSmbClientAsync(client => await _smbClientContext.RunWithSmbClientAsync(client =>
{ {
@@ -103,62 +70,23 @@ namespace FileTime.Providers.Smb
status = fileStore.Disconnect(); status = fileStore.Disconnect();
}); });
} }
public Task Rename(string newName) public override Task Rename(string newName)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public async Task RefreshAsync(CancellationToken token = default) public override async Task<IEnumerable<IItem>> RefreshItems(CancellationToken token = default)
{ {
var containers = new List<IContainer>();
var elements = new List<IElement>();
try try
{ {
var path = FullName![(SmbShare.FullName!.Length + 1)..]; var path = FullName![(SmbShare.FullName!.Length + 1)..];
(containers, elements) = await SmbShare.ListFolder(this, path, token); var (containers, elements) = await SmbShare.ListFolder(this, path, token);
return containers.Cast<IItem>().Concat(elements);
} }
catch { } catch { }
_containers = containers.AsReadOnly(); return Enumerable.Empty<IItem>();
_elements = elements.AsReadOnly();
if (_items != null)
{
foreach (var item in _items)
{
item.Destroy();
}
}
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty, token);
}
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default)
{
if (_items == null) await RefreshAsync(token);
return _items;
}
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
{
if (_containers == null) await RefreshAsync(token);
return _containers;
}
public async Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
{
if (_elements == null) await RefreshAsync(token);
return _elements;
}
public Task<bool> CanOpenAsync() => Task.FromResult(true);
public void Destroy() => IsDestroyed = true;
public void Unload()
{
_items = null;
_containers = null;
_elements = null;
} }
private string GetPathFromShare() => FullName![(SmbShare.FullName!.Length + 1)..].Replace("/", "\\"); private string GetPathFromShare() => FullName![(SmbShare.FullName!.Length + 1)..].Replace("/", "\\");

View File

@@ -1,6 +1,5 @@
using System.Net; using System.Net;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using AsyncEvent;
using FileTime.Core.Interactions; using FileTime.Core.Interactions;
using FileTime.Core.Models; using FileTime.Core.Models;
using FileTime.Core.Providers; using FileTime.Core.Providers;
@@ -9,87 +8,46 @@ using SMBLibrary.Client;
namespace FileTime.Providers.Smb namespace FileTime.Providers.Smb
{ {
public class SmbServer : IContainer public class SmbServer : AbstractContainer<SmbContentProvider>
{ {
internal const int MAXRETRIES = 5; internal const int MAXRETRIES = 5;
private bool _reenterCredentials; private bool _reenterCredentials;
private IReadOnlyList<IContainer>? _shares;
private IReadOnlyList<IItem>? _items;
private readonly IReadOnlyList<IElement>? _elements = new List<IElement>().AsReadOnly();
private ISMBClient? _client; private ISMBClient? _client;
private readonly object _clientGuard = new(); private readonly object _clientGuard = new();
private bool _refreshingClient; private bool _refreshingClient;
private readonly IInputInterface _inputInterface; private readonly IInputInterface _inputInterface;
private readonly SmbClientContext _smbClientContext; private readonly SmbClientContext _smbClientContext;
private readonly List<Exception> _exceptions = new();
public string? Username { get; private set; } public string? Username { get; private set; }
public string? Password { get; private set; } public string? Password { get; private set; }
public string Name { get; } public SmbServer(string name, SmbContentProvider contentProvider, IInputInterface inputInterface, string? username = null, string? password = null)
public string? FullName { get; } : base(contentProvider, contentProvider, name)
public string? NativePath { get; }
public bool IsHidden => false;
public bool IsLoaded => _items != null;
public SmbContentProvider Provider { get; }
IContentProvider IItem.Provider => Provider;
public SupportsDelete CanDelete => SupportsDelete.True;
public bool CanRename => false;
public IReadOnlyList<Exception> Exceptions { get; }
public AsyncEventHandler Refreshed { get; } = new();
public bool SupportsDirectoryLevelSoftDelete => false;
public bool IsDestroyed => false;
public SmbServer(string path, SmbContentProvider contentProvider, IInputInterface inputInterface, string? username = null, string? password = null)
{ {
_inputInterface = inputInterface; _inputInterface = inputInterface;
_smbClientContext = new SmbClientContext(GetSmbClient, DisposeSmbClient); _smbClientContext = new SmbClientContext(GetSmbClient, DisposeSmbClient);
Exceptions = _exceptions.AsReadOnly();
Username = username; Username = username;
Password = password; Password = password;
CanDelete = SupportsDelete.True;
Provider = contentProvider;
Name = path;
FullName = contentProvider.Protocol + Name; FullName = contentProvider.Protocol + Name;
NativePath = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) NativePath = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? "\\\\" + Name ? "\\\\" + Name
: contentProvider.Protocol + Name; : contentProvider.Protocol + Name;
} }
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default) public override Task<IContainer> CreateContainerAsync(string name)
{
if (_shares == null) await RefreshAsync(token);
return _shares;
}
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
{
if (_shares == null) await RefreshAsync(token);
return _shares;
}
public Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
{
return Task.FromResult(_elements);
}
public Task<IContainer> CreateContainerAsync(string name)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
public Task<IElement> CreateElementAsync(string name) public override Task<IElement> CreateElementAsync(string name)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
public Task Delete(bool hardDelete = false) public override Task Delete(bool hardDelete = false)
{ {
return Task.CompletedTask; return Task.CompletedTask;
} }
@@ -114,31 +72,23 @@ namespace FileTime.Providers.Smb
return null; return null;
} }
public IContainer? GetParent() => Provider; public override async Task<IEnumerable<IItem>> RefreshItems(CancellationToken token = default)
public Task<bool> IsExistsAsync(string name)
{
throw new NotImplementedException();
}
public async Task RefreshAsync(CancellationToken token = default)
{ {
try try
{ {
_exceptions.Clear(); var shares = await _smbClientContext.RunWithSmbClientAsync((client) => client.ListShares(out var status), IsLoaded ? MAXRETRIES : 1);
List<string> shares = await _smbClientContext.RunWithSmbClientAsync((client) => client.ListShares(out var status), _shares == null ? 1 : MAXRETRIES);
_shares = shares.ConvertAll(s => new SmbShare(s, Provider, this, _smbClientContext)).AsReadOnly(); return shares.Select(s => new SmbShare(s, Provider, this, _smbClientContext));
_items = _shares.Cast<IItem>().ToList().AsReadOnly();
await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty, token);
} }
catch (Exception e) catch (Exception e)
{ {
_exceptions.Add(e); AddException(e);
} }
return Enumerable.Empty<IItem>();
} }
public Task<IContainer> CloneAsync() => Task.FromResult((IContainer)this); public override Task<IContainer> CloneAsync() => Task.FromResult((IContainer)this);
private void DisposeSmbClient() private void DisposeSmbClient()
{ {
@@ -156,8 +106,7 @@ namespace FileTime.Providers.Smb
isClientNull = _client == null; isClientNull = _client == null;
} }
int reTries = 0; for (var reTries = 0; isClientNull; reTries++)
while (isClientNull)
{ {
if (!await RefreshSmbClient()) if (!await RefreshSmbClient())
{ {
@@ -173,7 +122,6 @@ namespace FileTime.Providers.Smb
{ {
throw new Exception($"Could not connect to server {Name} after {reTries} retry"); throw new Exception($"Could not connect to server {Name} after {reTries} retry");
} }
reTries++;
} }
return _client!; return _client!;
} }
@@ -235,11 +183,7 @@ namespace FileTime.Providers.Smb
return true; return true;
} }
public Task Rename(string newName) => throw new NotSupportedException(); public override Task Rename(string newName) => throw new NotSupportedException();
public Task<bool> CanOpenAsync() => Task.FromResult(true); public override Task<bool> CanOpenAsync() => Task.FromResult(true);
public void Destroy() { }
public void Unload() { }
} }
} }

View File

@@ -1,4 +1,3 @@
using AsyncEvent;
using FileTime.Core.Models; using FileTime.Core.Models;
using FileTime.Core.Providers; using FileTime.Core.Providers;
using SMBLibrary; using SMBLibrary;
@@ -6,67 +5,24 @@ using SMBLibrary.Client;
namespace FileTime.Providers.Smb namespace FileTime.Providers.Smb
{ {
public class SmbShare : IContainer public class SmbShare : AbstractContainer<SmbContentProvider>
{ {
private IReadOnlyList<IItem>? _items;
private IReadOnlyList<IContainer>? _containers;
private IReadOnlyList<IElement>? _elements;
private readonly SmbClientContext _smbClientContext; private readonly SmbClientContext _smbClientContext;
private readonly IContainer? _parent;
public string Name { get; }
public string? FullName { get; }
public string? NativePath { get; }
public bool IsHidden => false;
public bool IsLoaded => _items != null;
public SmbContentProvider Provider { get; }
IContentProvider IItem.Provider => Provider;
public SupportsDelete CanDelete => SupportsDelete.False;
public bool CanRename => false;
public AsyncEventHandler Refreshed { get; } = new();
public IReadOnlyList<Exception> Exceptions { get; } = new List<Exception>().AsReadOnly();
public bool IsDestroyed => false;
public bool SupportsDirectoryLevelSoftDelete => false;
public SmbShare(string name, SmbContentProvider contentProvider, IContainer parent, SmbClientContext smbClientContext) public SmbShare(string name, SmbContentProvider contentProvider, IContainer parent, SmbClientContext smbClientContext)
: base(contentProvider, parent, name)
{ {
_parent = parent;
_smbClientContext = smbClientContext; _smbClientContext = smbClientContext;
Name = name;
FullName = parent.FullName! + Constants.SeparatorChar + Name;
NativePath = parent.NativePath + SmbContentProvider.GetNativePathSeparator() + name; NativePath = parent.NativePath + SmbContentProvider.GetNativePathSeparator() + name;
Provider = contentProvider; CanDelete = SupportsDelete.False;
} }
public async Task<IReadOnlyList<IItem>?> GetItems(CancellationToken token = default) public override async Task<IContainer> CreateContainerAsync(string name)
{
if (_items == null) await RefreshAsync(token);
return _items;
}
public async Task<IReadOnlyList<IContainer>?> GetContainers(CancellationToken token = default)
{
if (_containers == null) await RefreshAsync(token);
return _containers;
}
public async Task<IReadOnlyList<IElement>?> GetElements(CancellationToken token = default)
{
if (_elements == null) await RefreshAsync(token);
return _elements;
}
public async Task<IContainer> CreateContainerAsync(string name)
{ {
await CreateContainerWithPathAsync(name); await CreateContainerWithPathAsync(name);
await RefreshAsync(); await RefreshAsync();
return _containers!.FirstOrDefault(e => e.Name == name)!; return (await GetContainers())!.FirstOrDefault(e => e.Name == name)!;
} }
internal async Task CreateContainerWithPathAsync(string path) internal async Task CreateContainerWithPathAsync(string path)
{ {
@@ -101,12 +57,12 @@ namespace FileTime.Providers.Smb
}); });
} }
public async Task<IElement> CreateElementAsync(string name) public override async Task<IElement> CreateElementAsync(string name)
{ {
await CreateElementWithPathAsync(name); await CreateElementWithPathAsync(name);
await RefreshAsync(); await RefreshAsync();
return _elements!.FirstOrDefault(e => e.Name == name)!; return (await GetElements())!.FirstOrDefault(e => e.Name == name)!;
} }
internal async Task CreateElementWithPathAsync(string path) internal async Task CreateElementWithPathAsync(string path)
{ {
@@ -141,45 +97,24 @@ namespace FileTime.Providers.Smb
}); });
} }
public Task Delete(bool hardDelete = false) public override Task Delete(bool hardDelete = false)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
public IContainer? GetParent() => _parent; public override Task<IContainer> CloneAsync() => Task.FromResult((IContainer)this);
public Task<IContainer> CloneAsync() => Task.FromResult((IContainer)this); public override async Task<IEnumerable<IItem>> RefreshItems(CancellationToken token = default)
public async Task<bool> IsExistsAsync(string name)
{ {
var items = await GetItems();
return items?.Any(i => i.Name == name) ?? false;
}
public async Task RefreshAsync(CancellationToken token = default)
{
var containers = new List<IContainer>();
var elements = new List<IElement>();
try try
{ {
(containers, elements) = await ListFolder(this, string.Empty, token); var (containers, elements) = await ListFolder(this, string.Empty, token);
return containers.Cast<IItem>().Concat(elements);
} }
catch { } catch { }
if (_items != null) return Enumerable.Empty<IItem>();
{
foreach (var item in _items)
{
item.Destroy();
}
}
_containers = containers.AsReadOnly();
_elements = elements.AsReadOnly();
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
await Refreshed.InvokeAsync(this, AsyncEventArgs.Empty, token);
} }
public async Task<(List<IContainer> containers, List<IElement> elements)> ListFolder(IContainer parent, string folderName, CancellationToken token = default) public async Task<(List<IContainer> containers, List<IElement> elements)> ListFolder(IContainer parent, string folderName, CancellationToken token = default)
@@ -228,16 +163,6 @@ namespace FileTime.Providers.Smb
return client.TreeConnect(Name, out status); return client.TreeConnect(Name, out status);
} }
public Task Rename(string newName) => throw new NotSupportedException(); public override Task Rename(string newName) => throw new NotSupportedException();
public Task<bool> CanOpenAsync() => Task.FromResult(true);
public void Destroy() { }
public void Unload()
{
_items = null;
_containers = null;
_elements = null;
}
} }
} }