Skip to content

Commit

Permalink
Add smb file share. (#366)
Browse files Browse the repository at this point in the history
* Add smb file share.

* update smb library

* try

* revert

* test

* revert

* update

---------

Co-authored-by: Dibianco, Jason <[email protected]>
  • Loading branch information
dibiancoj and Dibianco, Jason authored Sep 20, 2024
1 parent 703359f commit 9961237
Show file tree
Hide file tree
Showing 14 changed files with 1,007 additions and 5 deletions.
22 changes: 21 additions & 1 deletion LibraryCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibraryCore.Aot.Json", "Src
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Aot", "Aot", "{896C5DB1-3EDC-4DCC-AAAB-73F705B52B18}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibraryCore.Tests.Aot.Json", "Tests\LibraryCore.Tests.Aot.Json\LibraryCore.Tests.Aot.Json.csproj", "{01CB3A66-9110-4BFA-9D7D-3117578E1575}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibraryCore.Tests.Aot.Json", "Tests\LibraryCore.Tests.Aot.Json\LibraryCore.Tests.Aot.Json.csproj", "{01CB3A66-9110-4BFA-9D7D-3117578E1575}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FileShare", "FileShare", "{CBED8CCA-44B7-41D5-8918-6C2F7C66B5EC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Smb", "Smb", "{391A5319-213C-4F05-8A0B-4A60FBC089D2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibraryCore.FileShare.Smb", "Src\LibraryCore.FileShare.Smb\LibraryCore.FileShare.Smb.csproj", "{3C4FDEE8-41AB-407F-BE33-839D91C873BB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibraryCore.Tests.FileShare.Smb", "Tests\LibraryCore.Tests.FileShare.Smb\LibraryCore.Tests.FileShare.Smb.csproj", "{DFBE8FA0-1C88-483D-87FB-B9AEDF085BF6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -294,6 +302,14 @@ Global
{01CB3A66-9110-4BFA-9D7D-3117578E1575}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01CB3A66-9110-4BFA-9D7D-3117578E1575}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01CB3A66-9110-4BFA-9D7D-3117578E1575}.Release|Any CPU.Build.0 = Release|Any CPU
{3C4FDEE8-41AB-407F-BE33-839D91C873BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C4FDEE8-41AB-407F-BE33-839D91C873BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C4FDEE8-41AB-407F-BE33-839D91C873BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C4FDEE8-41AB-407F-BE33-839D91C873BB}.Release|Any CPU.Build.0 = Release|Any CPU
{DFBE8FA0-1C88-483D-87FB-B9AEDF085BF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DFBE8FA0-1C88-483D-87FB-B9AEDF085BF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DFBE8FA0-1C88-483D-87FB-B9AEDF085BF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DFBE8FA0-1C88-483D-87FB-B9AEDF085BF6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -354,6 +370,10 @@ Global
{9169F37A-8A3E-4E73-A75E-830A3F8E3224} = {896C5DB1-3EDC-4DCC-AAAB-73F705B52B18}
{896C5DB1-3EDC-4DCC-AAAB-73F705B52B18} = {75C64940-5413-442D-B876-AEAA8DC20FC0}
{01CB3A66-9110-4BFA-9D7D-3117578E1575} = {896C5DB1-3EDC-4DCC-AAAB-73F705B52B18}
{CBED8CCA-44B7-41D5-8918-6C2F7C66B5EC} = {75C64940-5413-442D-B876-AEAA8DC20FC0}
{391A5319-213C-4F05-8A0B-4A60FBC089D2} = {CBED8CCA-44B7-41D5-8918-6C2F7C66B5EC}
{3C4FDEE8-41AB-407F-BE33-839D91C873BB} = {391A5319-213C-4F05-8A0B-4A60FBC089D2}
{DFBE8FA0-1C88-483D-87FB-B9AEDF085BF6} = {391A5319-213C-4F05-8A0B-4A60FBC089D2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E61C7767-7D12-4313-8DA2-56AE8FC5AAD5}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using SMBLibrary;

namespace LibraryCore.FileShare.Smb.CustomExceptions;

public class SmbStatusException(NTStatus statusFound, string? Expression = null) : Exception
{
public NTStatus StatusFound { get; } = statusFound;
public string? Expression { get; } = Expression;

public override string ToString()
{
return $"SMB Status Exception: StatusId = {StatusFound} - Expression = {Expression}";
}
}
26 changes: 26 additions & 0 deletions Src/LibraryCore.FileShare.Smb/ExtensionMethods/ThrowUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using LibraryCore.FileShare.Smb.CustomExceptions;
using SMBLibrary;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("LibraryCore.Tests.FileShare.Smb")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

namespace LibraryCore.FileShare.Smb.ExtensionMethods;

internal static class ThrowUtilities
{
internal static NTStatus[] OnlySuccess { get; } = [NTStatus.STATUS_SUCCESS];

internal static void ThrowIfNotSuccessful(this NTStatus value, [CallerArgumentExpression(nameof(value))] string? expression = null)
{
value.ThrowIfNotStatus(OnlySuccess, expression);
}

internal static void ThrowIfNotStatus(this NTStatus value, NTStatus[] statusValuesToAllow, [CallerArgumentExpression(nameof(value))] string? expression = null)
{
if (!statusValuesToAllow.Contains(value))
{
throw new SmbStatusException(value, expression);
}
}
}
21 changes: 21 additions & 0 deletions Src/LibraryCore.FileShare.Smb/LibraryCore.FileShare.Smb.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Authors>Jason DiBianco</Authors>
<Description>Shared Package For Smb File Share Functionality</Description>
<Version>1.0.0</Version>
<RepositoryUrl>https://github.com/dibiancoj/LibraryCore</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Title>LibraryCore - SmbFileShare</Title>
<IsTrimmable>true</IsTrimmable>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
<PackageReference Include="SMBLibrary" Version="1.5.3.5" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
</ItemGroup>

</Project>
55 changes: 55 additions & 0 deletions Src/LibraryCore.FileShare.Smb/Service/ISmbFileShareService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using SMBLibrary;

namespace LibraryCore.FileShare.Smb.Service;

/// <summary>
/// Service class to interact with the SMB file share. This service will support Smb 2.0 on a linux or windows device / server.
/// </summary>
public interface ISmbFileShareService : IDisposable
{
/// <summary>
/// Get a list of file shares for a given server
/// </summary>
/// <returns>List of file share names</returns>
public IEnumerable<string> ListShares();

/// <summary>
/// Get a list of directories and files for a given share
/// </summary>
/// <param name="shareName">The share name to connect to</param>
/// <param name="fileName">File name to search for or use the default of a star to search for everything</param>
/// <param name="path">Search on a specific path. Format should be @"MySubFolder\MySubSubFolder. Or you can leave it blank to search from the root of the file share</param>
/// <returns>List of files and directories which you can parse through</returns>
public IEnumerable<QueryDirectoryFileInformation> ListFileAndDirectories(string shareName, string fileName = "*", string path = "");

/// <summary>
/// Read a file from a share and path
/// </summary>
/// <param name="shareName">The share name to connect to</param>
/// <param name="filePath">Full Path to the file to read</param>
/// <returns>The file in a byte array</returns>
public byte[] ReadFile(string shareName, string filePath);

/// <summary>
/// Create a file in a share and folder path
/// </summary>
/// <param name="shareName">The share name to connect to</param>
/// <param name="filePath">Full Path to the file to create</param>
/// <param name="fileStream">The stream of the file you want to create</param>
public void CreateFile(string shareName, string filePath, Stream fileStream);

/// <summary>
/// Delete a file from a share and folder path
/// </summary>
/// <param name="shareName">The share name to connect to</param>
/// <param name="filePath">Full Path to the file to delete</param>
public void DeleteFile(string shareName, string filePath);

/// <summary>
/// Move a file from a folder to another folder in a share
/// </summary>
/// <param name="shareName">Share name</param>
/// <param name="currentFilePath">The current path to the file. Ie: FolderA/File.pdf</param>
/// <param name="destinationFilePath">The destination to move it to. Ie: FolderB/File.pdf</param>
public void MoveFile(string shareName, string currentFilePath, string destinationFilePath);
}
Loading

0 comments on commit 9961237

Please sign in to comment.