Smb content provider

This commit is contained in:
2022-01-08 11:32:52 +01:00
parent a0c28eb749
commit 8cb0675254
21 changed files with 730 additions and 45 deletions

View File

@@ -3,6 +3,7 @@ using FileTime.Core.Command;
using FileTime.Core.Providers;
using FileTime.Core.StateManagement;
using FileTime.Providers.Local;
using FileTime.Providers.Smb;
using Microsoft.Extensions.DependencyInjection;
namespace FileTime.App.Core
@@ -15,8 +16,10 @@ namespace FileTime.App.Core
return serviceCollection
.AddSingleton<IClipboard, Clipboard.Clipboard>()
.AddSingleton<TopContainer>()
.AddSingleton<LocalContentProvider>()
.AddSingleton<IContentProvider, LocalContentProvider>(sp => sp.GetService<LocalContentProvider>() ?? throw new Exception($"No {nameof(LocalContentProvider)} instance found"))
.AddSingleton<IContentProvider, SmbContentProvider>()
.AddSingleton<ElementCreationStates>()
.AddSingleton<CommandExecutor>();
}

View File

@@ -11,6 +11,7 @@
<ItemGroup>
<ProjectReference Include="..\FileTime.App.Core\FileTime.App.Core.csproj" />
<ProjectReference Include="..\..\Providers\FileTime.Providers.Local\FileTime.Providers.Local.csproj" />
<ProjectReference Include="..\..\Providers\FileTime.Providers.Smb\FileTime.Providers.Smb.csproj" />
</ItemGroup>
</Project>

View File

@@ -267,7 +267,7 @@ namespace FileTime.ConsoleUI.App
private static bool AreKeysEqual(ConsoleKeyInfo keyInfo1, ConsoleKeyInfo keyInfo2) =>
keyInfo1.Key == keyInfo2.Key && keyInfo1.Modifiers == keyInfo2.Modifiers;
private static void MoveToIOLine(int left = 0)
public void MoveToIOLine(int left = 0)
{
Console.SetCursorPosition(left, Console.WindowHeight - 2);
}

View File

@@ -0,0 +1,35 @@
using FileTime.ConsoleUI.App.UI.Color;
using FileTime.Core.Interactions;
namespace FileTime.ConsoleUI.App.UI
{
public class ConsoleInputInterface : IInputInterface
{
private readonly Application _application;
private readonly IColoredConsoleRenderer _coloredConsoleRenderer;
private readonly ConsoleReader _consoleReader;
public ConsoleInputInterface(Application application, IColoredConsoleRenderer coloredConsoleRenderer, ConsoleReader consoleReader)
{
_application = application;
_coloredConsoleRenderer = coloredConsoleRenderer;
_consoleReader = consoleReader;
}
public string?[] ReadInputs(IEnumerable<InputElement> fields)
{
var results = new List<string?>();
_coloredConsoleRenderer.ResetColor();
foreach (var input in fields)
{
_application.MoveToIOLine();
_coloredConsoleRenderer.Write(input.Text + ": ");
results.Add(_consoleReader.ReadText(placeHolder: input.InputType == InputType.Password ? '*' : null));
}
return results.ToArray();
}
}
}

View File

@@ -10,7 +10,7 @@ namespace FileTime.ConsoleUI.App.UI
{
_coloredConsoleRenderer = coloredConsoleRenderer;
}
public string ReadText(int? maxLength = null, Action<string>? validator = null)
public string? ReadText(int? maxLength = null, Action<string>? validator = null, char? placeHolder = null)
{
var cursorVisible = false;
try
@@ -76,10 +76,10 @@ namespace FileTime.ConsoleUI.App.UI
validator?.Invoke(input);
Console.SetCursorPosition(currentConsoleLeft, currentConsoleTop);
_coloredConsoleRenderer.Write($"{{0,-{maxLength}}}", input);
_coloredConsoleRenderer.Write($"{{0,-{maxLength}}}", placeHolder == null ? input : new string((char)placeHolder, input.Length));
Console.SetCursorPosition(currentConsoleLeft + position, currentConsoleTop);
key = Console.ReadKey();
key = Console.ReadKey(true);
}
Console.CursorVisible = cursorVisible;

View File

@@ -173,7 +173,6 @@ namespace FileTime.ConsoleUI.App.UI
printedItemsCount = itemsToPrint.Count;
foreach (var item in itemsToPrint)
{
Console.SetCursorPosition(startX, startY + currentY++);
var namePart = item.Name.Length > maxTextWidth
? string.Concat(item.Name.AsSpan(0, maxTextWidth - 1), "~")
: item.Name;
@@ -183,6 +182,8 @@ namespace FileTime.ConsoleUI.App.UI
var container = item as IContainer;
var element = item as IElement;
attributePart = container != null ? "" + container.Items.Count : element!.GetPrimaryAttributeText();
IConsoleColor? backgroundColor = null;
IConsoleColor? foregroundColor = null;
@@ -220,11 +221,10 @@ namespace FileTime.ConsoleUI.App.UI
_coloredRenderer.BackgroundColor = backgroundColor;
_coloredRenderer.ForegroundColor = foregroundColor;
attributePart = container != null ? "" + container.Items.Count : element!.GetPrimaryAttributeText();
var text = string.Format($"{{0,-{elementWidth}}}", _paddingLeft + (isSelected ? " " : "") + namePart + _paddingRight);
text = string.Concat(text.AsSpan(0, text.Length - attributePart.Length - 1), " ", attributePart);
Console.SetCursorPosition(startX, startY + currentY++);
_coloredRenderer.Write(text);
_coloredRenderer.ResetColor();

View File

@@ -1,12 +1,11 @@
using FileTime.App.Core;
using FileTime.App.Core.Clipboard;
using FileTime.ConsoleUI.App;
using FileTime.ConsoleUI.App.UI;
using FileTime.ConsoleUI.App.UI.Color;
using FileTime.Core.Command;
using FileTime.Core.Interactions;
using FileTime.Core.Models;
using FileTime.Core.Providers;
using FileTime.Core.StateManagement;
using FileTime.Providers.Local;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@@ -19,37 +18,6 @@ namespace FileTime.ConsoleUI
public static void Main()
{
/* Console.Clear();
for (var x = 0; x < 16; x++)
{
for (var y = 0; y < 16; y++)
{
var i = x * 16 + y;
Console.Write("\u001b[48;5;{0}m{0,4}", i);
Console.ResetColor();
Console.Write(' ');
}
Console.WriteLine("\n");
}
return; */
/* var colors = new int[][]
{
new int[] {0,43,54},
new int[] {255,0,0},
new int[] {0,255,0},
new int[] {0,0,255},
};
foreach (var color in colors)
{
Console.Write($"\u001b[0m\u001b[48;2;{color[0]};{color[1]};{color[2]}mTESZT ");
Console.WriteLine($"\u001b[0m\u001b[38;2;{color[0]};{color[1]};{color[2]}mTESZT");
}
Console.WriteLine("\u001b[0m\u001b[48;5;0;38;5;14mASD");
return; */
var serviceProvider = CreateServiceProvider();
_logger = serviceProvider.GetService<ILogger<Program>>()!;
@@ -62,6 +30,7 @@ namespace FileTime.ConsoleUI
if (currentPossibleDirectory is IContainer container)
{
serviceProvider.GetService<TopContainer>();
coloredConsoleRenderer.Clear();
Console.CursorVisible = false;
@@ -103,10 +72,10 @@ namespace FileTime.ConsoleUI
return DependencyInjection.RegisterDefaultServices()
.AddLogging(/* (builder) => builder.AddConsole().AddDebug() */)
.AddSingleton<Application>()
.AddSingleton<IStyles>(new Styles(IsAnsiColorSupported()))
.AddSingleton<IColoredConsoleRenderer, ColoredConsoleRenderer>()
.AddSingleton<ConsoleReader>()
.AddSingleton<IInputInterface, ConsoleInputInterface>()
.AddTransient<Render>()
.RegisterCommandHandlers()

View File

@@ -55,6 +55,10 @@ namespace FileTime.Core.Components
{
CurrentSelectedItem = CurrentLocation.Items.FirstOrDefault(i => i.FullName == currentSelectedName) ?? currentLocation.Items.FirstOrDefault();
}
else if (CurrentLocation.Items.Count > 0)
{
CurrentSelectedItem = CurrentLocation.Items[0];
}
}
public void SelectFirstItem()

View File

@@ -0,0 +1,7 @@
namespace FileTime.Core.Interactions
{
public interface IInputInterface
{
string?[] ReadInputs(IEnumerable<InputElement> fields);
}
}

View File

@@ -0,0 +1,14 @@
namespace FileTime.Core.Interactions
{
public class InputElement
{
public string Text { get; }
public InputType InputType { get; }
public InputElement(string text, InputType inputType)
{
Text = text;
InputType = inputType;
}
}
}

View File

@@ -0,0 +1,8 @@
namespace FileTime.Core.Interactions
{
public enum InputType
{
Text,
Password
}
}

View File

@@ -5,5 +5,9 @@ namespace FileTime.Core.Providers
public interface IContentProvider : IContainer
{
IReadOnlyList<IContainer> RootContainers { get; }
bool CanHandlePath(string path);
void SetParent(IContainer container);
}
}

View File

@@ -0,0 +1,54 @@
using FileTime.Core.Models;
namespace FileTime.Core.Providers
{
public class TopContainer : IContainer
{
private readonly List<IContentProvider> _contentProviders;
public IReadOnlyList<IItem> Items => Containers;
public IReadOnlyList<IContainer> Containers { get; }
public IReadOnlyList<IElement> Elements { get; } = new List<IElement>().AsReadOnly();
public string Name => null;
public string? FullName => null;
public bool IsHidden => false;
public IContentProvider Provider => null;
public event EventHandler? Refreshed;
public TopContainer(IEnumerable<IContentProvider> contentProviders)
{
_contentProviders = new List<IContentProvider>(contentProviders);
Containers = _contentProviders.AsReadOnly();
foreach (var contentProvider in contentProviders)
{
contentProvider.SetParent(this);
}
}
public IContainer CreateContainer(string name) => throw new NotImplementedException();
public IElement CreateElement(string name) => throw new NotImplementedException();
public void Delete() => throw new NotImplementedException();
public IItem? GetByPath(string path) => throw new NotImplementedException();
public IContainer? GetParent() => null;
public bool IsExists(string name) => throw new NotImplementedException();
public void Refresh()
{
}
}
}

View File

@@ -27,6 +27,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{38B1B927-4
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileTime.App.DependencyInjection", "AppCommon\FileTime.App.DependencyInjection\FileTime.App.DependencyInjection.csproj", "{F46D6CE5-4811-45B8-9CD4-3C993318A2E6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileTime.Providers.Smb", "Providers\FileTime.Providers.Smb\FileTime.Providers.Smb.csproj", "{35963404-39F6-4CDA-A0FE-A91036FA8DAC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -201,6 +203,26 @@ Global
{F46D6CE5-4811-45B8-9CD4-3C993318A2E6}.Release|x64.Build.0 = Release|Any CPU
{F46D6CE5-4811-45B8-9CD4-3C993318A2E6}.Release|x86.ActiveCfg = Release|Any CPU
{F46D6CE5-4811-45B8-9CD4-3C993318A2E6}.Release|x86.Build.0 = Release|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Debug|ARM.ActiveCfg = Debug|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Debug|ARM.Build.0 = Debug|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Debug|ARM64.Build.0 = Debug|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Debug|x64.ActiveCfg = Debug|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Debug|x64.Build.0 = Debug|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Debug|x86.ActiveCfg = Debug|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Debug|x86.Build.0 = Debug|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Release|Any CPU.Build.0 = Release|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Release|ARM.ActiveCfg = Release|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Release|ARM.Build.0 = Release|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Release|ARM64.ActiveCfg = Release|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Release|ARM64.Build.0 = Release|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Release|x64.ActiveCfg = Release|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Release|x64.Build.0 = Release|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Release|x86.ActiveCfg = Release|Any CPU
{35963404-39F6-4CDA-A0FE-A91036FA8DAC}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -214,6 +236,7 @@ Global
{92961CA3-ECAB-4920-95CA-F37E8F3EFDFA} = {D4C7E692-53C0-4423-9944-E25FE3D51BA2}
{BEA824B0-7684-44FF-95BF-A75E92A36C9F} = {D4C7E692-53C0-4423-9944-E25FE3D51BA2}
{F46D6CE5-4811-45B8-9CD4-3C993318A2E6} = {D4C7E692-53C0-4423-9944-E25FE3D51BA2}
{35963404-39F6-4CDA-A0FE-A91036FA8DAC} = {517D96CE-A956-4638-A93D-465D34DE22B1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8D679DCE-AC84-4A91-BFED-8F8D8E1D8183}

View File

@@ -8,6 +8,7 @@ namespace FileTime.Providers.Local
public class LocalContentProvider : IContentProvider
{
private readonly ILogger<LocalContentProvider> _logger;
private IContainer _parent = null;
public IReadOnlyList<IContainer> RootContainers { get; }
@@ -61,10 +62,7 @@ namespace FileTime.Providers.Local
{
}
public IContainer? GetParent()
{
return null;
}
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);
@@ -72,5 +70,12 @@ namespace FileTime.Providers.Local
public void 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 void SetParent(IContainer container)
{
_parent = container;
}
}
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Core\FileTime.Core\FileTime.Core.csproj"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="SMBLibrary" Version="1.4.8"/>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,82 @@
using FileTime.Core.Interactions;
using FileTime.Core.Models;
using FileTime.Core.Providers;
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>();
public string Name { get; } = "smb";
public string? FullName { get; }
public bool IsHidden => false;
public IContentProvider Provider => this;
public event EventHandler? Refreshed;
public SmbContentProvider(IInputInterface inputInterface)
{
_rootContainers = new List<IContainer>();
RootContainers = _rootContainers.AsReadOnly();
_inputInterface = inputInterface;
}
public IContainer CreateContainer(string name)
{
var fullName = "\\\\" + name;
var container = _rootContainers.Find(c => c.Name == name);
if (container == null)
{
container = new SmbServer(fullName, this, _inputInterface);
_rootContainers.Add(container);
}
Refresh();
return container;
}
public IElement CreateElement(string name)
{
throw new NotSupportedException();
}
public void Delete()
{
throw new NotSupportedException();
}
public IItem? GetByPath(string path)
{
throw new NotImplementedException();
}
public IContainer? GetParent() => _parent;
public bool IsExists(string name) => Items.Any(i => i.Name == name);
public void Refresh()
{
Refreshed?.Invoke(this, EventArgs.Empty);
}
public bool CanHandlePath(string path) => path.StartsWith("smb://") || path.StartsWith(@"\\");
public void SetParent(IContainer container) => _parent = container;
}
}

View File

@@ -0,0 +1,40 @@
using FileTime.Core.Models;
using FileTime.Core.Providers;
using SMBLibrary.Client;
namespace FileTime.Providers.Smb
{
public class SmbFile : IElement
{
public bool IsSpecial => false;
public string Name { get; }
public string? FullName { get; }
public bool IsHidden => false;
public IContentProvider Provider { get; }
private readonly Func<ISMBClient> _getSmbClient;
public SmbFile(string name, SmbContentProvider provider, IContainer parent, Func<ISMBClient> getSmbClient)
{
Name = name;
FullName = parent.FullName + Constants.SeparatorChar + Name;
Provider = provider;
_getSmbClient = getSmbClient;
}
public void Delete()
{
throw new NotImplementedException();
}
public string GetPrimaryAttributeText()
{
return "";
}
}
}

View File

@@ -0,0 +1,132 @@
using FileTime.Core.Models;
using FileTime.Core.Providers;
using SMBLibrary;
using SMBLibrary.Client;
namespace FileTime.Providers.Smb
{
public class SmbFolder : IContainer
{
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; }
public bool IsHidden => false;
public SmbContentProvider Provider { get; }
IContentProvider IItem.Provider => Provider;
public event EventHandler? Refreshed;
public SmbFolder(string name, SmbContentProvider contentProvider, SmbShare smbShare, IContainer parent, Func<ISMBClient> getSmbClient)
{
_parent = parent;
_getSmbClient = getSmbClient;
Name = name;
FullName = parent?.FullName == null ? Name : parent.FullName + Constants.SeparatorChar + Name;
Provider = contentProvider;
_smbShare = smbShare;
}
public IContainer CreateContainer(string name)
{
throw new NotImplementedException();
}
public IElement CreateElement(string name)
{
throw new NotImplementedException();
}
public void Delete()
{
throw new NotImplementedException();
}
public IItem? GetByPath(string path)
{
var paths = path.Split(Constants.SeparatorChar);
var item = Items.FirstOrDefault(i => i.Name == paths[0]);
if (paths.Length == 1)
{
return item;
}
if (item is IContainer container)
{
return container.GetByPath(string.Join(Constants.SeparatorChar, paths.Skip(1)));
}
return null;
}
public IContainer? GetParent() => _parent;
public bool IsExists(string name)
{
throw new NotImplementedException();
}
public void Refresh()
{
var containers = new List<IContainer>();
var elements = new List<IElement>();
try
{
var path = FullName![(_smbShare.FullName!.Length + 1)..];
(containers, elements) = _smbShare.ListFolder(this, _smbShare.Name, path);
}
catch { }
_containers = containers.AsReadOnly();
_elements = elements.AsReadOnly();
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
Refreshed?.Invoke(this, EventArgs.Empty);
}
}
}

View File

@@ -0,0 +1,126 @@
using FileTime.Core.Interactions;
using FileTime.Core.Models;
using FileTime.Core.Providers;
using SMBLibrary;
using SMBLibrary.Client;
namespace FileTime.Providers.Smb
{
public class SmbServer : IContainer
{
private string? _username;
private string? _password;
private IReadOnlyList<IContainer>? _shares;
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; }
public bool IsHidden => false;
public SmbContentProvider Provider { get; }
IContentProvider IItem.Provider => Provider;
public event EventHandler? Refreshed;
public SmbServer(string path, SmbContentProvider contentProvider, IInputInterface inputInterface)
{
_inputInterface = inputInterface;
Provider = contentProvider;
FullName = Name = path;
}
public IContainer CreateContainer(string name)
{
throw new NotSupportedException();
}
public IElement CreateElement(string name)
{
throw new NotSupportedException();
}
public void Delete()
{
}
public IItem? GetByPath(string path)
{
throw new NotImplementedException();
}
public IContainer? GetParent() => Provider;
public bool IsExists(string name)
{
throw new NotImplementedException();
}
public void Refresh()
{
ISMBClient client = GetSmbClient();
List<string> shares = new List<string>(); //client.ListShares(out var status);
_shares = shares.ConvertAll(s => new SmbShare(s, Provider, this, GetSmbClient)).AsReadOnly();
Refreshed?.Invoke(this, EventArgs.Empty);
}
private ISMBClient GetSmbClient()
{
if (_client == null)
{
_client = new SMB2Client();
if (_client.Connect(Name[2..], SMBTransportType.DirectTCPTransport))
{
if (_username == null && _password == null)
{
var inputs = _inputInterface.ReadInputs(
new InputElement[]
{
new InputElement($"Username for '{Name}'", InputType.Text),
new InputElement($"Password for '{Name}'", InputType.Password)
});
_username = inputs[0];
_password = inputs[1];
}
if (_client.Login(string.Empty, _username, _password) != NTStatus.STATUS_SUCCESS)
{
_username = null;
_password = null;
}
}
}
return _client;
}
}
}

View File

@@ -0,0 +1,165 @@
using FileTime.Core.Interactions;
using FileTime.Core.Models;
using FileTime.Core.Providers;
using SMBLibrary;
using SMBLibrary.Client;
namespace FileTime.Providers.Smb
{
public class SmbShare : IContainer
{
private IReadOnlyList<IItem>? _items;
private IReadOnlyList<IContainer>? _containers;
private IReadOnlyList<IElement>? _elements;
private Func<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; }
public bool IsHidden => false;
public SmbContentProvider Provider { get; }
IContentProvider IItem.Provider => Provider;
public event EventHandler? Refreshed;
public SmbShare(string name, SmbContentProvider contentProvider, IContainer parent, Func<ISMBClient> getSmbClient)
{
_parent = parent;
_getSmbClient = getSmbClient;
Name = name;
FullName = parent?.FullName == null ? Name : parent.FullName + Constants.SeparatorChar + Name;
Provider = contentProvider;
}
public IContainer CreateContainer(string name)
{
throw new NotImplementedException();
}
public IElement CreateElement(string name)
{
throw new NotImplementedException();
}
public void Delete()
{
throw new NotImplementedException();
}
public IItem? GetByPath(string path)
{
var paths = path.Split(Constants.SeparatorChar);
var item = Items.FirstOrDefault(i => i.Name == paths[0]);
if (paths.Length == 1)
{
return item;
}
if (item is IContainer container)
{
return container.GetByPath(string.Join(Constants.SeparatorChar, paths.Skip(1)));
}
return null;
}
public IContainer? GetParent() => _parent;
public bool IsExists(string name)
{
throw new NotImplementedException();
}
public void Refresh()
{
var containers = new List<IContainer>();
var elements = new List<IElement>();
try
{
(containers, elements) = ListFolder(this, Name, string.Empty);
}
catch { }
_containers = containers.AsReadOnly();
_elements = elements.AsReadOnly();
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
Refreshed?.Invoke(this, EventArgs.Empty);
}
public (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();
ISMBFileStore fileStore = client.TreeConnect(shareName, out var status);
if (status == NTStatus.STATUS_SUCCESS)
{
status = fileStore.CreateFile(out object directoryHandle, out FileStatus fileStatus, folderName, AccessMask.GENERIC_READ, SMBLibrary.FileAttributes.Directory, ShareAccess.Read | ShareAccess.Write, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null);
if (status == NTStatus.STATUS_SUCCESS)
{
status = fileStore.QueryDirectory(out List<QueryDirectoryFileInformation> fileList, directoryHandle, "*", FileInformationClass.FileDirectoryInformation);
status = fileStore.CloseFile(directoryHandle);
foreach (var item in fileList)
{
if (item is FileDirectoryInformation fileDirectoryInformation && fileDirectoryInformation.FileName != "." && fileDirectoryInformation.FileName != "..")
{
if ((fileDirectoryInformation.FileAttributes & SMBLibrary.FileAttributes.Directory) == SMBLibrary.FileAttributes.Directory)
{
containers.Add(new SmbFolder(fileDirectoryInformation.FileName, Provider, this, parent, _getSmbClient));
}
else
{
elements.Add(new SmbFile(fileDirectoryInformation.FileName, Provider, parent, _getSmbClient));
}
}
}
}
}
return (containers, elements);
}
}
}